Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comparing dates

by Anonymous Monk
on Oct 17, 2001 at 02:38 UTC ( [id://119276]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings.
I have a script that is generating a report in html using data from a flat-file pipe de-limited database where each entry is on a separate line. The report is comparing the most current entry with the last entry from the previous week. I am using the code below to compare the dates of the most recent entry with each previous entry until a different date is found.
open (DATABASE, "$filename" || &ErrorMessage); @entry = <DATABASE>; #Assign DB entrie +s to array close(DATABASE); @CurrentData = split(/\|/, $entry[$lines - 1]); #Assign last line + of data to array @PreviousData = split(/\|/, $entry[$lines - 1]); $count = $lines - 1; #Find previous we +eks data by comparing dates while (($PreviousData[1] eq $CurrentData[1]) && ($count >= 0)) { $count -= 1; @PreviousData = split(/\|/, $entry[$count]); }

This works as long as the dates are in the same month. If the dates are in different months the script will not display the most recent entry. Can anyone see what I'm doing wrong here? Is there a better/more effecient way of doing this?

Thanks for your time.

Replies are listed 'Best First'.
Re: comparing dates
by andreychek (Parson) on Oct 17, 2001 at 03:59 UTC
    Ahhh, yes.. comparing dates. I've always found that the absolute easiest way to compare two dates is by first converting them to seconds (seconds since the epoch, to be specific).

    You didn't show the particular format of date which you are using -- however, assuming that you have any kind of date format but seconds since the epoch, you can use the Time::Local module to convert it for you. Once you convert it to seconds, you're simply comparing two integers, which is very trivial.

    If you read the post Re: Within A Date Time Range, I offered an example implementation on how this might be done. However, if you make use of Super Search, there's bound to be some other examples of that too. Also, you may want to check out Date format manipulation, which was posted just a few hours before your question.

    Good luck!
    -Eric
      Interesting, I've always used POSIX::mktime to convert a localtime array back into a timestamp. Does anyone else have a preference or rationale why one might be used over the other?
      #!/usr/bin/perl -wT use strict; use Time::Local; use POSIX; my @localtimearr = localtime; print Time::Local::timelocal(@localtimearr), "\n"; print POSIX::mktime(@localtimearr), "\n"; =sample OUTPUT 1003277170 1003277170

      -Blake

Re: comparing dates
by MrNobo1024 (Hermit) on Oct 17, 2001 at 08:02 UTC
    This is somewhat unrelated to your question, but I noticed this line:
    open (DATABASE, "$filename" || &ErrorMessage);
    This only gives an error message if $filename isn't true. What you probably meant was
    open (DATABASE, "$filename") || &ErrorMessage;
    which gives an error message when the file couldn't be opened.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://119276]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2024-04-25 00:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found