Delphi - get TTimeZone for a non-local time zone and convert between time zones

My topic is probably terribly worded, but here's what I have.

I have a web service that reports timestamps in its local time, and the timestamps reflect daylight hours if the server is in a vulnerable region. The web service also has a second call to get the server time in UTC, so the server in Pacific Daylight Time (PDT) reports its UTC offset as -08: 00, although it is effectively -07: 00 due to PDT.

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

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

So, if the time stamp returned by the web service is 3/12/2013 12:00, and the UTC offset is -08: 00, and I live in the Arizona daylight saving zone, where my UTC offset is -07: 00 How can I convert from a returned timestamp to my local time?

Killer here is a web service that uses local time in timestamps. If they simply adhered to a universal format, my life would be easy. My current thinking is that if I can get the server information in TTimeZone or equivalent structure, then I can use the TTimeZone.IsDaylightTime (Timestamp) function to find out if I need to subtract the hour from the timestamp before using the server - 08:00 offset and -07: 00 local offset to get the correct local time.

+4
source share
1 answer

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, FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Stamp2), TZ.Local.DisplayName])); end; 
+7
source

Source: https://habr.com/ru/post/1468747/


All Articles