Score:5

date pipe - bug or my mistake?

sy flag

I faced a strange behavior of the date command:

echo '1 JAN 2023' | LC_ALL=en_US.utf8  date -d -

This produces plainly wrong output:

Sun Jun 11 12:00:00 AM CEST 2023

But this is ok:

LC_ALL=en_US.utf8  date -d '1 Jan 2023'  # Sun Jan  1 12:00:00 AM CET 2023

This is ok as well:

my_date='1 JAN 2023'
LC_ALL=en_US.utf8  date -d  "$my_date" # Sun Jan  1 12:00:00 AM CET 2023

I cannot get the pipe version working. Is a bug or am I doing something wrong?

Score:17
hr flag

The date command's --date / -d option expects a string argument - the standard input is discarded, and - is parsed as a date input string according to the GNU date command's General date syntax, which says

Hyphens not followed by a digit are currently ignored.

and

The empty string means the beginning of today (i.e., midnight).

So your command is simply equivalent to LC_ALL=en_US.utf8 date -d 'today 00:00'.

To take input from a pipe, you'd either need to convert stdin to an argument - for example with xargs:

$ echo '1 JAN 2023' | LC_ALL=en_US.utf8  xargs -d '\n' date -d
Sun Jan  1 12:00:00 AM EST 2023

or (as suggested by Raffa) by passing it through to a command that does read standard input, capturing that command's output in a command substitution:

$ echo '1 JAN 2023' | LC_ALL=en_US.utf8  date -d "$(cat -)"
Sun Jan  1 12:00:00 AM EST 2023

It's much simpler to use the -f option instead, which does read from standard input:

$ echo '1 JAN 2023' | LC_ALL=en_US.utf8  date -f -
Sun Jan  1 12:00:00 AM EST 2023

From man date:

   -f, --file=DATEFILE
          like --date; once for each line of DATEFILE
Raffa avatar
jp flag
Might be worth noting that utilities that can read and *echo* from the pipe such as `cat` and `tee` should work as well in a command substitution syntax ... e.g. `echo '1 JAN 2023' | date -d "$(cat)"` and `echo '1 JAN 2023' | date -d "$(tee)"`
hr flag
@Raffa thanks - done
xerostomus avatar
sy flag
Thank you all, I guess that "date -f - " is the best as it is no bashism, this is as it ought to work. (Stdin is a virtual file in fact.) It is nice to have so many smart people around, isn't it? :-)
Score:2
cn flag

As others mentioned, date doesn't read from standard input; the -d option expects the date string in the next argument. You can use command substitution to put the output of a command there.

date -d "$(echo '1 JAN 2023')"
Score:2
ua flag

I don't know what version of date are you using but man date doesn't mention that when - is specified input is taken from standard input:

$ echo something | date -d -
Sun Jun 11 00:00:00 CEST 2023
$ date -d -
Sun Jun 11 00:00:00 CEST 2023
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.