Produce valid dates in RFC3339

23 Nov 2008

In perl, to produce valid dates according to the RFC3339 standard, e.g. as required by the Atom specification, use UnixDate from Date::Manip. UnixDate is magic, it parses and translates strings containing dates in pretty much any conceivable format.

use Date::Manip;

# normalize all dates to UTC
Date_Init("ConvTZ=UTC");

sub valid_date {
  return UnixDate($_[0], "%OZ");
}

# then you use it as
$date = valid_date("now"); # 2008-11-23T12:22:06Z
$date = valid_date("Sat, 07 Sep 2002 00:00:01 GMT"); # 2002-09-07T00:00:01Z