Thursday, January 14, 2010

UTC, GMT and Datetime Related

In the modern world, a global universal time is essential for many systems where time syncronization is required. GMT and UTC are that kind of time, especially UTC - Coordinated Universal Time is adoped broadly, one reason is it is more precise (with atomic clock).

UTC and GMT are the same if we don't care precision. The difference between them are less than x seconds. UTC uses fix duration for second and and subordinate units, while uses variable minute and upper level units. because 1 minute = 60 seconds or 59 seconds or 61 seconds


For time synchronization, .Net framework provides following facilities:

DateTime class
DataTime.Now - current local time
DateTime.UtcNow - current UTC time

A datetime instance can associate with a DateTimeKind:

DateTime d = DateTime.SpecifyKind(datetimeInstance, kind);

where kind is a value of
enum DateTimeKind{Utc, Local, Unspecified}

As a result, d is a datetime instance accomodating a Kind property.

d.ToUniversalTime() will return a datetime with Utc kind.



TimeZone class
TimeZone currentZone = TimeZone.New;
string zoneName = currentZone.StandardName;
// convert local time to UTC time
currentZone .ToUniversalTime(localTime);
TimeSpan offset = currentZone.GetUtcOffset(localDateTime);
DayLightTime dayLight = currentZone.GetDayLightChanges(year);
DateTime start = dayLight.Start;
DateTime end = dayLight.End;
// delta is hour-difference of daylight time and the standard time
int delta = dayLight.Delta;

e.g.
Pacific Standard Time (PST) = GMT-8
Pacific Daylight Time (PDT) = GMT-7

then the Delta is 1



Timezone are usually geographically divided for civic convenience, but there are examples that based on political borders.


Culture class is related to datetime, but it just concerns about the format when converting to string.

No comments: