TDateTime Type
The TDateTime
type stores a date and time as the
number of days since the start of the day on December 30, 1899, which
is the standard used in OLE automation. The integer part is the
number of days, and the fractional part specifies the time of day.
TDateTime
does not store a time zone, so if you
want to record a date and time in a file or database, you should
convert the local time to UTC (Coordinated Universal Time). When
loading the date and time from the file or database, convert back to
local time. That lets users work in different time zones without
running into
problems.
You can count the number of days between two
TDateTime
values by subtracting, and converting
the result to an integer. Whether you round off or truncate depends
on the circumstances. Do you want to count the number of
“days” between 1:00 A.M. on March 3 to 11:00 P.M. on
March 4 as one day (truncate) or two days (round off)?
Unix represents a date and time as the number of seconds since
midnight January 1, 1970 UTC. If a file or database contains a Unix
time, you can convert it to a Delphi TDateTime
by
dividing by SecsPerDay
and then adding 25569. That
gives you the UTC date and time; you must convert this to a local
date and time before using it in most Delphi programs.
// Convert a local time to UTC by adding the time zone bias. // To convert from UTC back to local, subtract the bias. function LocalToUTC(DateTime: TDateTime): TDateTime; var Info: TTimeZoneInformation; Bias: LongInt; begin case GetTimeZoneInformation(Info) of Time_Zone_Id_Standard, Time_Zone_Id_Daylight: begin // The value returned by GetTimeZoneInformation is for the current // date and time, not for DateTime. Determine whether DateTime // is in standard or daylight savings time. if IsDaylightSavingsTime(DateTime, Info) then Bias := Info.Bias + Info.DaylightBias else Bias := Info.Bias + Info.StandardBias; Result := DateTime + Bias / MinutesPerDay; end; Time_Zone_Id_Unknown: Result := DateTime + Info.Bias / MinutesPerDay; else RaiseLastWin32Error; Result := DateTime; // turn off Delphi's warning end; end;