Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: subtracting 10 mins from a given time

Tags:

bash

time

In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?

ie, 90000 -> 85000

like image 769
Roger Moore Avatar asked Sep 09 '10 09:09

Roger Moore


2 Answers

why not just use epoch time and then take 600 off of it?

$ echo "`date +%s` - 600"| bc; date 
1284050588
Thu Sep  9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
like image 197
kSiR Avatar answered Sep 24 '22 03:09

kSiR


This is a bit tricky. Date can do general manipulations, i.e. you can do:

date --date '-10 min'

Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):

date --date '11:45:30 UTC -10 min'

To split your date string, the only way I can think of is substring expansion:

a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"

And if you want to just get back hhmmss:

date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
like image 37
wds Avatar answered Sep 22 '22 03:09

wds