8

I want to get the date information with this command:

date --date=2019-03-22

or

date --date=2019/03/22

but it shows this error:

date: invalid date ‘2019-03-22’

or

 date: invalid date ‘2019/03/22’

as you can see it is not related to dash. the same thing happens with slash.

When I use another date like

date --date=2019-03-21

It shows the information correctly.

It shouldn't be related to the bad dash character. because I just deleted the last 2 and replaced it with 1 and the output is OK.

What is going wrong?

Result of some commands for more information:

$ date --version
date (GNU coreutils) 8.28
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.
$ type -a date
date is /bin/date
$ uname -m
x86_64
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:    18.04
Codename:   bionic
$ which date
/bin/date
$ apt-cache policy coreutils
coreutils:
  Installed: 8.28-1ubuntu1
  Candidate: 8.28-1ubuntu1
  Version table:
 *** 8.28-1ubuntu1 500
        500 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 Packages
        100 /var/lib/dpkg/status
$ date
Fri Mar 22 06:54:59 PDT 2019
date --date=2019-03-22 2>&1 | od -c
0000000   d   a   t   e   :       i   n   v   a   l   i   d       d   a
0000020   t   e     342 200 230   2   0   1   9   -   0   3   -   2   2
0000040 342 200 231  \n
0000044

Something weird going on with different timezone in this date: 2019-03-22. I randomly changed timezone to different areas. Some of them have errors, some of them not! When I select these I have problem with that specific date:

  • Los Angeles (USA)
  • Shanghai (China)
  • Madrid (Spain)
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Thomas Ward
    Mar 22, 2019 at 17:10

2 Answers 2

7

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

3
  • Thanks, adding time to date fixed the problem.
    – ICE
    Mar 22, 2019 at 19:45
  • They "change to daylight saving at midnight" should not be a problem for 2019-03-22 as the users TZ is currently PDT. Mar 22, 2019 at 20:12
  • reinstalling tzdata package fixed the problem for me. Now only one timezone has error as you explained in your answer without adding time. I don't know what went wrong with tzdata package but even TZ=America/Toronto date --date="2019-03-10 02:00:00" returned the result without error before reinstalling the tzdata package. thank you for your time.
    – ICE
    Mar 23, 2019 at 0:49
4
$ 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’
2
  • Seems There is something wrong with timezone in my system :(
    – ICE
    Mar 22, 2019 at 15:18
  • 1
    you should reinstall the tzdata package. Mar 22, 2019 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .