Syntax for using epoch times in a calculation with the `--date=STRING` usage of the `date` command

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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?
command-line date
add a comment |Â
up vote
2
down vote
favorite
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?
command-line date
1
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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?
command-line date
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?
command-line date
asked Apr 29 at 17:51
bigjosh
25439
25439
1
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58
add a comment |Â
1
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58
1
1
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
You can put the calculation inside parenthesis...
root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
1
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output asdate -d "@1524884843".
â Stéphane Chazelas
Apr 30 at 9:56
add a comment |Â
up vote
0
down vote
The only way is to use a two step conversion:
$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can put the calculation inside parenthesis...
root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
1
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output asdate -d "@1524884843".
â Stéphane Chazelas
Apr 30 at 9:56
add a comment |Â
up vote
0
down vote
You can put the calculation inside parenthesis...
root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
1
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output asdate -d "@1524884843".
â Stéphane Chazelas
Apr 30 at 9:56
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can put the calculation inside parenthesis...
root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
You can put the calculation inside parenthesis...
root@beaglebone:~/bbbrtc# date -d "@1524884843+(1 month)"
Sat Apr 28 03:07:23 UTC 2018
answered Apr 29 at 17:51
bigjosh
25439
25439
1
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output asdate -d "@1524884843".
â Stéphane Chazelas
Apr 30 at 9:56
add a comment |Â
1
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output asdate -d "@1524884843".
â Stéphane Chazelas
Apr 30 at 9:56
1
1
No, that does not work.
â Isaac
Apr 30 at 7:49
No, that does not work.
â Isaac
Apr 30 at 7:49
That gives the same output as
date -d "@1524884843".â Stéphane Chazelas
Apr 30 at 9:56
That gives the same output as
date -d "@1524884843".â Stéphane Chazelas
Apr 30 at 9:56
add a comment |Â
up vote
0
down vote
The only way is to use a two step conversion:
$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018
add a comment |Â
up vote
0
down vote
The only way is to use a two step conversion:
$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The only way is to use a two step conversion:
$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018
The only way is to use a two step conversion:
$ date -d "$(date -d "@1524884843") + 1 month"
Mon May 28 03:07:23 UTC 2018
answered Apr 30 at 8:07
Isaac
6,4161633
6,4161633
add a comment |Â
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Apr 30 at 10:03
answered Apr 30 at 9:55
Stéphane Chazelas
279k53514846
279k53514846
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440771%2fsyntax-for-using-epoch-times-in-a-calculation-with-the-date-string-usage-of%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Of course, per unix.stackexchange.com/questions/422904 , there are other pitfalls to look out for here.
â JdeBP
Apr 30 at 6:58