Why does having the time in GNU date break "- 1 day"?

Matthew Herbst

This converts the given datetime to epoch time (seconds)

date -d"2015-07-24 11:29:00" +%s
// gives 1437762540

Now I want to do the same thing, but subtract a day. Normally, this is as simple as adding "- 1 day". However, instead of subtracting a day, it actually adds a day.

date -d"2015-07-24 11:29:00 - 1 day" +%s
// gives 1437848940 (notice, this value is great than the one above)

If I take away the time portion from my timestamp it works great. The time portion seems to break it however. I know I can do this in two separate steps and avoid this problem. However, I was hoping to do it in one command. Is this possible?

mob

There is some ambiguity in the date command about how to interpret the - 1 token in your date string. It resolves it as a time zone specification

$ date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 08:29:00 EDT 2015

$ date -d "2015-07-24 11:29:00 UTC-1"
Fri Jul 24 08:29:00 EDT 2015

$ date -d "2015-07-24 11:29:00 - 2"
Fri Jul 24 09:29:00 EDT 2015

$ date -d "2015-07-24 11:29:00 UTC-2"
Fri Jul 24 09:29:00 EDT 2015

$ TZ=UTC date -d "2015-07-24 11:29:00 -1"
Fri Jul 24 12:29:00 UTC 2015

(your results may vary depending on your TZ setting)

The day part is then interpreted to mean add one day

$ date -d "2015-07-24 11:29:00 - 1 day"
Sat Jul 25 12:29:00 EDT 2015

$ date -d "2015-07-24 11:29:00 UTC-1 + 1 day"
Sat Jul 25 12:29:00 EDT 2015

Add a timezone spec to your date string, as @amdixon suggests, to resolve the ambiguity and get the expected results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

adding 1 day to date and time

From Dev

why firefox does not break the date?

From Dev

Increment date in row by 1 day each time

From Dev

Why is new Date() subtracting 1 day in Javascript?

From Dev

Increment the date by 1 day by comparing the current time and given time

From Dev

Add 1 year to Date and minus 1 Day at the same time

From Dev

Getting offset by 1 day when converting NTP time to date

From Dev

Why does Javascript's Date() function break for my birthday in Safari?

From Dev

Why does Javascript's Date() function break for my birthday in Safari?

From Dev

Why does datediff return 1 day less than the actual difference?

From Dev

Add 1 Day to a Date

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

From Dev

Having problems with date and time editing

From Dev

Having problems with date and time editing

From Dev

Why is `time` not in the GNU Bash manual?

From Dev

Why bash time is more precise then GNU time?

From Dev

Why does GNU say it is "Not Unix"?

From Dev

H2 Get Date 1 day old from current time

From Dev

Why does FakeFS break RSpec?

From Dev

Why does IgnorePatternWhitespace break regex?

From Dev

Why does this function break in Powershell?

From Dev

Why does this break UILabel adjustsFontSizeToFitWidth?

From Dev

Why does jQuery break this code?

From Dev

Remove 1 day from date

From Dev

Wrong calculation on date "-1 Day"

From Dev

Why does `date` show UTC time, even with a different timezone set?

From Dev

Why does my date output have "STD" as the time zone?

From Dev

Adding 1 day to my time

Related Related

  1. 1

    adding 1 day to date and time

  2. 2

    why firefox does not break the date?

  3. 3

    Increment date in row by 1 day each time

  4. 4

    Why is new Date() subtracting 1 day in Javascript?

  5. 5

    Increment the date by 1 day by comparing the current time and given time

  6. 6

    Add 1 year to Date and minus 1 Day at the same time

  7. 7

    Getting offset by 1 day when converting NTP time to date

  8. 8

    Why does Javascript's Date() function break for my birthday in Safari?

  9. 9

    Why does Javascript's Date() function break for my birthday in Safari?

  10. 10

    Why does datediff return 1 day less than the actual difference?

  11. 11

    Add 1 Day to a Date

  12. 12

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  13. 13

    Why does the `-` (minus) interpretation of GNU date differs from the intuitive one, when a date is specified?

  14. 14

    Having problems with date and time editing

  15. 15

    Having problems with date and time editing

  16. 16

    Why is `time` not in the GNU Bash manual?

  17. 17

    Why bash time is more precise then GNU time?

  18. 18

    Why does GNU say it is "Not Unix"?

  19. 19

    H2 Get Date 1 day old from current time

  20. 20

    Why does FakeFS break RSpec?

  21. 21

    Why does IgnorePatternWhitespace break regex?

  22. 22

    Why does this function break in Powershell?

  23. 23

    Why does this break UILabel adjustsFontSizeToFitWidth?

  24. 24

    Why does jQuery break this code?

  25. 25

    Remove 1 day from date

  26. 26

    Wrong calculation on date "-1 Day"

  27. 27

    Why does `date` show UTC time, even with a different timezone set?

  28. 28

    Why does my date output have "STD" as the time zone?

  29. 29

    Adding 1 day to my time

HotTag

Archive