Convert TDateTime to a different time zone Regardless of the local time zone

Regardless of what the user time zone is set for using Delphi 2007, I need to determine the time (TDateTime) in the eastern time zone.

How can i do this? Of course, you need to know the time of daylight saving time.

+4
source share
3 answers

There is no time zone information in TDateTime (this is just a double date as an integer, time as a decimal), so you need this separately. You will also need your logic for DST, I do not believe that there is anything in Delphi. Then use the IncHour function in DateUtils.pas to change the TDateTime to Eastern Time Zone.

There are probably web services that will do this for you. Is your application required for independent use or can it connect to the Internet for this?

+3
source

If I understand you correctly, you want Eastern time to match the current system time.

To do this, use the WiNAPI GetSystemTime() function to get the current computer time in UTC. UTC is independent of time zones and will always receive time on the first meridian.

You can then use the WinAPI function SystemTimeToTzSpecificLocalTime() to calculate the local time in any other given time zone since UTC. In order for SystemTimeToTzSpecificLocalTime() to work, you need to specify a TTimeZoneInformation record filled with the correct information for the time zone in which you want to convert.

The following sample always gives you the local time in Eastern time in accordance with the Energy Policy Act of 2005.

 function GetEasternTime: TDateTime; var T: TSystemTime; TZ: TTimeZoneInformation; begin // Get Current time in UTC GetSystemTime(T); // Setup Timezone Information for Eastern Time TZ.Bias:= 0; // DST ends at First Sunday in November at 2am TZ.StandardBias:= 300; TZ.StandardDate.wYear:= 0; TZ.StandardDate.wMonth:= 11; // November TZ.StandardDate.wDay:= 1; // First TZ.StandardDate.wDayOfWeek:= 0; // Sunday TZ.StandardDate.wHour:= 2; TZ.StandardDate.wMinute:= 0; TZ.StandardDate.wSecond:= 0; TZ.StandardDate.wMilliseconds:= 0; // DST starts at Second Sunday in March at 2am TZ.DaylightBias:= 240; TZ.DaylightDate.wYear:= 0; TZ.DaylightDate.wMonth:= 3; // March TZ.DaylightDate.wDay:= 2; // Second TZ.DaylightDate.wDayOfWeek:= 0; // Sunday TZ.DaylightDate.wHour:= 2; TZ.DaylightDate.wMinute:= 0; TZ.DaylightDate.wSecond:= 0; TZ.DaylightDate.wMilliseconds:= 0; // Convert UTC to Eastern Time Win32Check(SystemTimeToTzSpecificLocalTime(@TZ, T, T)); // Convert to and return as TDateTime Result := EncodeDate(T.wYear, T.wMonth, T.wDay) + EncodeTime(T.wHour, T.wMinute, T.wSecond, T.wMilliSeconds); end; procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption:= 'In New York Citiy, it is now ' + DateTimeToStr(GetEasternTime); end; 
+11
source

To be specific, TDateTime is not an object, it is just an alias for double.

+1
source

All Articles