Invalid date error by date command
Solution 1:
I'm almost sure this is due to the changeover to Daylight Saving Time in the given timezone: effectively this means that an hour "disappears" (and hence becomes "invalid
").
In my own timezone, DST started at 2AM on Sunday 10th March, so that hour is invalid:
$ cat /etc/timezone
America/Toronto
$ date --date="2019-03-10 02:00:00"
date: invalid date ‘2019-03-10 02:00:00’
whereas the times immediately before and after are valid:
$ date --date="2019-03-10 01:59:59"
Sun Mar 10 01:59:59 EST 2019
$ date --date="2019-03-10 03:00:00"
Sun Mar 10 03:00:00 EDT 2019
In timezones where the change over happens at midnight, the bare date appears invalid because GNU date
assumes a time of midnight:
$ TZ=Asia/Tehran date --date='2019-03-22'
date: invalid date ‘2019-03-22’
but one hour later is valid:
$ TZ=Asia/Tehran date --date='2019-03-22 01:00:00'
Fri Mar 22 01:00:00 +0430 2019
See also Invalid Date Linux
Solution 2:
$ date_ascii="2019-03-22"
$ printf "%s" "$date_ascii" | od -c
0000000 2 0 1 9 - 0 3 - 2 2
0000012
$ TZ=Asia/Shanghai date -d "$date_ascii"
Fri Mar 22 00:00:00 America 2019
and
$ date_unicode="2019‑03‑22"
$ printf "%s" "$date_unicode" | od -c
0000000 2 0 1 9 342 200 221 0 3 342 200 221 2 2
0000016
$ TZ=Asia/Shanghai date -d "$date_unicode"
date: invalid date ‘2019‑03‑22’