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

Hello Monks... I am using date extraction in my scripts for comparison with Oracle database dates (in mm/dd/yyyy format). The script which I am using is
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(tim +e); my $currSysDate; my $currYear; my $day; $year = $year + 1900; $mon = $mon+1; $day = $mday; if($mon <10){ $mon ="0$mon"; } if($day <10){ $day ="0$day"; } $currSysDate = join("/",$mon,$day,$year); print "$year \n"; print "$currSysDate \n";
Now the question is that this does not seem to be a very good approach. as incase I try to increment the month just before the "join" the date will again have single digit month. So is there a better approach to simply get the date in mm/dd/yyyy format so that it can be compared with oracle database date format. Thanks

Replies are listed 'Best First'.
Re: Formats for date.
by gellyfish (Monsignor) on Jun 28, 2005 at 21:33 UTC
Re: Formats for date.
by BaldPenguin (Friar) on Jun 28, 2005 at 21:48 UTC
    You could also use:
    $currSysDate = sprintf("%02d/%02d/%04d",$mon,$day,$year);
    But it should be noted that if you use this approach, no validation occurs. You could create a bad date;

    Don
    WHITEPAGES.COM | INC

    Edit by castaway: Closed small tag in signature

      Are you implying that strftime does validation? I can't find any documentation to support that. Validation isn't needed anyway, since he's printing out the result of localtime.
        Actually, what I was implying was that if the localtime was read and then a value such as month were to be incremented, then sprintf() would print out a bad date( i.e. 13/30/2005). You are correct in that passing localtime straight to strftime would not require validation, if any were to exist. Thanks.

        True validation could be accomplished using Date::Calc or DateTime or similar to increment the date values.

        Don
        WHITEPAGES.COM | INC

        Edit by castaway: Closed small tag in signature

Re: Formats for date.
by shiza (Hermit) on Jun 28, 2005 at 23:58 UTC
Re: Formats for date.
by fmerges (Chaplain) on Jun 28, 2005 at 22:13 UTC

    Hi,

    You can also say the Oracle that for your session you want another date output, for example if we were talking about a timestamp, you could get from the DB a timestamp in epoch so it is easier and faster to do comparisions later in your code.

    Salve,