Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unix shell script to get date 30 minutes ago in GMT

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.

like image 916
Joelio Avatar asked Jan 08 '10 18:01

Joelio


People also ask

How do you echo time in shell script?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

How do you change date and time in Unix?

The date command under UNIX displays date and time. You can use the same command set date and time. You must be the super-user (root) to change the date and time on Unix like operating systems. The date command shows the date and time read from the kernel clock.


2 Answers

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

like image 56
malonso Avatar answered Sep 19 '22 09:09

malonso


You can also do this:

TZ='UTC+0:30' date
like image 26
Dennis Williamson Avatar answered Sep 21 '22 09:09

Dennis Williamson