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: arnold
Subject: Re: strftime() using '-' to remove leading 0s functionality removed or broken?
Date: Sun, 28 Jan 2024 11:27:12 -0700
User-agent: Heirloom mailx 12.5 7/5/10

Doc update now pushed to git. Thanks.

Ed Morton <mortoneccc@comcast.net> wrote:

> Looks good, thanks!
>
> On 1/28/2024 1:51 AM, arnold@skeeve.com wrote:
> > Here is what I plan to add to the manual.
> >
> > diff --git a/doc/gawk.texi b/doc/gawk.texi
> > index 1b758db3..e4da785f 100644
> > --- a/doc/gawk.texi
> > +++ b/doc/gawk.texi
> > @@ -19949,6 +19949,14 @@ The date in OpenVMS format (e.g., 
> > @samp{20-JUN-1991}).
> >   Additionally, the alternative representations are recognized but their
> >   normal representations are used.
> >   
> > +@quotation NOTE
> > +Similar to @code{printf()}, some versions of @code{strftime()} support
> > +the use of flags between the @code{%} and the format specification
> > +letter.  Check your local manpage for @code{strftime()} to see if it
> > +supports flags or not, and what they are. Be aware, however, that using
> > +any such flags is likely to make your script less portable to other 
> > systems.
> > +@end quotation
> > +
> >   @cindex @code{date} utility @subentry POSIX
> >   @cindex POSIX @command{awk} @subentry @code{date} utility and
> >   The following example is an @command{awk} implementation of the POSIX
> >
> >
> > Ed Morton<mortoneccc@comcast.net>  wrote:
> >
> >> Arnold - Yeah I don’t blame you. You could spend your life trying to
> >> keep up with things outside of your control and there’s definitely a
> >> strong argument for “if you’re using something that’s not documented
> >> then you’re on your own”. Thanks for taking a look.
> >>
> >> Ed Morton
> >>
> >>> On Jan 27, 2024, at 1:06 PM,arnold@skeeve.com  wrote:
> >>>
> >>> Hi Ed.
> >>>
> >>> It's definitely an issue with the underlying C library version of
> >>> strftime, which is what gawk uses.
> >>>
> >>> I'll take a look at the manual, but I'm not sure I want to
> >>> get into details like '-' flags which are unportable.
> >>>
> >>> Thanks,
> >>>
> >>> Arnold
> >>>
> >>> Ed Morton<mortoneccc@comcast.net>  wrote:
> >>>
> >>>> 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]