Tadeusz
Tadeusz

Reputation: 6913

What I need to save in data base in order to be able convert to UTC?

My problem is: I have a lot of data containg a date time (in own local time) and I need to convert It to UTC. It looks so:

class Data
{
  int id; //Some id for database
  DateTime time; //Time in local time, i need to convert it to utc
}

//Converting:
1)Find this id in database and extract some info (which excatly?)
2)Convert time to UTC

My question is: which sort of data I need to keep in database in order to convert time correctly?

My version is: to keep an integer difference in hours between local time and utc and convert by adding It to local time. But Is this appropriate solution?

Upvotes: 0

Views: 53

Answers (2)

William Mioch
William Mioch

Reputation: 917

The generally accepted solution is to always store the UTC date time and then convert it into local time when loading or displaying it to the user

Upvotes: 1

Petar Ivanov
Petar Ivanov

Reputation: 93090

Keeping an integer difference is not enough, because this doesn't differentiate between say daylight saving time (DST) and non-DST.

The best way is to keep a time zone. Universal and very well supported time zone system is Olson Tz.

They are references by names of big cities, which are representative of the time zone. For example Pasific Time would be America\Los_Angeles. Note that this includes both daylight and non-daylight based on what the stored date is. So you don't need to know when daylight caomes into play - this is incorporated in the olson time zone.

Look here too.

Upvotes: 0

Related Questions