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;
source share