comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Getting the 3 letter time zone abbreviation
Date: Thu, 30 Apr 2020 23:11:34 +0200
Date: 2020-04-30T23:11:34+02:00	[thread overview]
Message-ID: <r8feu5$1o0b$1@gioia.aioe.org> (raw)
In-Reply-To: ff1dabc9-db8c-483a-8a54-b1c2978fc7cf@googlegroups.com

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

  reply	other threads:[~2020-04-30 21:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
replies disabled

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