Boost logo

Boost Users :

Subject: [Boost-users] [date_time] Decoding ISO 8601 strings with timezones?
From: Allen Cronce (allenslists_at_[hidden])
Date: 2010-05-01 15:29:34


Hi all,

I'm trying to decode ISO strings into ptime values. But when I initialize what should be two equivalent ptimes, one in UTC and the other with a timezone designation, the resulting ptimes do not compare. It's as if the ISO extended format input facet is ignoring timezone values. I'm using boost 1.42.0.

I tried instancing an input facet with "%Y-%m-%dT%H:%M:%S%F%Q" instead of calling set_iso_extended_format, but this didn't work either.

Can someone suggest a solution?

Also, all the sample code I've seen in this area allocates a time_input_facet but does not delete it. Does the stream take care of deallocating the input facet when it goes out of scope, or will this result in a memory leak?

Here's the code and resulting assert output:

--snip--
ptime from_iso_extended_string(const string& inDateString)
{
        // If we get passed a zero length string, then return the "not a date" value.
        if (inDateString.empty())
        {
                return not_a_date_time;
        }
        
        // Use the ISO extended input facet to interpret the string.
        std::stringstream ss;
        time_input_facet* input_facet = new time_input_facet();
        input_facet->set_iso_extended_format();
        ss.imbue(std::locale(ss.getloc(), input_facet));
        ss.str(inDateString);
        ptime timeFromString;
        ss >> timeFromString;
        
        return timeFromString;
}

void boostTimeTest(void)
{
        cout << "Test decoding an ISO 8601 string with different timezones for equivalency:" << endl;
        string utcTimeStr("2010-05-01T18:00:00.000Z");
        string localTimeStr("2010-05-01T11:00:00.000-07:00");
        ptime utcTime = from_iso_extended_string(utcTimeStr);
        ptime localTime = from_iso_extended_string(localTimeStr);
        cout << " Original utc string: " << utcTimeStr << endl;
        cout << " Decoded utc string: " << to_iso_extended_string(utcTime) << endl;
        cout << " Original local string: " << localTimeStr << endl;
        cout << " Decoded local string: " << to_iso_extended_string(localTime) << endl;

        // The following asserts because the resulting ptimes are not equivalent
        assert(utcTime == localTime);
}

--------------------------------------------
Test decoding an ISO 8601 string with different timezones for equivalency:
  Original utc string: 2010-05-01T18:00:00.000Z
  Decoded utc string: 2010-05-01T18:00:00
  Original local string: 2010-05-01T11:00:00.000-07:00
  Decoded local string: 2010-05-01T11:00:00
Assertion failed: (utcTime == localTime), function boostTimeTest

--snip--

Best,

--
Allen Cronce

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net