Analysis
I made some tests in Debian 9, studied man 1 date
and info date
. The latter contains more information about how strings like 2019-10-19 01:37:02 +7 days
are interpreted.
Preliminary notes:
- My answer uses
date
because this is the tool in Debian. (Use alias date=gdate
to test my commands with gdate
without editing them each time).
- It seems your timezone is roughly equivalent to
TZ=UTC+4
.
I tried to replicate your result:
$ date -d '2019-10-19 01:37:02 +7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-19 20:37:02
My result is different, this indicates timezones matter. To really replicate your result:
$ TZ=UTC+4 date -d '2019-10-19 01:37:02 +7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-19 14:37:02
Explanation
It turns out +7
in your "misbehaving" command is interpreted as UTC+7
and days
is interpreted as +1 days
. Compare:
$ TZ=UTC+4 date -d '2019-10-19 01:37:02 UTC+7 +1 days' +"%Y-%m-%d %H:%M:%S"
2019-10-19 14:37:02
(Note TZ=UTC+4
means UTC-04:00 while UTC+7
in the string means UTC+07:00. The inconsistency with signs is explained here.)
The mysterious result is not so mysterious now.
Quirk
What is really weird is this:
$ TZ=UTC+4 date -d '2019-10-19 01:37:02 UTC+7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-25 21:37:02
Apparently it's not UTC+7
and days
(meaning +1 days
as above) but rather UTC
(meaning UTC+0
) and +7 days
:
$ TZ=UTC+4 date -d '2019-10-19 01:37:02 UTC+0 +7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-25 21:37:02
Solution
Move the +7 days
substring towards the beginning, so it appears before 01:37:02
. This way +7
wont be interpreted as UTC+7
:
$ date -d '2019-10-19 +7 days 01:37:02' +"%Y-%m-%d %H:%M:%S"
2019-10-26 01:37:02
Now 01:37:02
in the input string is interpreted according to whatever timezone date
considers right. The result* should be the same for me in Europe, fellow users in America, Australia, anywhere. This is why I dropped TZ=UTC+4
, it doesn't matter anymore.
*I mean the string of characters printed by date
should be the same. Different users will interpret the same string in different timezones and they will get different moments in reality. In this sense their results are different. In the same sense their inputs are different.
3
gdate -d '2019-10-19 +7 days 01:37:02' +"%Y-%m-%d %H:%M:%S"
? – Kamil Maciorowski – 2019-11-05T16:05:16.263You're my hero, if you write up that answer I'll give the check – sedavidw – 2019-11-05T16:23:52.557