Delphi – Gets TTIMEZONE in a non-local time zone and converts between time zones

See answer in English> How to convert a historical timestamp to a different time zone with DST in Delphi? 1
My subject may be very worded, but this is what I have

I have a web service that reports a timestamp in local time. If the server is in the affected area, the timestamp reflects daylight time. There is a second call to the web service in UTC The format retrieves the server time, so the server in Pacific Daylight Time (PDT) reports its UTC offset as -08:00, even though it is actually -07:00 due to PDT.

This is I call the result of returning the server time zone information.

Pacific Standard Time; -480; (UTC-08: 00) Pacific Time (US and Canada); Pacific Standard Time; Pacific Daylight Time; [01: 01 : 0001; 12: 31: 2006; 60; [0; 02] :0: 00; 4; 1; 0;]; [0; 02: 00: 00; 10; 5; 0;];] [01: 01: 2007; 12: 31: 9999; 60; [0; 02 :00: 00; 3; 2; 0;]; [0; 02: 00: 00; 11; 1; 0;];];

So, if the timestamp returned from the web service is 3/12/2013 12:00 am and the UTC offset is -08:00 and I live in a daytime exempt area in Arizona, my UTC offset The shift is -07: 00. How can I convert the returned timestamp to local time?

The killer here is web services that use local timestamps. If they just stick to a common format, my life will be easier. My current thinking is if I can use TTimeZone or equivalent structure To get the server information, then I can use the TTimeZone.IsDaylightTime(Timestamp) function to know if I need to subtract an hour offset and -07:00 local offset from the timestamp before using the -08:00 server. The correct local time.

you can use delphi-tzdb. pseudo code:

uses
..., TZDB;

procedure Main;
var
ServerTZID: string;
TZ: TTimeZone;
Stamp1, Stamp2: TDateTime;
begin
// 1. retrieve server timezone info
ServerTZID := ... // MyServer.GetTimezoneInfo; eg ' Pacific Standard Time';
// look up the retrieved timezone
TZ := TBundledTimeZone.GetTimeZone(ServerTZID); // nil if not found
// 2. retrieve server timestamp
Stamp1 := ... // MyServer.RetrieveTimestamp;
// 3. convert to UTC and back to local timezone
Stamp2 := TZ.Local.ToLocalTime(TZ.ToUniversalTime(Stamp1));

Writeln(Format('%s %s -> %s %s', [FormatDateTime('yyyy-mm-dd hh:n n:ss.zzz', Stamp1),
TZ.DisplayName, FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Stamp2),
TZ.Local.DisplayName])) ;
end;

See answer in English> How to convert a historical timestamp to a different time zone with DST in Delphi? 1 my topic It may be very worded, but this is what I have.

I have a web service that reports a timestamp in local time, and if the server is in the affected area, the timestamp reflects daylight time The web service also has a second call to retrieve the server time in UTC, so the server in Pacific Daylight Time (PDT) reports its UTC offset as -08:00, even though it is actually -07 due to PDT: 00.

This is the result of my call to return the server time zone information.

Pacific Standard Time; -480; (UTC-08: 00) Pacific Time (US and Canada); Pacific Standard time; Pacific Daylight Time; [01: 01: 0001; 12: 31: 2006; 60; [0; 02] :0: 00; 4; 1; 0;]; [0; 02: 00: 00; 10 ; 5; 0;];] [01: 01: 2007; 12: 31: 9999; 60; [0; 02: 00: 00; 3; 2; 0;]; [0; 02: 00: 00; 11 ; 1; 0;];];

So if the timestamp returned from the web service is 3/12/2013 12:00 am and the UTC offset is -08:00 and I live in Arizona The state’s daytime time exempt area, my UTC offset is -07:00. How can I convert the returned timestamp to local time?

The killer here is web services that use local timestamps. If they just stick to a common format, my life will be easier. My current thinking is if I can use TTimeZone or equivalent structure To get the server information, then I can use the TTimeZone.IsDaylightTime(Timestamp) function to know if I need to subtract an hour offset and -07:00 local offset from the timestamp before using the -08:00 server. The correct local time.

You can use delphi-tzdb. Pseudo code:

 uses
..., TZDB;

procedure Main;
var
ServerTZID: string;
TZ: TTimeZone;
Stamp1, Stamp2 : TDateTime;
begin
// 1. retrieve server timezone info
ServerTZID := ... // MyServer.GetTimezoneInfo; eg'Pacific Standard Time';
// look up the retrieved timezone
TZ := TBundledTimeZone.GetTimeZone(ServerTZID); // nil if not found
// 2. retrieve server timestamp
Stamp1 := ... // MyServer.RetrieveTimestamp ;
// 3. convert to UTC and back to local timezone
Stamp2 := TZ.Local.ToLocalTime(TZ.ToUniversalTime(Stamp1));

Writeln(Format( '%s %s -> %s %s', [FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Stamp1),
TZ.DisplayName, FormatDate Time('yyyy-mm-dd hh:nn:ss.zzz', Stamp2),
TZ.Local.DisplayName]));
end;

Leave a Comment

Your email address will not be published.