Discussion:
Parsing RFC 822 date/time
Tobias Grimm
2005-09-18 01:16:29 UTC
Permalink
Hi!

I'm trying to parse a rfc822 date as used in RSS feeds. It looks like this:

Sat, 07 Sep 2002 00:00:01 GMT

Is this possible with time_facet somehow? I think at least the time zone
can't be parsed, because the corresponding format specifiers are for
output only. Any idea how this kind of date format should be parsed?

bye,

Tobias
Jeff Garland
2005-09-18 15:24:38 UTC
Permalink
Post by Tobias Grimm
Hi!
Sat, 07 Sep 2002 00:00:01 GMT
Is this possible with time_facet somehow? I think at least the time zone
can't be parsed, because the corresponding format specifiers are for
output only. Any idea how this kind of date format should be parsed?
You are exactly right. For the base time information you should be able to do
the following (I didn't test this)...

time_facet* timefacet = new time_facet("%a, %d %b %Y %H:%M:%S");
std::stringstream ss;
ss.str("Sat, 07 Sep 2002 00:00:01 GMT");
ss.imbue(std::locale(std::locale::classic(), timefacet));
ptime t;
ss >> t;

As for the timezone RFC 822 makes some unfortunate 'special cases' for US
based timezones. Life would be better if they would conform to posix 1003.1
specifications. So basically you will need a little parsing engine to extract
the timezone and do something useful with it. I'd recommend mapping the
special cases (eg: MDT, ECT) to a posix time string which you can use to
create a posix_time_zone. You can find details at:

http://www.boost.org/doc/html/date_time/local_time.html#date_time.local_time.posix_time_zone
Post by Tobias Grimm
From there you can construct the appropriate local_time instance.
Probably a better approach would be to check out the spirit grammer for rfc822
written by Peter Simons. Look for 'rfcdate' at

http://spirit.sourceforge.net/repository/applications/show_contents.php

This could be easily combined with date-time to do what you want.

Jeff
Tobias Grimm
2005-09-18 23:50:37 UTC
Permalink
Hi,
Post by Jeff Garland
You are exactly right. For the base time information you should be able to do
the following (I didn't test this)...
time_facet* timefacet = new time_facet("%a, %d %b %Y %H:%M:%S");
Mmm... I even can't get this to compile.

date_test.cc:17: error: use of class template `template<class time_type,
class
CharT, class OutItrT> class boost::date_time::time_facet' as expression

That's my test case:

#include <cxxtest/TestSuite.h>
#include <sstream>
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/time_facet.hpp"

using namespace boost::posix_time;
using namespace boost::date_time;

class DateTest: public CxxTest::TestSuite
{
public:
void TestConvert()
{
time_facet* timefacet = new time_facet("%a, %d %b %Y %H:%M:%S");
std::stringstream ss;
ss.str("Sat, 07 Sep 2002 00:00:01 GMT");
ss.imbue(std::locale(std::locale::classic(), timefacet));
ptime t;
ss >> t;
}
};

(Using version 1.32.0 with gcc 3.3.6)
Post by Jeff Garland
Probably a better approach would be to check out the spirit grammer for rfc822
written by Peter Simons. Look for 'rfcdate' at
Thanks a millon! This looks interesting.

Regards,

Tobias
Jeff Garland
2005-09-19 01:20:46 UTC
Permalink
Post by Tobias Grimm
Hi,
Post by Jeff Garland
You are exactly right. For the base time information you should be able to do
the following (I didn't test this)...
time_facet* timefacet = new time_facet("%a, %d %b %Y %H:%M:%S");
Mmm... I even can't get this to compile.
Sorry I wasn't watching the boost versions -- this facet code is a 1.33
feature. It's pretty much impossible with 1.32 facet code...
Post by Tobias Grimm
Post by Jeff Garland
Probably a better approach would be to check out the spirit grammer
for rfc822 written by Peter Simons. Look for 'rfcdate' at
Thanks a millon! This looks interesting.
You'll still have to do some work to integrate them as rfcdate produces a tm
and not a date-time type (although there is a simple function to convert a tm
to a ptime). Oh and the posix_time_zone and local_time classes are also 1.33
features so if you want to use them you'll need to upgrade. But in lots of
scenarios you can just drop the tz data and construct a UTC time...all depends
on what you want to do after you get the time. Anyway, if you make a nice
variant of the rfc822 parser and want to contribute it back I'll be happy to
look at putting it in as at least an example ;-) Good luck.

Jeff

Continue reading on narkive:
Search results for 'Parsing RFC 822 date/time' (Questions and Answers)
7
replies
What is it MPEG4 ?
started 2006-10-19 12:10:16 UTC
computer networking
Loading...