Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Time / Date Arithmetic Module Recommendations

by tomazos (Deacon)
on Oct 09, 2008 at 02:15 UTC ( #716119=perlquestion: print w/replies, xml ) Need Help??

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

I would like to do some arithmetic on time, dates and timezones. Could someone recommend the best approach and modules to use?

For example:

How many days between now and Christmas?

use Foo; my $christmas = Foo->date(25, Foo->december(), Foo->today()->as_year() +); my $days_to_christmas = (Foo->today() - $christmas)->in_days();

-Andrew.


Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com

Replies are listed 'Best First'.
Re: Time / Date Arithmetic Module Recommendations
by ikegami (Patriarch) on Oct 09, 2008 at 03:36 UTC

    I dealt with DateTime recently, and I have great respect for it. The author understands the complexities of data and time arithmetic and does a good job of preventing you from shooting yourself in the foot.

    use strict; use warnings; use DateTime qw( ); my $today = DateTime->today( time_zone => 'local' ); my $xmas = $today->clone()->set_month( 12 )->set_day( 25 ); if ( $xmas < $today ) { $xmas->add( year => 1 ); } my $days = $xmas->delta_days( $today )->in_units( 'days' ); if ($days == 0) { print( "Merry Christmas!\n" ); } elsif ($days == 1) { print( "Christmas is tomorrow!\n" ); } else { print( "Christmas is in $days days.\n" ); }

    prints

    Christmas is in 78 days.

    Update: Removed unnecessary $today->set_time_zone( 'floating' );.

Re: Time / Date Arithmetic Module Recommendations
by smiffy (Pilgrim) on Oct 09, 2008 at 05:54 UTC

    My toolkit consists of:

    Date::Calc takes care of most things for me, but I need HTTP::Date to handle those awful string dates used by the HTTP protocol.

    For your example, you would probably want to have a look at Delta_days in the Date::Calc module. The link above is to the POD and should tell you what you need to know.

    Just as an aside, as a web applications developer, I actually do most of my date crunching in MySQL. Perl tends to just have to do date validation before firing data off to the MySQL stored procedure (or query), with the same functionality duplicated client-side in JavaScript. (The latter cuts down on the rubbish that gets sent to the server. I tend to set a flag and a checksum in JavaScript that tells the server that the input has been pre-validated; Perl then only has to check the checksum before passing the data on down to the database.)

    Hope this helps.

    Update

    Fixed broken links in second list item. I will now write out 100 times "I must check my posts fully before submitting them."

Re: Time / Date Arithmetic Module Recommendations
by GrandFather (Saint) on Oct 09, 2008 at 03:33 UTC

    The kitchen sink module is Date::Manip. It's pretty up front about where and when you should use it and why you might not - see the 'SHOULD I USE DATE::MANIP' section in the docs.

    use strict; use warnings; use Date::Manip; Date_Init ('DateFormat = NZ', 'TZ = NZST'); my $today = ParseDate ('today'); DateCalc ($today, '25 december ' . UnixDate ($today, '%Y')) =~ /\d+:\d ++:(\d+):(\d+)/; print $1 * 7 + $2, ' sleeps until Xmas';

    Prints:

    76 sleeps until Xmas

    On most *nix boxes you shouldn't need the Date_Init, but you will need it on most Windows boxes. Your time zone may be different than mine too. ;)

    <p.Update updated for children.


    Perl reduces RSI - it saves typing

      76 days until Xmas

      At the time of our postings, there were 78 days to Christmas in the US and 77 days to Christmas in NZ. I think you aren't counting partial days. On the Dec 20th, one says it's five days to Christmas.

        Actually one tells one's children: "it is five sleeps until Christmas" so gaps between pickets is more appropriate than pickets in this case. ;)


        Perl reduces RSI - it saves typing
      You get odd results for Dec 26 .. Dec 31.
Re: Time / Date Arithmetic Module Recommendations
by andreas1234567 (Vicar) on Oct 09, 2008 at 07:49 UTC
    The Many Dates and Times of Perl (perl.com, by Dave Rolsky, March 13, 2003) is an extensive article on time concepts and modules.
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2023-11-30 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?