[PBP-pm] Fwd: Working with dates

Rick Measham rick at measham.id.au
Wed Dec 21 04:36:18 PST 2005


----- Forwarded message from Jay Buffington <jay at mlug.missouri.edu> -----
> 
> Hi Everyone.
> 
> I'd like some feedback on best practices for working with dates in
> perl.  Here's what I came up with:
--- >8 ---
> Displaying Dates
> Use CPAN's DateTime to format dates for display.

If you're going to be using DateTime, then stick with DateTime. It 
probably does everything you need*.

my $lower = new DateTime(year => 2005, month => 12, day => 12 );
my $upper = new DateTime(year => 2005, month => 12, day => 19 );
my $date  = DateTime->today();

if (($date >= $lower) && ($date <= $upper)) {
    print "Today is between Dec. 12th and Dec. 19th!\n";
}

Plus, each of those three DateTimes can be in a different time zone and 
the result will still return the truth.


You can return the same hash from a function (though I'd personally 
return a hash rather than a hashref) and feed it straight into 
DateTime's constructor:

my $date1 = DateTime->new( %{ yourFunction() } );
my $date2 = DateTime->new( myFunction() );

Cheers!
Rick Measham

* If it doesn't do everything you need, then let the datetime list know 
so we can consider it!

-- 
  "War is God's way of teaching Americans geography."
                              -- Ambrose Bierce


More information about the PBP-pm mailing list