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 - Convert Seconds To Time...

  • submit to reddit
// Convert seconds since the epoch to a normal time string...
// You can do:

# $ date +'%s'

// to generate seconds since the epoch... or of course:

$ perl -e 'print time, $/;'

// The program...


use warnings;
use strict;

my $time = shift;

die "Usage: $0 [TIME_IN_SECS_SINCE_EPOCH]\n" unless $time;

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;