Perl programmer for hire: download my resume (PDF).
John Bokma's Hacking & Hiking

RFC #822 and RFC #3339 dates in Perl

October 9, 2019

When adding a JSON feed and an RSS feed to tumblelog I needed a publication date and time for each article published. As I don't have an actual publication time I decided to use the end of day, as explained in A Matter of Time.

In order to obtain a Time::Piece Perl object for the local end of day for a given date I came up with the following idea:

I accomplished this in Perl as follows:

sub get_end_of_day {

    return Time::Piece->localtime(
        Time::Piece->strptime( shift . ' 23:59:59', '%Y-%m-%d %H:%M:%S' )
    );
}

Next, for the RSS feed I needed the pubDate to be in RFC #822 format, which I obtained by using the strftime method as follows:

my $pub_date = $end_of_day->strftime( '%a, %d %b %Y %H:%M:%S %z' );

It's tempting to use %Z instead of %z but note that, for example, CEST is not valid according to the RSS feed validator, which refers to section 5.1 of RFC #822. Hence, why digits must be used instead for a general solution.

An example of a $pub_date value is:

Wed, 09 Oct 2019 23:59:59 +0200

Finally, for the JSON feed I needed a date_published in RFC #3339 format. I also used the strftime method, which almost gives the correct result except that the time returned by %z doesn't contain a colon, so insert this required character.

( my $date_published = $end_of_day->strftime( '%Y-%m-%dT%H:%M:%S%z' ) )
    =~ s/(\d\d)$/:$1/;

An example of a $date_published value is:

2019-10-09T23:59:59+02:00

See Also