CoderZone.org

Category: >> Perl Code >> Convert Unix time string to a date/time group Bookmark and Share

<< lastnext >>

Snippet Name: Convert Unix time string to a date/time group

Description: Handy snippet that converts a Unix timestamp to a date/time group.

Also see:
» Get Unix Timestamp for midnight to...
» UNIX Timestamp conversion

Comment: (none)

Author: CoderZone
Language: PERL
Highlight Mode: PERL
Last Modified: December 03rd, 2010

#!/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;
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 17