Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date: invalid date ‘2018-10-21 +1 day’

I'm getting a weird behavior when I ask date for the day after 2018-10-21:

date --date "2018-10-20 +1 day" +"%Y-%m-%d"    # OK, 2018-10-21
date --date "2018-10-21 +1 day" +"%Y-%m-%d"    # invalid date, see below
date --date "2018-10-22 +1 day" +"%Y-%m-%d"    # OK, 2018-10-23

The exact error-message is:

date: invalid date ‘2018-10-21 +1 day’

Why is this happening?


In case it's relevant . . .

Here's the output of lsb_release -a:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:    14.04
Codename:   trusty

(and I get the same behavior on Ubuntu 16.04.4 LTS).

Here's the output of locale:

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

Here's the output of date "+%z %Z":

-0300 BRT

Here's the output of date --version:

date (GNU coreutils) 8.21

(and I get the same behavior on date (GNU coreutils) 8.25).

like image 776
Rod Elias Avatar asked Jan 27 '23 20:01

Rod Elias


1 Answers

In Brasil, Summer Time starts at midnight. That's different from most parts of the world, where the change is at 2:00 a.m., precisely because of the confusion which gives rise to this question.


NOTE: The following represents the information in the time zone file currently installed on my machine, and presumably that of the OP. However, in December, 2017, the government of Brasil decided to delay Summer Time by two weeks this year, so the transition will actually be on November 4, 2018. Hopefully, the TZ database will be updated before then.

Not all Brasilian states change their clocks twice a year. In at least one state (Matto Grosso), the decision is made by individual municipalities.


So in Sāo Paulo, there will be no 00:00:00 on October 21, 2018. When the clock ticks over a second from 23:59:59 on October 20, 2018, Summer Time will kick in and Sunday will start at 1 a.m.:

$ TZ=America/Sao_Paulo date -d "23:59:59 2018-10-20"
Sat Oct 20 23:59:59 -03 2018

$ TZ=America/Sao_Paulo date -d "23:59:59 2018-10-20 +1 second"
Sun Oct 21 01:00:00 -02 2018

Note the change in TZ offset.

When you just type a date without a time, the time defaults to 0:00:00. If that time doesn't exist on a given day, date complains:

$ TZ=America/Sao_Paulo date -d "2018-10-21"
date: invalid date ‘2018-10-21’

$ TZ=America/Sao_Paulo date -d "00:00:00 2018-10-21"
date: invalid date ‘00:00:00 2018-10-21’

$ TZ=America/Sao_Paulo date -d "00:30:00 2018-10-21"
date: invalid date ‘00:30:00 2018-10-21’
like image 185
rici Avatar answered Jan 30 '23 09:01

rici