2

I have a time represented as seconds since 1970-01-01 00:00:00 UTC, like 1524884843.

I want to get the time, say, 1 month from the above specified time.

Normally if I want to get the time 1 month from now, I can use...

root@beaglebone:~/bbbrtc# date
Sat Apr 28 03:12:54 UTC 2018
root@beaglebone:~/bbbrtc# date -d "now+1 month"
Mon May 28 03:12:57 UTC 2018

I can also specify an epoch seconds time in the -d argument by prefixing it with an @ as in....

root@beaglebone:~/bbbrtc# date -d "@1524884843"
Sat Apr 28 03:07:23 UTC 2018

However when I try to combine an @ prefixed epoch time with a calculation, I get an error...

root@beaglebone:~/bbbrtc# date -d "@1524884843+1 month"
date: invalid date `@1524884843+1 month'

What is the correct syntax for combining an epoch time with a relative calculation?

1

2 Answers 2

0

You can put the calculation inside parenthesis...

root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
2
  • 1
    No, that does not work.
    – user232326
    Commented Apr 30, 2018 at 7:49
  • That gives the same output as date -d "@1524884843". Commented Apr 30, 2018 at 9:56
0

With ast-open's date (possibly ksh93's builtin date depending on how it was built and the environment):

$ date -d '#1234567890'
Fri Feb 13 23:31:30 GMT 2009
$ date -d "#1234567890 30 days"
Sun Mar 15 23:31:30 GMT 2009

(ast date doesn't support offsets expressed in months (months don't have a fixed length)).

With GNU date, you can use the old method (before @epoch support was added) where you express the epoch time as an offset in seconds relative to 1970-01-01 00:00:00 UTC:

$ date -d '1970-01-01T00:00:00Z + 1234567890 seconds'
Fri 13 Feb 23:31:30 GMT 2009
$ date -d '1970-01-01T00:00:00Z + 1234567890 seconds + 1 month'
Mon 16 Mar 23:31:30 GMT 2009

Note however, that here, 1 month is 31 days, presumably because date considers the 1 month offset is relative to Jan 1970.

If your date is the GNU ones and you would like the behaviour it exhibits for month-relative dates whereby a one-month offset gives you the same day of the month (assuming there's such a day in the next month) as that of the starting date (though not necessarily same hour of the day if there has been some DST change in the interval), then you would probably want to use @isaac's two step approach.

You must log in to answer this question.

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