Perl: Converting GMT to EST

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.

Looks like you haven't paid a visit to CPAN.

A wonderful module, "DateTime::TimeZone" will do just that, and much more.
It will take Daylight savings into account, it is an object and hence flexible (and depends on DateTime module).

Very handy module. Taking daylight into account is very important and often neglected.

Until next time...