comp.lang.ada
 help / color / mirror / Atom feed
* Getting the 3 letter time zone abbreviation
@ 2020-04-29  8:46 Bob Goddard
  2020-04-29  9:09 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: Bob Goddard @ 2020-04-29  8:46 UTC (permalink / raw)


I'm sure this has been asked many times...

I need to get the 3 letter time zone abbreviation.

Does anyone have code that can do that?

Neither Ada.Calander, not ada_util can get it.

I did take a look at the tz db source code, and I shuddered multiple times.



B

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-29  8:46 Getting the 3 letter time zone abbreviation Bob Goddard
@ 2020-04-29  9:09 ` Dmitry A. Kazakov
  2020-04-29 19:20   ` Bob Goddard
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-29  9:09 UTC (permalink / raw)


On 2020-04-29 10:46, Bob Goddard wrote:
> I'm sure this has been asked many times...
> 
> I need to get the 3 letter time zone abbreviation.

3-4 you mean, e.g. CEST.

> Does anyone have code that can do that?

I had only a partial success. I used GTK/GLib time zone functions. The 
abbreviation of the zone name is the thing. Unfortunately it works 
poorly under Windows, and Windows updates tend to break time zone 
settings [*] I needed to plant various fallback to deduce the zone from 
UTC offset.

Anyway, Ada bindings are here:

http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#5.14

-----------
* I believe it was the case why one could not log into Origin account 
for a couple of days not so long ago. Their server verified the time 
zone and blocked access because Windows reported garbage.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-29  9:09 ` Dmitry A. Kazakov
@ 2020-04-29 19:20   ` Bob Goddard
  2020-04-29 19:53     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: Bob Goddard @ 2020-04-29 19:20 UTC (permalink / raw)


On Wednesday, 29 April 2020 10:09:39 UTC+1, Dmitry A. Kazakov  wrote:
> On 2020-04-29 10:46, Bob Goddard wrote:
> > I'm sure this has been asked many times...
> > 
> > I need to get the 3 letter time zone abbreviation.
> 
> 3-4 you mean, e.g. CEST.
> 
> > Does anyone have code that can do that?
> 
> I had only a partial success. I used GTK/GLib time zone functions. The 
> abbreviation of the zone name is the thing. Unfortunately it works 
> poorly under Windows, and Windows updates tend to break time zone 
> settings [*] I needed to plant various fallback to deduce the zone from 
> UTC offset.
> 
> Anyway, Ada bindings are here:
> 
> http://www.dmitry-kazakov.de/ada/gtkada_contributions.htm#5.14
> 
> -----------
> * I believe it was the case why one could not log into Origin account 
> for a couple of days not so long ago. Their server verified the time 
> zone and blocked access because Windows reported garbage.

Seems easier just to import strftime and call it requesting just "%Z". This is on Linux, but MS suggests it should also work on Windows.


B

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-29 19:20   ` Bob Goddard
@ 2020-04-29 19:53     ` Dmitry A. Kazakov
  2020-04-30 18:59       ` Bob Goddard
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-29 19:53 UTC (permalink / raw)


On 2020-04-29 21:20, Bob Goddard wrote:

> Seems easier just to import strftime and call it requesting just "%Z". This is on Linux, but MS suggests it should also work on Windows.

An interesting idea. Did you try it under Windows? (There is a 
suspicious remark that it depends on the setlocale)

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-29 19:53     ` Dmitry A. Kazakov
@ 2020-04-30 18:59       ` Bob Goddard
  2020-04-30 21:11         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: Bob Goddard @ 2020-04-30 18:59 UTC (permalink / raw)


On Wednesday, 29 April 2020 20:53:11 UTC+1, Dmitry A. Kazakov  wrote:
> On 2020-04-29 21:20, Bob Goddard wrote:
> 
> > Seems easier just to import strftime and call it requesting just "%Z". This is on Linux, but MS suggests it should also work on Windows.
> 
> An interesting idea. Did you try it under Windows? (There is a 
> suspicious remark that it depends on the setlocale)

'Fraid not, I'm a Linux user. I just noticed that Windows does have it.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-30 18:59       ` Bob Goddard
@ 2020-04-30 21:11         ` Dmitry A. Kazakov
  2020-05-02 12:46           ` Bob Goddard
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-04-30 21:11 UTC (permalink / raw)


On 2020-04-30 20:59, Bob Goddard wrote:
> On Wednesday, 29 April 2020 20:53:11 UTC+1, Dmitry A. Kazakov  wrote:
>> On 2020-04-29 21:20, Bob Goddard wrote:
>>
>>> Seems easier just to import strftime and call it requesting just "%Z". This is on Linux, but MS suggests it should also work on Windows.
>>
>> An interesting idea. Did you try it under Windows? (There is a
>> suspicious remark that it depends on the setlocale)
> 
> 'Fraid not, I'm a Linux user. I just noticed that Windows does have it.

OK, I tested it. As expected it does not work. For example this one:
---------------------------
with Ada.Command_Line;           use Ada.Command_Line;
with Interfaces.C;               use Interfaces.C;
with Ada.Exceptions;             use Ada.Exceptions;
with Ada.Text_IO;                use Ada.Text_IO;

with System;

procedure Strftime_Test is

    type tm is record
       tm_sec   : int;
       tm_min   : int;
       tm_hour  : int;
       tm_mday  : int;
       tm_mon   : int;
       tm_year  : int;
       tm_wday  : int;
       tm_yday  : int;
       tm_isdst : int;
    end record;
    pragma Convention (C, tm);
    type tm_Ptr is access all tm;
    pragma Convention (C, tm_Ptr);

    type time_t is new Interfaces.Unsigned_64;

    function localtime (timep : access time_t) return tm_Ptr;
    pragma Import (C, localtime);

    function time (destTime : System.Address := System.Null_Address)
       return time_t;
    pragma Import (C, time, "_time64");

    function strftime
             (  strDest : char_array;
                maxsize : size_t;
                format  : char_array;
                timeptr : access tm
             )  return size_t;
    pragma Import (C, strftime);

    Result    : size_t;
    Buffer    : char_array (1..200);
    Now       : aliased time_t := 0;
    Local_Ptr : tm_Ptr;
begin
    Now := time;
    Put_Line ("Time=" & time_t'Image (Now));
    Local_Ptr := localtime (Now'Access);
    if Local_Ptr /= null then
       declare
          Local : tm renames localtime (Now'Access).all;
       begin
          Put_Line
          (  int'Image (Local.tm_year)
          &  " -"
          &  int'Image (Local.tm_mon)
          &  " -"
          &  int'Image (Local.tm_mday)
          &  " "
          &  int'Image (Local.tm_hour)
          &  " :"
          &  int'Image (Local.tm_min)
          &  " :"
          &  int'Image (Local.tm_sec)
          );
          Result := strftime
                    (  Buffer,
                       Buffer'Length,
                       To_C ("%#Z"),
                       Local'Access
                    );
          Put_Line ("Result=" & To_Ada (Buffer (1..Result), False));
       end;
    end if;
    Set_Exit_Status (0);
exception
    when Error : Status_Error | Data_Error =>
       Put_Line (Exception_Message (Error));
       Set_Exit_Status (1);
    when Error : others =>
       Put_Line ("Fault: " & Exception_Information (Error));
       Set_Exit_Status (2);
end Strftime_Test;
----------------------------------

Gives the output on my Windows machine:

    Result=W. Europe Daylight Time

instead of

    CEST

Windows POSIX layer relies on Windows API. If the API does something 
wrong, so would whatever POSIX function.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-04-30 21:11         ` Dmitry A. Kazakov
@ 2020-05-02 12:46           ` Bob Goddard
  2020-05-02 15:08             ` Bob Goddard
  0 siblings, 1 reply; 10+ messages in thread
From: Bob Goddard @ 2020-05-02 12:46 UTC (permalink / raw)


Here goes...

On Linux and the various BSD's, the tm structure has been extended to include:

  long int tm_gmtoff;           /* Seconds east of UTC.  */
  const char *tm_zone;          /* Timezone abbreviation.  */

When you make a call to localtime_r, these are filled in with the correct into, at least on Linux.

The other big iron Unix's do not have this extension, and neither does Windows.

Windows also does not have localtime_r, instead it has localtime_s plus its 32 & 64 versions.

On the other Unix's & Windows, a call made to tzset (_tzset on Windows), which fills in tzname (_tzname under Windows). This should contain 2 record for non-saving & saving timezone name. and the correct entry can be garnered by way of tm_isdst.

To set another timezone, external var TZ should be set.

And don't test an example time of 1234 seconds past epoch for the UK expecting GMT. The UK was in fact in daylight saving time and double time (I think) for a few years. In my defence, I was only about 7yo and totally forgot about it.

There, clear as mud.

This works on Linux, the tm_zone is filled in by the call automatically...


with System;
with Interfaces;
with Interfaces.C;
with Interfaces.C.Strings;

package Unix is
   type tm is record
      tm_sec    : Interfaces.C.int;
      tm_min    : Interfaces.C.int;
      tm_hour   : Interfaces.C.int;
      tm_day    : Interfaces.C.int;
      tm_mon    : Interfaces.C.int;
      tm_year   : Interfaces.C.int;
      tm_wday   : Interfaces.C.int;
      tm_yday   : Interfaces.C.int;
      tm_isdst  : Interfaces.C.int;
      tm_gmtoff : Interfaces.C.long;
      tm_zone   : Interfaces.C.Strings.chars_ptr;
   end record;
   pragma Convention (C_Pass_By_Copy, tm);
   
   procedure localtime_r (T : System.Address; TM_Struct : System.Address);
   pragma Import (C, localtime_r, "localtime_r");
end Unix;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-05-02 12:46           ` Bob Goddard
@ 2020-05-02 15:08             ` Bob Goddard
  2020-05-02 15:38               ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: Bob Goddard @ 2020-05-02 15:08 UTC (permalink / raw)


And apparently this will not work on non BSD/Linux system, as we need access to the external var tzname, which, if going by the similarly external errno, would require a small C program.

Unless someone knows how to get access to arbitrary external vars.

Still, there is no reason why this could not be implemented in GNAT.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-05-02 15:08             ` Bob Goddard
@ 2020-05-02 15:38               ` Dmitry A. Kazakov
  2020-05-02 19:25                 ` Bob Goddard
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2020-05-02 15:38 UTC (permalink / raw)


On 2020-05-02 17:08, Bob Goddard wrote:
> And apparently this will not work on non BSD/Linux system, as we need access to the external var tzname, which, if going by the similarly external errno, would require a small C program.
> 
> Unless someone knows how to get access to arbitrary external vars.

http://ada-auth.org/standards/12rm/html/RM-A-17.html

> Still, there is no reason why this could not be implemented in GNAT.

You can propose an AI for the incoming standard.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Getting the 3 letter time zone abbreviation
  2020-05-02 15:38               ` Dmitry A. Kazakov
@ 2020-05-02 19:25                 ` Bob Goddard
  0 siblings, 0 replies; 10+ messages in thread
From: Bob Goddard @ 2020-05-02 19:25 UTC (permalink / raw)


On Saturday, 2 May 2020 16:38:43 UTC+1, Dmitry A. Kazakov  wrote:
> On 2020-05-02 17:08, Bob Goddard wrote:
> > And apparently this will not work on non BSD/Linux system, as we need access to the external var tzname, which, if going by the similarly external errno, would require a small C program.
> > 
> > Unless someone knows how to get access to arbitrary external vars.
> 
> http://ada-auth.org/standards/12rm/html/RM-A-17.html
> 
> > Still, there is no reason why this could not be implemented in GNAT.
> 
> You can propose an AI for the incoming standard.

RM-A-17 is for environmental vars, rather than external, ie library or system ones;

Anyway, turns out Windows does not support the IANA 3/4 letter tz database.

Calling tzset and examining the returned array, returns a string similar to "New Zealand Daylight Time".

tzset, at least on Linux, and I assume every other unix type system which uses the tz data base does return the 3/4 letter tz name.

Ho hum!

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-05-02 19:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-29  8:46 Getting the 3 letter time zone abbreviation Bob Goddard
2020-04-29  9:09 ` Dmitry A. Kazakov
2020-04-29 19:20   ` Bob Goddard
2020-04-29 19:53     ` Dmitry A. Kazakov
2020-04-30 18:59       ` Bob Goddard
2020-04-30 21:11         ` Dmitry A. Kazakov
2020-05-02 12:46           ` Bob Goddard
2020-05-02 15:08             ` Bob Goddard
2020-05-02 15:38               ` Dmitry A. Kazakov
2020-05-02 19:25                 ` Bob Goddard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox