Convert Old Unix Date to Perl and compare

2019-07-13 11:55发布

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, $yday);
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

5条回答
叛逆
2楼-- · 2019-07-13 12:34
my ($year, $month, $day, $hour, $min) = $file =~ /(\d{2})/g;

if ($min) {
    $year += 100; # Assuming 2012 and not 1912
    $month--;
    # Do stuff
}
查看更多
Lonely孤独者°
3楼-- · 2019-07-13 12:39

Using a module that parses dates might be nice. This code will parse the date and return a DateTime object. Refer to the documentation to see the many ways to manipulate this object.

use DateTime::Format::Strptime;

my $date = "1202242219";
my $dt = get_obj($date);

sub get_obj {
    my $date = shift;
    my $strp = DateTime::Format::Strptime->new(
        pattern     => '%y%m%d%H%M'
    );
    return $strp->parse_datetime($date);
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-07-13 12:45

Might as well teach DateTime::Format::Strptime to make the comparison much simpler:

use DateTime qw();
use DateTime::Format::Strptime qw();

if (
    DateTime::Format::Strptime
        ->new(pattern => '%y%m%d%H%M')
        ->parse_datetime('Rajesh.1202242219')
    < DateTime->now
) {
    say 'filename timestamp is earlier than now';
} else {
    say 'filename timestamp is later than now';
};
查看更多
乱世女痞
5楼-- · 2019-07-13 12:50

I think unpack might be a better fit.

if ( my ( $num ) = $file =~ /Rajesh.(\d+).*/ ) {
    my ( $year, $mon, $day, $hour, $min ) = unpack( 'A2 A2 A2 A2 A2', $num ); 
    my $ts = POSIX::mktime( 0, $min, $hour, $day, $mon - 1, $year + 100 );
    ...
}
查看更多
三岁会撩人
6楼-- · 2019-07-13 12:51

I like to use time objects to simplify the logic. I use Time::Piece here because it is simple and light weight (and part of the core). DateTime can be another choice.

use Time::Piece;
my ( $datetime ) = $file =~ /(\d+)/;
my $t1 = Time::Piece->strptime( $datetime, '%y%m%d%H%M' );
my $t2 = localtime(); # equivalent to Time::Piece->new

# you can do date comparisons on the object
if ($t1 < $t2) {
    # do something
    print "[$t1] < [$t2]\n";
}
查看更多
登录 后发表回答