bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: strftime() using '-' to remove leading 0s functionality removed or b


From: Ed Morton
Subject: Re: strftime() using '-' to remove leading 0s functionality removed or broken?
Date: Sat, 27 Jan 2024 08:15:23 -0600
User-agent: Mozilla Thunderbird

Apparently the person for whom the `-` works in gawk 4.2.1 also sees it working in gawk 5.3.0, see:

   https://imgur.com/a/YtgCkA6

They are testing on a Mac while I tested on 2 different laptops, both running Windows 11, one in cygwin and the other git bash, so I'm guessing this is something to do with underlying primitives. It's odd to me  that `date` behaves differently from `gawk` in this regard but I guess it's just implemented differently. FWIW perl behaves the same way as gawk:

   $ cat tst.prl
   #!/usr/bin/perl
   use POSIX qw(strftime);

   # Modules used
   use strict;
   use warnings;

   # Print function
   printf("Without -: %s\n", strftime "%m", localtime);
   printf("With -:%s\n", strftime "%-m", localtime);

   $ ./tst.prl
   Without -: 01
   With -:
   $

and so does python:

   $ cat tst.py
   #!/usr/bin/python
   from datetime import datetime

   now = datetime.now()

   print("Without -:", now.strftime("%m"))
   print("With -:", now.strftime("%-m"))

   $ ./tst.py
   Without -: 01
   With -:

Assuming it's not something that can/should be made to work portably, maybe it's worth a brief note in the documentation that this is a thing, just like the underlying primitives impact on rounding and reading binary files are described elsewhere in the docs?

    Ed.

On 1/27/2024 6:56 AM, Ed Morton wrote:
Someone posted an answer at https://stackoverflow.com/a/77884684/1745001 that puts a `-` in front of `strftime()` format specifiers to remove leading `0`s so that, for example, we can print the month number by doing:

    awk 'BEGIN{print strftime("%-m")}'

and get `1` output instead of the `01` we'd get with

    awk 'BEGIN{print strftime("%m")}'

That's consistent with how GNU date (and apparently various other tools) works:

    $ date +'%-m'
    1
    $

    $ date +'%m'
    01
    $

and it's what that SO answer shows with gawk 4.2.1.

When I try to do the same with gawk 5.0.0 or later, though, then I get:

    $ awk 'BEGIN{print strftime("%-m")}'

    $

    $ awk 'BEGIN{print strftime("%m")}'
    01
    $

i.e. adding the `-` makes `strftime()` produce no output.

That functionality isn't documented in the manual best I can tell - is that functionality that was removed or is it breakage or something else?

    Ed.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]