Search code examples
linuxdatecalendargnuutc

Adding time to calendar date in Linux gives incorrect result


I am on Linux and I am trying to add a certain number of days, hours and minutes to a specified calendar date using the date tool:

For example:

$ date -d "2013-01-01 + 305 days 12 hours 30 minutes" +"%Y%m%d%0k%M%S"
20131102123000

Splitting the result up so it's easier for us to read:

2013-11-02 12:30:00

This appears to be working correctly. However, when I add 306 days instead of 305:

$ date -d "2013-01-01 + 306 days 12 hours 30 minutes" +"%Y%m%d%0k%M%S"
20131103113000

Split:

2013-11-03 11:30:00

Notice how the hour now shows 11 instead of 12! But I told it to add 12 hours... Let me increment the day one more time:

$ date -d "2013-01-01 + 307 days 12 hours 30 minutes" +"%Y%m%d%0k%M%S"
20131104123000

And split the result again:

2013-11-04 12:30:00

Now the hour goes back to what I expected. I have tested this using date versions 8.12 and 8.4, the same result happens on both. What am I doing wrong?


Solution

  • I was trying to add from a UTC date, but I did not specify this to the tool. This is the solution:

    date --utc -d "2013-01-01 + 307 days 12 hours 30 minutes" +"%Y%m%d%0k%M%S"
    

    Because UTC does not observe DST, there will be no shifts in the hour.