Recent

Author Topic: PascalTZ capabilities  (Read 449 times)

MaxCuriosus

  • Full Member
  • ***
  • Posts: 111
PascalTZ capabilities
« on: September 18, 2020, 03:15:22 pm »
How can I get the abbreviated timezone given a date/time value and a location name, for exemple (as of this writing):

"Now" and "Europe/London" yielding "BST"?

Thaddy

  • Hero Member
  • *****
  • Posts: 10568
Re: PascalTZ capabilities
« Reply #1 on: September 18, 2020, 03:34:36 pm »
To convert to strings (you seem to gear towards scripting languages over compiled languages) you need a look-up table or a dictionary.
I am not aware of a proper Pascal implementation, but it is easy to do, since such tables are plenty available.

Note, however, that time zones are not always perfectly mapped to actual bearings: you really need to look them up.
But modern Pascal can do that.
https://en.wikipedia.org/wiki/Time_zone
https://en.wikipedia.org/wiki/Greenwich_Mean_Time
BTW London is too big to fit in GMT. It does not fit into the ten minute resolution.
(Maybe after brexit?    :( )
« Last Edit: September 18, 2020, 03:51:01 pm by Thaddy »

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: PascalTZ capabilities
« Reply #2 on: September 18, 2020, 06:34:40 pm »
How can I get the abbreviated timezone given a date/time value and a location name, for exemple (as of this writing):

"Now" and "Europe/London" yielding "BST"?
You can use one of the class methods such as GMTToLocalTime for that.

Code: Pascal  [Select][+][-]
  1. var
  2.   Abbreviation: String
  3.  
  4.   ...
  5.   PTZ:= TPascalTZ.Create;
  6.   ...
  7.   PTZ.GMTToLocalTime(NowUTC, 'Europe/London', Abbreviation);
  8.   WriteLn('Timezone abbreviation for current DST : ', Abreviation);
  9.   ..
  10.   PTZ.Free;
  11.   ...
  12.  

Do note however that this requires function nowutc() (from lazsysutils) and not function now(). As written in the FPC documentation, using timezone related functions from Pascal itself are marked as not being trustworthy (as it goes beyond the scope to interpret/support  timezones/DST's correctly, hence the existence of units like PascalTZ).

However, if you know the name of your own timezone then you can convert your local now() time to another local time using class method TimeZoneToTimeZone().


edit: I forgot to add that IANA itself is very strict with regards to use of the abbreviations and as such you won't be able to find many/all of them because there is a lot of ambiguity there.
« Last Edit: September 18, 2020, 06:44:52 pm by TRon »

MaxCuriosus

  • Full Member
  • ***
  • Posts: 111
Re: PascalTZ capabilities
« Reply #3 on: September 19, 2020, 09:24:27 pm »
(you seem to gear towards scripting languages over compiled languages)
Not so. I highly prefer compiled Pascal whenever possible. But sometimes a small script is a simpler solution.

MaxCuriosus

  • Full Member
  • ***
  • Posts: 111
Re: PascalTZ capabilities
« Reply #4 on: September 19, 2020, 09:25:31 pm »
TRon,
thank you for your solution, simple and easy.

It would be useful if the wiki documentation would be expanded to better explain the capabilities of PascalTZ.

 

TinyPortal © 2005-2018