GhodMode has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to convert from/to RFC 822 dates without using the DateTime module?

I'm working on a program to process podcast RSS feeds and I'm running into a minor problem with the pubDate element. I want to determine whether or not to download the podcast based on the date that it was published, but all of the feeds publish in different time zones. So, I need to convert the pubDate (formatted per RFC 822) to my local time in order to use it.

One solution involves the DateTime module, but I'd like to avoid additional dependencies, if possible.

Another solution is to just use the shell's date command, which recognizes the RFC 822 date format and can convert it to another format for me.
For example:

use POSIX 'strftime'; my $pubdate = 'Thu, 11 Jun 2009 11:50:00 PDT'; my $pubdate_here = strftime("%D %T", localtime(`date +"%s" --date="$pu +bdate"`));
This is the solution I'll use if there isn't another option, but it seems like there must be something built into Perl that I'm missing.

Thank you.
--
-- Ghodmode
Blessed is he who has found his work; let him ask no other blessedness.
-- Thomas Carlyle

Replies are listed 'Best First'.
Re: RFC 822 date manipulation without DateTime module?
by Anonymous Monk on Jun 12, 2009 at 07:02 UTC

      When you mentioned it, I thought that strptime was exactly what I was looking for, but it's not actually part of the core POSIX module. It's just an extension.

      XML::FeedPP doesn't allow the text representation of the time zone, so it wouldn't solve this particular problem. It would save me a lot of effort if I had started with something like this, but I'm doing this more to learn how to program around an RSS/podcast feed than to actually get the job done.

      My main goal when I asked this question was to avoid introducing additional dependencies on the program rather than just to avoid DateTime. So, I'm not really looking for another module to do the job. I think that the answer is that Perl doesn't have this feature.

      Thank you.

      --
      -- Ghodmode
      
      Blessed is he who has found his work; let him ask no other blessedness.
      -- Thomas Carlyle
Re: RFC 822 date manipulation without DateTime module?
by Marshall (Canon) on Jun 12, 2009 at 10:39 UTC
    I haven't used this Perl version of strftime (a C function), but in general convert to gmtime (which takes a trip though Unix Epoch time) and then back to your local time which is what the code above does underneath the covers (or so I think). The time() functions are actually fairly "expensive" in terms of CPU, but unless you have some high performance app, this is the way to go.

      I can't use gmtime because it can't use a string like the date specification in an RSS feed. It only takes the number of seconds since the epoch as returned by time (or the shell's date +%s).

      The only real problem is with the text representation of the time-zone. Looking through the time zones used in my feeds, I see that they're using either an offset of UTC, or a three-character representation of the time zone. These are mentioned in RFC 822, but it also says that "time zone may be indicated in several ways".

      The notes for the POSIX strftime function say that the timezone format specifier is notoriously unportable because the timezones are non-standard. That's probably a good reason why it's not built into the core of the language. The (GNU/Linux) shell's date program recognizes every one, though, whether it uses the offset or the abbreviation.

      --
      -- Ghodmode
      
      Blessed is he who has found his work; let him ask no other blessedness.
      -- Thomas Carlyle
        I was just saying the time conversion routines will take a trip through epoch time. That is true for whatever module or library that you use. These things take your text and convert it to a number and then take that number and make new text out of it!

        I found this article on Tech Republic: http://articles.techrepublic.com.com/5100-10878_11-6132371.html

        Basically this local time to gmtime is a mess! I hear that there are some parts of US states that adjust clocks +-30 minutes, not whole hours! The Perl modules will do what the GNU modules will do and maybe even more.

        If you are really interested in "getting it right", I would suggest the Perl date/time module. This thing figures out leap seconds and stuff like that.

        for conversion like from PDT to PSDT, etc, Perl is great at that!