Date math not giving expected result

0

0

I've seen several examples (including this post) and it seems simple enough but I don't quite get what I'm doing wrong (I know that post is for linux but I tried w/ the date command on a linux machine and go the same result there)

An example command and ouput

me@mymachine~$ gdate -d '2019-10-19 01:37:02 +7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-19 14:37:02

I would expected the result to start with 2019-10-26. So it seems that it's not parsing my input right?

What's even weirder (to me anyway), is if I take off the time part of the input it works as expected

me@mymachine~$ gdate -d '2019-10-19 +7 days' +"%Y-%m-%d %H:%M:%S"
2019-10-26 00:00:00

sedavidw

Posted 2019-11-05T16:01:27.633

Reputation: 101

3gdate -d '2019-10-19 +7 days 01:37:02' +"%Y-%m-%d %H:%M:%S"? – Kamil Maciorowski – 2019-11-05T16:05:16.263

You're my hero, if you write up that answer I'll give the check – sedavidw – 2019-11-05T16:23:52.557

Answers

0

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.

Kamil Maciorowski

Posted 2019-11-05T16:01:27.633

Reputation: 38 429