Closed Earnestly closed 2 years ago
Hi,
thanks for the idea. strptime(1)
seems an ideal candidate to push for libc input as well as output. I'll try and come up with a modified version.
Oh I was just about to prepare the code for strptime/strftime and noticed it already does what you want. The I/O seems broken though. Newlines are missing.
I fixed the I/O issues in 7d1f2c266
$ strptime -f '%a %b %e %H:%M:%S %Z %Y' 2022-03-07T09:13:41 -i %FT%T
Mon Mar 7 09:13:41 UTC 2022
I was using this with dateadd
and I'm not sure how strptime
helps, perhaps by transforming "libc" into something dateutils understands? dateadd "$(strptime ...)" ...
%e
is simply %d
for input:
$ dateadd -i '%a %b %d %T UTC %Y' "Mon Mar 7 09:13:41 UTC 2022" +1w
2022-03-14T09:13:41
There's the %Z
issue again though.
I know dateutils has its own, but portable (POSIX) systems expect %e
(strftime(3)
) and so that's what they produce, e.g. locale date_fmt
. Do you suggest modifying the output of this command and changing %e
to %d
via sed or so?
I don't know the exact use case here. If I had to do it and there's a date in the style of locale date_fmt
, I'd use strptime(1)
first:
$ strptime -ti "`locale date_fmt`" "Mon Mar 7 09:13:41 UTC 2022"
2022-03-07 09:13:41 UTC
which can then be passed to dateadd
and then to strptime again (this time with ISO input and date_fmt output):
$ strptime -ti "`locale date_fmt`" "Mon Mar 7 09:13:41 UTC 2022" | dateadd +1w | strptime -ti %FT%T -f "`locale date_fmt`"
Mon Mar 14 09:13:41 UTC 2022
Or you explain what you need to accomplish.
Hm, I thought I had; here is what lead me here: datefmt=$(locale date_fmt); dateadd -f "$datefmt" -i "$datefmt" now 2h
The use of datefmt
as a variable came about due to dateutils not supporting strftime's %c
. Although since then I've moved on and have been playing with dateutil's ability to suffix days with "rd", "th", etc. which is cute.
Your example in my world would be:
$ dateadd now 2h | strptime -ti %FT%T -f "$datefmt"
i.e. you calculate the new timestamp then pass it to strptime to format it using libc's specifiers.
Yeah that's fair enough
As the
date*
utils repurpose%c
for the weekday count in a month I attempted to uselocale date_fmt
instead.Currently on my system
LC_TIME=C locale date_fmt
produces%a %b %e %H:%M:%S %Z %Y
which when used with-f
produces, for example:Sat Mar % 16:54:07 +00:00 2022
According to the 2017 POSIX version of
strftime(3)
%e
is defined as:Is there either a way to produce
-f
output in locale date format, or for dateutils to support morestrftime
spec?