Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Time / Date Arithmetic Module Recommendations

by ikegami (Patriarch)
on Oct 09, 2008 at 03:36 UTC ( [id://716123]=note: print w/replies, xml ) Need Help??


in reply to Time / Date Arithmetic Module Recommendations

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' );.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found