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

Hi, Requirement - I have file name called "Rajesh.1202242219". Numbers are nothing but a date "`date '+%y''%m''%d''%H''%M'`" format. Now I am trying to write a perl script to extract the numbers from file name and compare with current system date and time and based on output of this comparison, print some value using perl. Approach: Extract the Digit from File name:
if ($file =~ /Rajesh.(\d+).*/) { print $1; }
Convert this time into readable time in perl
my $sec = 0; # Not Feeded my $min = 19; my $hour = 22; my $day = 24; my $mon = 02 - 1; my $year = 2012 - 1900; my $wday = 0; # Not Feeded my $yday = 0; # Not Feeded my $unixtime = mktime ($sec, $min, $hour, $day, $mon, $year, $wday, $y +day); print "$unixtime\n"; my $readable_time = localtime($unixtime); print "$readable_time\n";
find Current time and compare...
my $CurrentTime = time(); my $Todaydate = localtime($startTime);
But the problem here is, I am not getting solution of how to extract 2 digit from $1 and assign to $sec, $min, etc... Any help????? Also, if you have good approach for this problem statement, Please share with me

Replies are listed 'Best First'.
Re: Convert Old Unix Date to Perl and compare
by JavaFan (Canon) on Feb 27, 2012 at 12:40 UTC
    Untested:
    if (my($year, $mon, $day, $hour, $min) = $file =~ /Rajesh\.([0-9]{2})( +[0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/) { my $unixtime = mktime(0, $min, $hour, $mon - 1, $year + 100, 0, 0) +; ... }
      shouldn't it be timelocal() from Time::Local instead of mktime()? Or you could use POSIX qw(mktime) instead, but POSIX is a lot to bring in just for this.
        The OP was using mktime, indicating his problem was extracting the pieces of data from the filename, not with the construction of the time. Who am I to second guess him?
      In this case, $year is already in "year-1900" format so that value can be used as is.

      I'm assuming you meant +1900 and not +100 since that makes no sense at all?

      Edit: Nevermind me. I just woke up.

      -- Time flies when you don't know what you're doing
Re: Convert Old Unix Date to Perl and compare
by TomDLux (Vicar) on Feb 27, 2012 at 19:39 UTC

    I use unpack ...

    my $file = 'Rajesh.1202242219'; my ($name, $datetime) = split /\./, $file; my ( $y, $m, $d, $hr, $min ) = unpack 'A2A2A2A2A2', $datetime; print "( $y, $m, $d, $hr, $min )"; # returns .. ( 12, 02, 24, 22, 19 )

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Convert Old Unix Date to Perl and compare
by Anonymous Monk on Feb 27, 2012 at 21:41 UTC
    There are two parts to this problem: (1) splitting the multi-digit string into component parts, as thoroughly shown; and (2) dealing with the date from that point on. For the latter, I go straight to packages like Date::Manip. What these modules do is to treat "a date" as "an opaque software object." You create one using whatever information you have, then you manipulate it however you need to do, then you get the result out in whatever printable format you need. You do not know how it represents its data; you do not care.