Get 60 mins before and after a given timestamp - Shell/ksh

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am looking to get a time range from a given time. The start time should be 60
mins before the given time and end time should be 60 mins after the given time.



Purpose:
The given time will be like an 'impact time', I am looking to get a suitable range of time around the impact time to be able to grab logs.



Example :
Given time is 2018-05-16 20:30:00
This is of the format '%Y-%m-%d %H:%M:%S'



Please help me get:
Start time is 2018-05-16 19:30:00
End time is 2018-05-16 21:30:00



This is going to run on a server that will be in PDT/PST always and the date-time is of the format '%Y-%m-%d %H:%M:%S'







share|improve this question

















  • 1




    Timezones have an effect on time, I’m afraid...
    – Jeff Schaller
    May 16 at 23:43










  • @JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
    – Sierra
    May 16 at 23:56










  • Transitions between PDT and PST affect the time, too.
    – Jeff Schaller
    May 17 at 0:02














up vote
0
down vote

favorite












I am looking to get a time range from a given time. The start time should be 60
mins before the given time and end time should be 60 mins after the given time.



Purpose:
The given time will be like an 'impact time', I am looking to get a suitable range of time around the impact time to be able to grab logs.



Example :
Given time is 2018-05-16 20:30:00
This is of the format '%Y-%m-%d %H:%M:%S'



Please help me get:
Start time is 2018-05-16 19:30:00
End time is 2018-05-16 21:30:00



This is going to run on a server that will be in PDT/PST always and the date-time is of the format '%Y-%m-%d %H:%M:%S'







share|improve this question

















  • 1




    Timezones have an effect on time, I’m afraid...
    – Jeff Schaller
    May 16 at 23:43










  • @JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
    – Sierra
    May 16 at 23:56










  • Transitions between PDT and PST affect the time, too.
    – Jeff Schaller
    May 17 at 0:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am looking to get a time range from a given time. The start time should be 60
mins before the given time and end time should be 60 mins after the given time.



Purpose:
The given time will be like an 'impact time', I am looking to get a suitable range of time around the impact time to be able to grab logs.



Example :
Given time is 2018-05-16 20:30:00
This is of the format '%Y-%m-%d %H:%M:%S'



Please help me get:
Start time is 2018-05-16 19:30:00
End time is 2018-05-16 21:30:00



This is going to run on a server that will be in PDT/PST always and the date-time is of the format '%Y-%m-%d %H:%M:%S'







share|improve this question













I am looking to get a time range from a given time. The start time should be 60
mins before the given time and end time should be 60 mins after the given time.



Purpose:
The given time will be like an 'impact time', I am looking to get a suitable range of time around the impact time to be able to grab logs.



Example :
Given time is 2018-05-16 20:30:00
This is of the format '%Y-%m-%d %H:%M:%S'



Please help me get:
Start time is 2018-05-16 19:30:00
End time is 2018-05-16 21:30:00



This is going to run on a server that will be in PDT/PST always and the date-time is of the format '%Y-%m-%d %H:%M:%S'









share|improve this question












share|improve this question




share|improve this question








edited May 16 at 23:50
























asked May 16 at 23:40









Sierra

103




103







  • 1




    Timezones have an effect on time, I’m afraid...
    – Jeff Schaller
    May 16 at 23:43










  • @JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
    – Sierra
    May 16 at 23:56










  • Transitions between PDT and PST affect the time, too.
    – Jeff Schaller
    May 17 at 0:02












  • 1




    Timezones have an effect on time, I’m afraid...
    – Jeff Schaller
    May 16 at 23:43










  • @JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
    – Sierra
    May 16 at 23:56










  • Transitions between PDT and PST affect the time, too.
    – Jeff Schaller
    May 17 at 0:02







1




1




Timezones have an effect on time, I’m afraid...
– Jeff Schaller
May 16 at 23:43




Timezones have an effect on time, I’m afraid...
– Jeff Schaller
May 16 at 23:43












@JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
– Sierra
May 16 at 23:56




@JeffSchaller Sorry for the confusion , This script is going to run on a server that will be in PDT/PST always
– Sierra
May 16 at 23:56












Transitions between PDT and PST affect the time, too.
– Jeff Schaller
May 17 at 0:02




Transitions between PDT and PST affect the time, too.
– Jeff Schaller
May 17 at 0:02










1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Assuming the timezone is set correctly, and GNU date, you can get the Unix timestamp, subtract, add 60 minutes and convert it back to the format needed:



$ ts=$(date -d "2018-05-16 21:30:00" +%s) # For BSD date: date -jf "%F %T" "2018-05-16 21:30:00" +%s 
$ echo "$(date -d@$((ts - 3600)) +"%F %T")"
2018-05-16 20:30:00
$ echo "$(date -d@$((ts + 3600)) +"%F %T")"
2018-05-16 22:30:00


... I think this doesn't correctly account for leap seconds and various other problems every programmer should know about time.






share|improve this answer





















  • This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
    – Sierra
    May 17 at 18:22










  • Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
    – Olorin
    May 18 at 6:16










  • Oops sorry,, this works !! Thank you
    – Sierra
    May 21 at 2:47










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444262%2fget-60-mins-before-and-after-a-given-timestamp-shell-ksh%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Assuming the timezone is set correctly, and GNU date, you can get the Unix timestamp, subtract, add 60 minutes and convert it back to the format needed:



$ ts=$(date -d "2018-05-16 21:30:00" +%s) # For BSD date: date -jf "%F %T" "2018-05-16 21:30:00" +%s 
$ echo "$(date -d@$((ts - 3600)) +"%F %T")"
2018-05-16 20:30:00
$ echo "$(date -d@$((ts + 3600)) +"%F %T")"
2018-05-16 22:30:00


... I think this doesn't correctly account for leap seconds and various other problems every programmer should know about time.






share|improve this answer





















  • This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
    – Sierra
    May 17 at 18:22










  • Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
    – Olorin
    May 18 at 6:16










  • Oops sorry,, this works !! Thank you
    – Sierra
    May 21 at 2:47














up vote
0
down vote



accepted










Assuming the timezone is set correctly, and GNU date, you can get the Unix timestamp, subtract, add 60 minutes and convert it back to the format needed:



$ ts=$(date -d "2018-05-16 21:30:00" +%s) # For BSD date: date -jf "%F %T" "2018-05-16 21:30:00" +%s 
$ echo "$(date -d@$((ts - 3600)) +"%F %T")"
2018-05-16 20:30:00
$ echo "$(date -d@$((ts + 3600)) +"%F %T")"
2018-05-16 22:30:00


... I think this doesn't correctly account for leap seconds and various other problems every programmer should know about time.






share|improve this answer





















  • This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
    – Sierra
    May 17 at 18:22










  • Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
    – Olorin
    May 18 at 6:16










  • Oops sorry,, this works !! Thank you
    – Sierra
    May 21 at 2:47












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Assuming the timezone is set correctly, and GNU date, you can get the Unix timestamp, subtract, add 60 minutes and convert it back to the format needed:



$ ts=$(date -d "2018-05-16 21:30:00" +%s) # For BSD date: date -jf "%F %T" "2018-05-16 21:30:00" +%s 
$ echo "$(date -d@$((ts - 3600)) +"%F %T")"
2018-05-16 20:30:00
$ echo "$(date -d@$((ts + 3600)) +"%F %T")"
2018-05-16 22:30:00


... I think this doesn't correctly account for leap seconds and various other problems every programmer should know about time.






share|improve this answer













Assuming the timezone is set correctly, and GNU date, you can get the Unix timestamp, subtract, add 60 minutes and convert it back to the format needed:



$ ts=$(date -d "2018-05-16 21:30:00" +%s) # For BSD date: date -jf "%F %T" "2018-05-16 21:30:00" +%s 
$ echo "$(date -d@$((ts - 3600)) +"%F %T")"
2018-05-16 20:30:00
$ echo "$(date -d@$((ts + 3600)) +"%F %T")"
2018-05-16 22:30:00


... I think this doesn't correctly account for leap seconds and various other problems every programmer should know about time.







share|improve this answer













share|improve this answer



share|improve this answer











answered May 17 at 1:51









Olorin

1,15711




1,15711











  • This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
    – Sierra
    May 17 at 18:22










  • Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
    – Olorin
    May 18 at 6:16










  • Oops sorry,, this works !! Thank you
    – Sierra
    May 21 at 2:47
















  • This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
    – Sierra
    May 17 at 18:22










  • Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
    – Olorin
    May 18 at 6:16










  • Oops sorry,, this works !! Thank you
    – Sierra
    May 21 at 2:47















This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
– Sierra
May 17 at 18:22




This does not seem to work, I'm getting some weird year like 1972 ! does this command depend on the linux version ? Mine is Linux meade 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linux and the output i got was : + date -d@-3600 '+%F %T' + sBegindate='1969-12-31 15:00:00' + date -d@3600 '+%F %T' + sEnddate='1969-12-31 17:00:00' + print ' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00' Start date 1969-12-31 15:00:00 and End date 1969-12-31 17:00:00
– Sierra
May 17 at 18:22












Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
– Olorin
May 18 at 6:16




Uh, why are you doing date -d@-3600 and date -d@3600? 3600 seconds before and after the Unix epoch would of course be around 1970.
– Olorin
May 18 at 6:16












Oops sorry,, this works !! Thank you
– Sierra
May 21 at 2:47




Oops sorry,, this works !! Thank you
– Sierra
May 21 at 2:47












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444262%2fget-60-mins-before-and-after-a-given-timestamp-shell-ksh%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay