I want to use date
to calculate some future "alarm times" for the realtime clock on my Raspberry Pi. These "alarm times" will be used to "wake" the RPi.
The scheme I decided to use is as follows:
Immediately after booting, I get the current "baseline" time in my chosen format:
$ BASETIME=$(date "+%D %X") $ echo $BASETIME 05/03/21 14:03:43
Before
shutdown
, I need to calculate at least one "wake-up alarm time" from the "baseline" time; e.g. 6 hours from BASETIME. As I understand it, the-d
or--date=
option is used for this purpose, but I am not getting the correct result:$ date '+%D %X' -d "${BASETIME} + 6 hours" 05/03/21 04:03:43
The result is a time in the past!? - which is obviously incorrect (at least not what I wanted :) A thorough read of
man date
sheds no light; it suggests:The date string format is more complex than is easily documented here...
and
Full documentation [...] available locally via:
info '(coreutils) date invocation'
But I found nothing useful here either.
An alternative that comes close is this:
$ date "+%D %X" -d '+ 6 hours' 05/03/21 10:10:11
I can get the correct alarm time in this fashion, but this is relative to the current time, instead of the BASETIME.
My questions are:
Why does
date '+%D %X' -d "${BASETIME} + 6 hours"
decrement time rather than increment time?What is the correct way to increment from a fixed BASETIME?
NOTE: FWIW, my OS, kernel, and date
versions:
$ date --version
date (GNU coreutils) 8.30
...
$ hostnamectl
...
Operating System: Raspbian GNU/Linux 10 (buster)
Kernel: Linux 5.10.17-v7+
...
-
(minus) interpretation of GNU date differs from the intuitive one, when a date is specified?+/- N
from being parsed as a timezone?