Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone, I cannot determine how to convert a date such as May 11 2001 into the srftime so I can search a text file. The text file has numbers such as 98725300. I tried
$mon = 11; $day = 03; $year = 2001; print time ($fmt,$seconds,$min,$hour,$day,$mon,$year,$wday,$yday,$is +dst);
but of course that does not work. I know this must be simple. Does any one have an idea? thanks, Kev ( kev@kevintaylor.net )

Replies are listed 'Best First'.
Re: Time
by chipmunk (Parson) on Dec 03, 2001 at 09:02 UTC
    It looks like you want to use the Time::Local module, which provides timelocal and timegm methods that convert a date and time back into seconds since the epoch. This module is part of the standard distribution.
      Thanks Chipmunk. I tried:
      use Time::local; $fmt = 1; $sec =2 ; $min =3; $hours =4; $mday = 6; $dmon =5; $mon =6; $year =7; $wkday =8; $Yearday =9; $DST = 10; # use POSIX qw(strftime); print timelocal($sec,$min,$hours,$mday,$mon,$year);
      This also does not work. Am I doing something stupid? Kev
        The name of the module is Time::Local, with the T and the L uppercase. Case is significant in module names. Fix that, and the code should work. You can test it with localtime:
        #!perl -l use Time::Local; $sec = 1; $min = 2; $hours = 3; $mday = 4; $mon = 5; $year = 6; print $time = timelocal($sec,$min,$hours,$mday,$mon,$year); print scalar localtime $time;
        Which prints out:
        1149404521 Sun Jun 4 03:02:01 2006
        timelocal does some guessing on the year; it figured that 6 meant 2006. Also remember that the month starts at 0, so 5 is June.
        Just a tip: for future reference, "does not work" generally "does not help." chipmunk spotted the problem right away, but that may not be as easy in the future without an actual error message or a description of the problem. Glad to see you got your question answered, though.
Re: Converting date to epoch time
by Fastolfe (Vicar) on Dec 04, 2001 at 00:34 UTC
      Hello, Yes I have got a solution (Maybe ;) )... ####CODE#### use Time::Local; #Epoch Decleares $date = `date +%d/%b/%Y:%H:%M:%S`; $epdate = &epoch($date); ($eday, $emon, $eyear, $ehr, $emin, $esec) = &etime($epdate); print "Current Date: $date\nEpoch: $epdate\n"; printf "%2d %3s %4d %02d\:%02d\:%02d\n", $eday, $emon, $eyear, $ehr, $ +emin, $esec, $host; exit; ### Subs sub etime ##Change from Epoch Time to DMYHMS { my $date = @_[0]; my %emonths = ('1',Jan,'2',Feb,'3',Mar,'4',Apr,'5',May,'6',Jun, '7',Jul,'8',Aug,'9',Sep,'10',Oct,'11',Nov,'12',Dec); ($esec, $emin, $ehr, $eday, $emon, $eyear) = localtime($date); $eyear = $eyear+1900; $emon = $emon+1; $ehr = $ehr; #Time Zone Chang +e (Zulu) $emon = $emonths{$emon}; return ($eday, $emon, $eyear, $ehr, $emin, $esec); } sub epoch ##Change from DMYHMS to Epoch Time { my $date = @_[0]; %months = ('Jan',1,'Feb',2,'Mar',3,'Apr',4,'May',5,'Jun',6, 'Jul',7,'Aug',8,'Sep',9,'Oct',10,'Nov',11,'Dec',12); $date =~ tr/\[//d; @dates = split(/\:/, $date); @mons = split(/\//, $dates[0]); $year = $mons[2]; $mon = $mons[1]; $day = $mons[0]; $hr = $dates[1]; $min = $dates[2]; $sec = $dates[3]; my $mon = $months{$mon}; if(!($mon < 1) || ($mon > 12)) { $time = timelocal($sec, $min, $hr, $day, $mon-1, $year-1900); #con +verts to Epoch chomp($time); } return $time; } ####CODE#### ###Output#### Current Date: 16/Dec/2001:22:02:08 Epoch: 1008568928 16 Dec 2001 22:02:08 ###Output#### Hope This Helps, Joe
        This will work great for converting New Zealand time to EST US. Now I need to see if there is a function for day light savings so I wont have to change the code every few months