hroptatyr / dateutils

nifty command line date and time utilities; fast date calculations and conversion in the shell
http://www.fresse.org/dateutils/
Other
616 stars 42 forks source link

Support for %e when used as output from locale date_fmt #142

Closed Earnestly closed 2 years ago

Earnestly commented 2 years ago

As the date* utils repurpose %c for the weekday count in a month I attempted to use locale 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:

e Replaced by the day of the month as a decimal number [1,31]; a single digit is preceded by a space. [tm_mday]

Is there either a way to produce -f output in locale date format, or for dateutils to support more strftime spec?

hroptatyr commented 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.

hroptatyr commented 2 years ago

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.

hroptatyr commented 2 years ago

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
Earnestly commented 2 years ago

I was using this with dateadd and I'm not sure how strptime helps, perhaps by transforming "libc" into something dateutils understands? dateadd "$(strptime ...)" ...

hroptatyr commented 2 years ago

%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.

Earnestly commented 2 years ago

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?

hroptatyr commented 2 years ago

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.

Earnestly commented 2 years ago

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.

hroptatyr commented 2 years ago

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.

Earnestly commented 2 years ago

Yeah that's fair enough