Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Need help comparing 4 dates

by ww (Archbishop)
on Oct 03, 2010 at 00:10 UTC ( #863122=note: print w/replies, xml ) Need Help??

in reply to Need help comparing 4 dates

"When i actually return the dates from oracle and compare 2 dates date_cmp works fine. I'm explaining this because the real issue is that i'm struggling with the concept of how to compare 4 dates..not that __DATA__ doesn't recognize the date formats i added for this sample. "
  1. Then what Oracle is returning includes some sort of timezone information absent from your __DATA__ section. See "ERROR reporting," below. [BUT, see also ig's reply to this node, which may clear dirtdog of malfeasance in regard to timezone & error reporting.]
  2. Perhaps you meant to repeat your reference to "data_cmp" (sic) but it's just as well you didn't, as the proper reference is to "Date_Cmp". As to your statement later re __DATA__ you should know __DATA__ doesn't recognize anything. Perl recognizes what follows __DATA__ as data to be used by the script above (assuming the script reads that data).

Returning to Date_Cmp, the doc (perldoc Date::Manip)tells us:

. Compare two dates $date1 = ParseDate($string1); $date2 = ParseDate($string2); $flag = Date_Cmp($date1,$date2); if ($flag<0) { # date1 is earlier } elsif ($flag==0) { # the two dates are identical } else { # date2 is earlier }

Also in the doc, and directly relevant to your desire to compare four dates,

Among other things, Date::Manip allow you to: ... 2. Compare two dates, entered in widely different formats to determine which is earlier

"Compare two dates" is relevant. Comparing two dates is one of the things Date::Manip does; comparing four (without more help from you) is NOT. Since you want to do more, you'll need to change your approach.

For example, you could convert all the date_times to seconds and sort those values, then assign ranks, based on the order in which they sorted (Tedious, but straightforward). Or you could use another capability in Date::Manip, the ability to determines deltas, by comparing each date_date in each row to the date_time of execution, and ranking (ordering) the deltas. Others will undoubtedly suggest other approaches, some of which may be superior to anything here.

ERROR reporting: When executed, your code outputs an error message, "ERROR: Date::Manip unable to determine TimeZone. You didn't tell us that; did you notice it?

Turning again to the doc, you'll find several discussions of TimeZone which may help you turn the code you posted into code that actually compiles. Doing so -- preferably before posting -- is a good way to help assure yourself that Monks inclined to help aren't ( annoyed | turned off ) by the failure to do so.

Update: Likely irrelevant portions stricken. My bad for failing to consider OSen (happened to be on a Win32 machine at the time) and vintage of said machine's modules.

Replies are listed 'Best First'.
Re^2: Need help comparing 4 dates
by ig (Vicar) on Oct 03, 2010 at 00:49 UTC

    I ran the OPs code on a linux system and didn't see an error about timezone. Maybe the OP didn't have such an error either.

    Older versions of Date::Manip need to be told the time zone when running on Windows, but were generally able to determine the time zone from the system on *nix platforms. Documentation for current version of Date::Manip suggests it tries to get the time zone by reading the registry on Windows, so it may no longer need to be told - I haven't tried current version on Windows.

    I am guessing you are running an older version of Date::Manip (5.x) on Windows and the OP is running current version on Windows or any verson on some other platform.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863122]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2018-02-25 06:44 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.