Perl Epoch Converter Routines |
time
my $time = time; # or any other epoch timestamp
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my ($sec, $min, $hour, $day,$month,$year) = (localtime($time))[0,1,2,3,4,5,6];
# You can use 'gmtime' for GMT/UTC dates instead of 'localtime'
print "Unix time ".$time." converts to ".$months[$month]." ".$day.", ".($year+1900);
print " ".$hour.":".$min.":".$sec."\n";
Using the DataTime module:
use DateTime;
$dt = DateTime->from_epoch( epoch => $epoch );
$year = $dt->year;
$month = $dt->month; # 1-12 - also mon
$day = $dt->day; # 1-31 - also day_of_month, mday
$dow = $dt->day_of_week; # 1-7 (Monday is 1) - also dow, wday
$hour = $dt->hour; # 0-23
$minute = $dt->minute; # 0-59 - also min
$second = $dt->second; # 0-61 (leap seconds!) - also sec
$doy = $dt->day_of_year; # 1-366 (leap years) - also doy
$doq = $dt->day_of_quarter; # 1.. - also doq
$qtr = $dt->quarter; # 1-4
$ymd = $dt->ymd; # 1974-11-30
$ymd = $dt->ymd('/'); # 1974/11/30 - also date
$hms = $dt->hms; # 13:30:00
$hms = $dt->hms('|'); # 13!30!00 - also time
use Time::Local; my $time = timelocal($sec,$min,$hours,$day,$month,$year); # replace 'timelocal' with 'timegm' if your input date is GMT/UTC
Using the DataTime module:
use DateTime; $dt = DateTime->new( year => 1974, month => 11, day => 30, hour => 13, minute => 30, second => 0, nanosecond => 500000000, time_zone => 'Asia/Taipei' ); $epoch_time = $dt->epoch;Find more detailed information on CPAN: Time::Local, DateTime. You can also use Date::Manip if you need more advanced date manipulation routines.
The Perl Cookbook, Second Edition
gives detailed information on manipulating dates and times in chapter 3 (pages 90-110).
Back to Epoch Converter Functions