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

While looking into our legacy system we found that some records which are stored as comma delimited text files, dates fields are with 2 digit year. Now for each key there are many records. we want to have records with oldest date only, How should we do date comaparison because some records are new also ie after 2000. rest of the process is done but we are confused about it. All scripts are in perl only.

Replies are listed 'Best First'.
Re: Date comparison Y2K problem
by andreas1234567 (Vicar) on Apr 15, 2008 at 20:06 UTC
    Date::Calc has a function Moving_Window that you may find useful for converting two-digit year to four digits:
    $year = Moving_Window($yy);
    That is, two-digit year numbers are first mapped to the same century as the current year. If the resulting year is smaller than the current year minus 50, then one more century is added to the result. If the resulting year is equal to or greater than the current year plus 50, then a century is subtracted from the result.
    --
    When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do. [1]
Re: Date comparison Y2K problem
by moritz (Cardinal) on Apr 15, 2008 at 19:50 UTC
    You can set an arbitrary limit, for example 30. Everything below that is interpreted as a 20XX date, everything above that as a 19XX date.
    $year += 100 if $year < 30;

    Then just compare by number.

      yeah similar stategy we are thinking to follow but we have also planned to convert these dates into 4 digit year dates .. is there any posibilities
        Of course!

        Just read your files record by record and write them out again with the dates suitably made Y2K compliant. Then you can use these new files without problems.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        moritz actually means 1900 so something like:

        sub fixY2K { my $date = shift; return $date if $date > 1900; return $date + 1900 if $date > 30; warn "Assuming '$date' means 20$date!\n"; return $date + 2000; }
Re: Date comparison Y2K problem
by locked_user sundialsvc4 (Abbot) on Apr 15, 2008 at 19:52 UTC

    Most date-handling packages in CPAN have some understanding of two-digit years. What you do is to take the various components that make up a date (in your data), use them to create an object of a suitable date/time type, then use that in the sort-compare routine... comparing the date/time objects you've made, using the methods they provide for comparing themselves to things.

    So you've handled the sorting process by means of a custom compare-function, and you've passed-off the date/time complexities to the object.

    If you want something simpler, just write your own compare function and normalize the dates yourself: if the value's in the range x-y, add 1900 to it; else add 2000 to it. Good for a hundred years. (“Dig me up and I'll fix it then...”)