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

Looking for an efficient way to do datetime transfer

by lihao (Monk)
on Mar 30, 2010 at 17:20 UTC ( [id://831906]=perlquestion: print w/replies, xml ) Need Help??

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");

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-23 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found