Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting from one timezone to another

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
Hi there,
Is there an efficent way to convert from one timezone to another or to add or subtract hours in either perl or bash system call?

I have something like (eastern time)
2005-7-30T18:00:00

and I need to say subtract 3 hours and change it to (GMT time)
2005-08-14T13:30:00Z

any input on this will be appriciated.

 
Hi keak,

If you only need to convert between GM time and Local time you can use the gmtime() and localtime() functions; they both take the output of the time() function which gives the number of seconds that have elapsed since some date or other which I can never remember.

If you want to add an hour to a particular time you can do it like this:

$my_time = time();
$my_time = $my_time + (60 * 60);
$my_time = $my_time + (2* (60 * 60)); # or add two hours
$my_time = $my_time - (3* (60 * 60)); # or subtract 3 hours


Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
hopefully you are working with the raw time stamp returned from time(). Mike gave some examples, here is another:

my $time = gmtime(time-3600);

where 3600 is 60 * 60, the number of seconds in an hour.
 
I'm nitpicking here, but EST is GMT - 5 hours. So to get from EST to GMT, you'd need to add 5 hours, not subtract 3.
 
I was being very general in my answer, I assumed the OP could extrapolate from the examples Mike and I provided, but I could also be wrong and your "nitpicking" will help them out if thats the case mate. Team work!
 
Kevin (and Mike too) - your answers are great, my comment was more for the OP. After seeing this:
have something like (eastern time)
2005-7-30T18:00:00

and I need to say subtract 3 hours and change it to (GMT time)
2005-08-14T13:30:00Z
I was concerned that the OP might be subtracting 3 hours and expecting GMT.
 
Maybe the OP is just looking for the GMT equivalent of 3 hours before the time given ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top