#!/usr/bin/perl -w use strict; use Time::Local; use POSIX qw(strftime); ####### how to get mtime from a file ######### my @Month_text = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my $source_path = "./testfile"; (my $m_time) = (stat($source_path))[9] or die "stat failed"; my ($i_month, $day, $year, $hour, $minutes) = (localtime($m_time))[4,3,5,2,1]; $year += 1900; printf "%s %2d %4d %d:%d\n",$Month_text[$i_month],$day,$year,$hour,$minutes,"\n"; #### parsing a date to put into timegm ###### my $test = "2004-09-03 23:58"; print "starting unparsed string=$test\n"; ( $year, my $month, my $days, my $hours, $minutes) = $test =~ m/\s*(\d+)-(\d+)-(\d+)\s*(\d+):(\d+)/; print "testing parsed vales =$year-$month-$days $hours:$minutes \n"; ######### The key epoch second routine ##### #remember for timegm month[0-11] and year is delta from 1900 my $time = timegm(0,$minutes,$hours,$days,$month-1,$year-1900); print "epoch seconds $time\n"; print "running gmtime() to convert back to components\n"; (my $seconds, $minutes, $hours, $days, $month, $year) = gmtime($time); printf("the printf version=%04d-%02d-%02d %02d:%02d \n", $year+1900, $month+1, $days, $hours, $minutes); #note don't have to adjust as strftime is using C time structure print "now running strftime format".'=%Y-%m-%d %H:%M'."\n"; my $str = strftime( "strfttime version =%Y-%m-%d %H:%M ", 0, $minutes, $hours, $days, $month,$year); print "$str\n"; my $string = gmtime($time); print "string in scalar context from gmtime=$string\n"; __END__ Mar 27 2009 9:30 starting unparsed string=2004-09-03 23:58 testing parsed vales =2004-09-03 23:58 epoch seconds 1094255880 running gmtime() the printf version=2004-09-03 23:58 now running strftime format=%Y-%m-%d %H:%M strfttime version =2004-09-03 23:58 string in scalar context from gmtime=Fri Sep 3 23:58:00 2004