Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^3: How to compare two dates?

by 1nickt (Abbot)
on Aug 04, 2019 at 04:25 UTC ( #11103840=note: print w/replies, xml ) Need Help??

in reply to Re^2: How to compare two dates?
in thread How to compare two dates?

Hi, so when you got the error, did you print out the value of $date to see what you were passing to the parser?

The first answer you got had all the tools you needed. Here I've added Test::More so I can set up some simple testing of my subroutine, checking comparisons that I already know the answer to. It's a good way to develop your code.

use strict; use warnings; use feature 'state'; use Test::More; use DateTime::Format::Strptime; for my $line (<DATA>) { chomp $line; my ( $first, $second, $expected ) = split /,/, $line; is( date_cmp($first, $second), $expected, "compared to $first, $se +cond is $expected" ); } done_testing; exit; sub date_cmp { state $parser = DateTime::Format::Strptime->new(pattern => '%m/%d/ +%Y'); my $dt1 = $parser->parse_datetime(shift) or die $parser->errmsg; my $dt2 = $parser->parse_datetime(shift) or return 'invalid'; return 'equal' if $dt1 eq $dt2; return $dt1 > $dt2 ? 'earlier' : 'later'; } __DATA__ 03/02/1999,03/01/1999,earlier 03/02/1999,03/03/1999,later 03/02/1999,03/02/1998,earlier 03/02/1999,03/02/2000,later 03/02/1999,03/02/1999,equal 03/02/1999,03/02/199 ,invalid
$ perl ok 1 - compared to 03/02/1999, 03/01/1999 is earlier ok 2 - compared to 03/02/1999, 03/03/1999 is later ok 3 - compared to 03/02/1999, 03/02/1998 is earlier ok 4 - compared to 03/02/1999, 03/02/2000 is later ok 5 - compared to 03/02/1999, 03/02/1999 is equal ok 6 - compared to 03/02/1999, 03/02/199 is invalid 1..6

Hope this helps!

The way forward always starts with a minimal test.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11103840]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2020-09-28 03:25 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (143 votes). Check out past polls.