tags:

views:

45

answers:

3

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

+2  A: 

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"
wds
Thanks for the reply. So to clarify...the time is not the present time...it is a five or six digit number passed into the script as a parameter...would this approach work for this?
Roger Moore
@RogerMoore yes, that's why I use --date. It has to be written as "hh:mm:ss UTC" because otherwise date won't be able to misinterpret it, but it seemed to work for your purpose. I'm not sure what happens when you pass in a time in the form of hh:mm:s, you best test it, also around midnight and noon.
wds
A: 

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

-(~)------------------------------------------------------------------------------------------------------------------------------------------------(11:51 Thu Sep 09)
risk@DockMaster [2102] --> echo "`date +%s` - 600"| bc; date 
1284050588
Thu Sep  9 11:53:08 CDT 2010
-(~)------------------------------------------------------------------------------------------------------------------------------------------------(11:53 Thu Sep 09)
risk@DockMaster [2103] --> date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
-(~)------------------------------------------------------------------------------------------------------------------------------------------------(11:53 Thu Sep 09)
risk@DockMaster [2104] --> 
kSiR
Thanks for the reply. The thing is I don't require to do it based on the present time. It is an arbitrary time will be passed into the script as a five or six digit number.
Roger Moore
Usually it's a good idea to strip your own styling/features and use a conventional prompt like "$" when posting. (I set `PS1='\$ '` before answering these questions so I can still copy-paste.)
Roger Pate
A: 

Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:

$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100

Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.

For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.

Roger Pate