Bash Date WakeUp

Viewed 59

Since months I used this command in a script on my NAS:

sudo rtcwake -m disk -t  "$(date -d 'tomorrow  02:00:00' '+%s')"

Today I got an error.

My test results:

root@omvnas:~# date -d 'tomorrow  02:00:00' '+%s'
date: ungültiges Datum „tomorrow  02:00:00“
root@omvnas:~# date -d '+1 day 3:00'
Mo 30. Mär 03:00:00 CEST 2020
root@omvnas:~# date -d '+1 day 2:00'
date: ungültiges Datum „+1 day 2:00“
root@omvnas:~# date -d '+1 day 1:00'
Mo 30. Mär 01:00:00 CEST 2020

"ungültiges Datum" is German for "invalid date".

Why is this happening ?

2 Answers

This issue is indeed related to the daylight saving time, but this does not appear to be a bug of the date program, rather a "feature": all the commands below raise an error, because the specified time does not exist in the local timezone (which directly goes from 01:59:59 to 03:00:00).

$ date -d '02:00:00'
date: invalid date ‘02:00:00’
$ date -d '02:30:00'
date: invalid date ‘02:30:00’
$ date -d '+1 day 02:30:00'
date: invalid date ‘+1 day 02:30:00’
$ date -d '+2 day 02:30:00'
date: invalid date ‘+2 day 02:30:00’

and the last two commands above certainly raise an error because the date program attempts to increment the number of days after resolving the specified time, which is indeed invalid.

As I mentioned in my comment, a reliable way to workaround this issue could be to rely on the UTC timezone (option --utc):

$ date -d 'today 01:59:59' -R
Sun, 29 Mar 2020 01:59:59 +0100
$ date -d 'today 01:59:59' '+%s'
1585443599
$ date -d 'today 02:00:00' '+%s'
date: invalid date ‘today 02:00:00’
$ date -d 'today 03:00:00' '+%s'
1585443600
$ date -u -d 'today 01:00:00' '+%s'  # OK thanks to option -u
1585443600
Related