DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Epoch2gmt

11.06.2009
| 2414 views |
  • submit to reddit
        Convert Unix time string to a date/time group

#!/usr/bin/perl

use warnings;
use strict;

my $time = shift;

die "Usage: $0 [TIME_IN_SECS_SINCE_EPOCH]\n" unless $time;
if ( length($time) > 10 ) {
    $time = substr($time, 0, 10);
}

my @mon  = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @day  = qw( Sun Mon Tue Wed Thu Fri Sat );

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
    = gmtime($time);

printf "%4d-%02d-%02d %02d:%02d:%02d (%s %02d %s %04d)\n",
    $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
    $day[$wday], $mday, $mon[$mon], $year + 1900;