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

I am looking for an efficient Perl way to replace the following bash command or MySQL statement

bash: date -d"1899-12-30 +40254 days" MySQL: DATE_ADD('1899-12-30', interval 40254 day)

Both export '2010-03-17'. I need to find a reliable and efficient Perl way or a small neat Perl module to handle the same thing, any recommendations?

Thank in advance

lihao

Update: Thanks guys for all suggestions. I am actually looking for some lighter solution or new Perl modules which I don't know. the target is to transfer the parsed Excel datetime into MySQL datetime format. In some 32-bit Linux box, i.e. 32bit RHEL5, the -d switch on the date command does NOT work for all datetime. Since I only care about the exact date instead of time, I am using the following formula to handle the date transfer(where 2209143600 is the second from "1899-12-30 00:00:00" on local to "1970-01-01 00:00:00" by a bash line date -d '1899-12-30 00:00:00' +%s)

sub get_excel_date { return if not $_[0]; my $date = shift; my @x = localtime($date*24*3600 - 2209143600); return sprintf("%04d-%02d-%02d", $x[5]+1900, $x[4]+1, $x[3]); }

Replies are listed 'Best First'.
Re: Looking for an efficient way to do datetime transfer
by Corion (Patriarch) on Mar 30, 2010 at 17:22 UTC

      Thanks, Corion, is there a Perl module that I can use in a way similar to bash's `date` command.

        Sarcastic reply, inspired by irritation at a question that's either lazy or senseless or both! (and posted by a Monk of long-standing association with the order: "User since:   Oct 17, 2007 at 21:16 GMT+5")

        "DateTime, Date::Calc, many other modules in the Date or DateTime namespace." (Linking omitted, as it was supplied in the excellent answer above.)

Re: Looking for an efficient way to do datetime transfer
by ikegami (Patriarch) on Mar 30, 2010 at 17:42 UTC
    Maybe not the most efficient or compact, but here's a reliable DateTime solution.
    use DateTime::Format::ISO8601 qw( ); my $dt = DateTime::Format::ISO8601 ->parse_datetime('1899-12-30') ->add( days => 40254 ); print($dt->ymd(), "\n");
Re: Looking for an efficient way to do datetime transfer
by moritz (Cardinal) on Mar 30, 2010 at 19:53 UTC
    again, Date::Simple to the rescue:
    $ perl -MDate::Simple=date -wle 'print date("1899-12-30") + 40254' 2010-03-17
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Looking for an efficient way to do datetime transfer
by ambrus (Abbot) on Mar 31, 2010 at 13:12 UTC

    Using the Date::Manip module, the solution is

    use Date::Manip; print UnixDate(DateCalc("1899-12-30", "+40254 days"), "%Y-%m-%d\n");