hide random home http://www.fmi.uni-passau.de/archive/doc/unix/perl/faq/2.26.html (Einblicke ins Internet, 10/1995)

How can I compare two date strings?

How can I compare two date strings?


    If the dates are in an easily parsed, predetermined format, then you
    can break them up into their component parts and call &timelocal from
    the distributed perl library.  If the date strings are in arbitrary
    formats, however, it's probably easier to use the getdate program
    from the Cnews distribution, since it accepts a wide variety of dates.
    Note that in either case the return values you will really be
    comparing will be the total time in seconds as return by time().
   
    Here's a getdate function for perl that's not very efficient; you 
    can do better this by sending it many dates at once or modifying
    getdate to behave better on a pipe.  Beware the hardcoded pathname.

        sub getdate {
            local($_) = shift;

            s/-(\d{4})$/+$1/ || s/\+(\d{4})$/-$1/; 
                # getdate has broken timezone sign reversal!

            $_ = `/usr/local/lib/news/newsbin/getdate '$_'`;
            chop;
            $_;
        } 

    Richard Ohnemus <rick@IMD.Sterling.COM> actually has a getdate.y
    for use with the Perl yacc.  You can get this from ftp.sterling.com
    [192.124.9.1] in /local/perl-byacc1.8.1.tar.Z, or send the author
    mail for details.

    You might also consider using these: 

    date.pl        - print dates how you want with the sysv +FORMAT method
    date.shar      - routines to manipulate and calculate dates
    ftp-chat2.shar - updated version of ftpget. includes library and demo programs
    getdate.shar   - returns number of seconds since epoch for any given date
    ptime.shar     - print dates how you want with the sysv +FORMAT method

    You probably want 'getdate.shar'... these and other files can be ftp'd from
    the /pub/perl/scripts directory on coombs.anu.edu.au. See the README file in
    the /pub/perl directory for time and the European mirror site details.