Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

snarkyboojum (9664)

  (email not shown publicly)

Journal of snarkyboojum (9664)

Monday April 05, 2010
08:53 PM

DateTime kicks butt

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

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
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!