Perl: time_formats.pl PDF Print E-mail
Wednesday, 23 September 2009 21:45
#!/usr/bin/perl 
use Time::Local;
use POSIX qw(strftime);
# June 1, 2008, 5:32:11pm and 844 milliseconds
$year = 2008;
$month = 5; # months are numbered starting at 0!
$day = 1;
$hour = 17; # use 24-hour clock for clarity
$min = 32;
$sec = 11;
$msec = 844;

# UNIX Time (Seconds since Jan 1, 1970) 1212355931
$unixtime = timelocal( $sec, $min, $hour, $day, $month, $year );
print "UNIX\t\t\t$unixtime\n";

# populate a few values (wday, yday, isdst) that we'll need for strftime
($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$isdst) = localtime($unixtime);

# YYYYMMDDhhmmss.sss 20080601173211.844
# We use strftime() because it accounts for Perl's zero-based month numbering
$timestring = strftime( "%Y%m%d%H%M%S",
$sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst );
$timestring .= ".$msec";
print "YYYYMMDDhhmmss.sss\t$timestring\n";

# YYMMDDhhmm 0806011732
$timestring = strftime( "%y%m%d%H%M", $sec,$min,$hour,$mday,
$mon,$year,$wday,$yday,$isdst );
print "YYMMDDhhmm\t\t$timestring\n";

# POSIX in "C" Locale Sun Jun 1 17:32:11 2008
$gmtime = localtime($unixtime);
print "POSIX\t\t\t$gmtime\n";