views:

314

answers:

3

Hi, I have a shell script that I want to get the date and time 30 minutes ago in GMT.

I have this working great for full hours, but partial hours don't seem to work:

1 hour ago

TZ=GMT+1 date +%Y-%m-%d" "%H:%M:%S 2010-01-08 17:43:57

2 hours ago

TZ=GMT+2 date +%Y-%m-%d" "%H:%M:%S 2010-01-08 16:44:07

1/2 hour ago

TZ=GMT+.5 date +%Y-%m-%d" "%H:%M:%S 2010-01-08 18:44:38

tried lots of combinations of 0.5 1.5, no partial hours seem to work, which is weird because there are some timezones that are not full offset of an hour.

any suggestions?

cant use perl or ruby needs to be regular shell or mysql call.

+3  A: 

date -u --date="-30 minutes"

malonso
In case you are on OS X, the equivalent option is: date -u -v-30M
Max
thanks, would be ideal if I had one that worked on both linux centos and osx, but I can live with 2 versions for now.
Joelio
As well as "-30 minutes", you can say "30 minutes ago", which matches that part of your question exactly.
camh
A: 
 /usr/bin/env TZ='GMT' date -d '-30 minutes'

This is with the version of the date command that's part of the GNU coreutils. I don't know if it works for other versions of the date program.

Noufal Ibrahim
this one didnt work on osx or centos for me.
Joelio
Can you post the error?
Noufal Ibrahim
+2  A: 

You can also do this:

TZ='UTC-0:30' date
Dennis Williamson
Works on Mac OS X and Ubuntu.
trashgod