Perl: Converting GMT to EST
By yuli - Posted on February 13th, 2008
Today I encounter the following issue. I had to convert GMT date to EST date format. In general it is 5 hours difference. I didn't find any Perl function to do it automatically. So, I ended up writing the following code:
$now = time();
$now -= 5*60*60;
@timeData = localtime($now);
$now_string = strftime("%b-%d-%Y %H:%M", @timeData);
print $now_string;
Perl time() function returns number of seconds from the Unix epoch time (00:00:00 UTC, January 1, 1970). It is an integer number. Because it is in seconds I can subtract 5 hours from it (5*60*60 seconds) and get the desired value.
After that localtime() Perl function is used to extract date formatted to an array.
