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?
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.
Comments