Friday, 2022-11-25

Parsing RFC 3339 timestamps using strptime in Perl

RFC 3339 is a subset[1] of ISO 8601 time formats for use in Internet communications.

An RFC 3339 timestamp can look like this: 2022-11-25T09:26:04+01:00.[2] This is the format required by the Atom and JSONfeed specifications.

Perl’s standard module Time::Piece has a simple string parsing functionality implementing the POSIX strptime format.

The problem is that the strftime directive %z does not match a timezone designation in the format +01:00, only +0100.

So to use Time::Piece to parse RFC 3339 dates, use the following Perl snippet:

my $string = "2022-11-25T09:26:04+01:00";  
$string =~ s/:(\d+)$/$1/; # strip last colon  
my $ts = Time::Piece->strptime($string, "%FT%T%z");   
# output Unix timestamp  
say $ts->epoch;

The %F directive represents the date %Y-%m-%d and is present as a GNU extension. It does not seem to be part of the original POSIX specification.

[1] For an exhaustive list of differences, see this page

[2] GNU date omits the separating T from its rfc-3339 output, but includes it in the iso-8601 version. Both indicate the timezone with a colon.