Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to do date computations in a shell script using gnu date with timestamp phrases and fail to explain these differences. Can someone enlighten me? Is this a bug?

% date -u -d "now"
Fri Aug  2 15:07:16 UTC 2013

% date -u -d "1970-01-01 00:00:00 UTC  + `date +%s` seconds"
Fri Aug  2 15:07:29 UTC 2013

% date -u -d "now -6 months -1 day"
Fri Feb  1 15:07:52 UTC 2013

% date -u -d "1970-01-01 00:00:00 UTC  + `date +%s` seconds -6 months -1 day"
Tue Jan 29 15:08:03 UTC 2013

I expected the last one to mean exactly the same as the the one before since %s is defined as "seconds since 1970-01-01 00:00:00 UTC" in the manpage.

share|improve this question
add comment

1 Answer

Seems like it's an order of operation error. If I do this command it works fine:

$ date -u -d "$(date -u -d "1970-01-01 00:00:00 UTC  + `date +%s` seconds") \
     -6 months -1 day"

Running this gives strange results:

$ date -u -d "$(date +%s) seconds -6 months -1 day"
Sun Sep  3 07:48:54 UTC 2056

But protecting the $(date +%s) seconds with parens seems to resolve the issue:

$ date -u -d "($(date +%s) seconds) -6 months -1 day"
Fri Feb  1 15:55:12 UTC 2013
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.