Strange behavior of bash date and time arithmetic

When I issue the bash command:

date --date="2018-03-03 12:16:13 -1hour" "+%Y:%m:%d %H:%M:%S"

I expect the result to be:

2018:03:03 11:16:13

but instead I get:

2018:03:03 15:16:13

I wonder if this needs to be done with time zones and how to avoid this behavior.

+4
source share
2 answers

From info coreutils 'date invocation'

When a relative element causes the resulting date to cross the border where the clock has been adjusted, usually for daylight saving time, the corresponding date and time are adjusted accordingly.

Fuzz . , "2003-07-31 -1 " 2003-07-01, 2003-06-31 - . , 15- . :

 $ date -R
 Thu, 31 Jul 2003 13:02:39 -0700
 $ date --date='-1 month' +'Last month was %B?'
 Last month was July?
 $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
 Last month was June!

, . 24 , , "TZ" 'UTC0', .

, -1 hour ,

$ date --date='-1 hour 2018-03-03 12:16:13' "+%Y:%m:%d %H:%M:%S"
2018:03:03 11:16:13
+1

. America/New_York

$ date --date="2018-03-03 12:16:13 - 1 hour" "+%Y:%m:%d %H:%M:%S"
2018:03:03 09:16:13
$ env TZ='Europe/Belgrade' date --date="2018-03-03 12:16:13 - 1 hour" "+%Y:%m:%d %H:%M:%S"
2018:03:03 15:16:13

, , -1 GMT + 01: 00, .

, , :

$ date --date="- 1 hour 2018-03-03 12:16:13" "+%Y:%m:%d %H:%M:%S"
2018:03:03 11:16:13
+5

Source: https://habr.com/ru/post/1694777/


All Articles