Pergunta

I want to compare different date formats in Perl like:

Date :-20140511062730Z 

compare with

Date :- 11-MAY-2014

Please suggest of how can we compare them if both these formats are in one file and need to extract them in different files.

File data is :-

dn: uid=501553930,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140511062730Z effectiveenddate: 11-MAY-2014

Here this user have same date format so should go in same_date_logs

dn: uid=501909342,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140611062730Z effectiveenddate: 11-MAY-2013

Here this user have different date format so should go in different_date_logs

Compare modifytimestamp with effectiveenddate for each record. File contains 1L records.

Nenhuma solução correta

Outras dicas

It's not clear what you mean by comparing the date formats, but this should help you.

Using the Time::Piece module's strptime to parse the different date/time formats you can generate consistent objects that can be compared directly.

use strict;
use warnings;

use Time::Piece;

my $t1 = Time::Piece->strptime('20140511062730Z', '%Y%m%d%H%M%SZ');
my $t2 = Time::Piece->strptime('11-MAY-2014', '%d-%b-%Y');

printf "%s is %s than %s\n", $t1, $t1 < $t2 ? 'earlier' : 'later', $t2;

output

Sun May 11 06:27:30 2014 is later than Sun May 11 00:00:00 2014

Would this solution meet your needs? Borrowed from Borodin with Time::Piece.

#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

open my $same, '>', 'same_date_logs' or die $!;
open my $diff, '>', 'diff_date_logs' or die $!;

while (<DATA>) {
    my ($modtimestamp) = /modifytimestamp: (\d{8})/;
    my ($effectiveend) = /effectiveenddate: (\d\d-[A-Z]{3}-\d{4})/;

    my $mod_date = Time::Piece->strptime($modtimestamp, '%Y%m%d%');
    my $end_date = Time::Piece->strptime($effectiveend, '%d-%b-%Y');

    if ($mod_date == $end_date) {
        print $same $_;
    }
    else {
        print $diff $_;
    }
}
close $same or die $!;
close $diff or die $!;

__DATA__
dn: uid=501553930,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140511062730Z effectiveenddate: 11-MAY-2014
dn: uid=501909342,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140611062730Z effectiveenddate: 11-MAY-2013

Prints to 2 files (below)

C:\Old_Data\perlp>type same_date_logs
dn: uid=501553930,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140511062730Z effectiveenddate: 11-MAY-2014

C:\Old_Data\perlp>type diff_date_logs
dn: uid=501909342,ou=worker,dc=tfayd,dc=com modifytimestamp: 20140611062730Z effectiveenddate: 11-MAY-2013
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top