DateTime kicks butt

snarkyboojum on 2010-04-06T01:53:17

The DateTime module blows me away with it's usability and functionality time and time again. I haven't been using it for very long (naughty), but I'm finding it so intuitive and easy to use. Below is a very simple example that I came up with this morning.

I wanted to convert dates and times that I retrieve from delicious that are of the format Mon Apr 05 17:25:35 +0000 2010 and I wanted to parse that reliably (avoiding regexes if possible). I also wanted to be able to change timezones, and format the date as I saw fit.

The script I whipped together to test this out was:


use strict;
use warnings;

use DateTime::Format::Strptime;

my $date = q{Mon Apr 05 17:25:35 +0000 2010};
my $strp = DateTime::Format::Strptime->new(pattern => '%a %b %d %T %z %Y');

my $dt = $strp->parse_datetime($date);
$dt->set_time_zone('Australia/Sydney');

my $readable_date = $dt->strftime('%d %B %H:%M %Y');

print "Original date/time: $date", "\n";
print "Converted date/time: $readable_date", "\n";

An example run:

$ perl test_date.pl
Original date/time: Mon Apr 05 17:25:35 +0000 2010
Converted date/time: 06 April 03:25 2010

Talk about Just Working. Use DateTime - it rocks!