Get 60 mins before and after a given timestamp - Shell/ksh
Clash 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'
bash shell date
add a comment |Â
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'
bash shell date
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
add a comment |Â
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'
bash shell date
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'
bash shell date
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
add a comment |Â
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
add a comment |Â
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.
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 doingdate -d@-3600
anddate -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
add a comment |Â
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.
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 doingdate -d@-3600
anddate -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
add a comment |Â
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.
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 doingdate -d@-3600
anddate -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
add a comment |Â
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.
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.
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 doingdate -d@-3600
anddate -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
add a comment |Â
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 doingdate -d@-3600
anddate -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
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%2f444262%2fget-60-mins-before-and-after-a-given-timestamp-shell-ksh%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
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