Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Earnestly opened this issue Mar 5, 2022 · 10 comments
Closed

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

Earnestly opened this issue Mar 5, 2022 · 10 comments

Comments

@Earnestly
Copy link

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

I fixed the I/O issues in 7d1f2c2

$ 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
Copy link
Author

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
Copy link
Owner

%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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

Earnestly commented Mar 7, 2022

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
Copy link
Owner

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
Copy link
Author

Yeah that's fair enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants