1

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.

1 Answer 1

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .