tags:

views:

80

answers:

2

I have dates in a bunch of formats. Now i would like to have a function (from some library) in c++ that can parse these date/time string and give me some structure like a tm or convert them to some deterministic representation so that i can play around with the date/time.

Some formats that I see are as follows : Tue, 19 Feb 2008 20:47:53 +0530 Tue, 28 Apr 2009 18:22:39 -0700 (PDT)

I am able to do the ones without the timezones but for the ones with the timezone, i basically need the library to convert it to UTC in the tm structure.

I have tried boost and strptime, but as far as i know, both do no support timezones on inputs. is there anything that i have missed?

ANy help on this one will be really appreciated.

Regards

A: 

You can do that with boost, but it's a little particular about the format of time zones in the input string. It has to be in POSIX time zone format.

For example:

#include <iostream>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/time_facet.hpp>
int main()
{
       std::string msg = "28 Apr 2009 18:22:39 PST-8PDT,M4.1.0,M10.1.0"; // or just "PDT-7" but that will be a new, fake time zone called 'PDT'

       std::istringstream ss(msg);
       ss.imbue( std::locale(ss.getloc(),
                 new boost::local_time::local_time_input_facet("%d %b %Y %H:%M:%S%F %ZP")));
       boost::local_time::local_date_time t(boost::date_time::pos_infin);
       ss >> t;
       std::cout << t << '\n';
       // and now you can call t.to_tm() to get your tm structure
}

I would add some sort of pre-processing to convert your time zone formats to posix format, and then feed the strings to boost.

Cubbi
+1  A: 

If you have a lot of formats to deal with, I suggest taking a look at ICU: http://site.icu-project.org/

NuSkooler