[Boost-users] [date_time] Decoding ISO 8601 strings with timezones?

895 views
Skip to first unread message

Allen Cronce

unread,
May 1, 2010, 3:29:34 PM5/1/10
to boost...@lists.boost.org
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 mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

--
You received this message because you are subscribed to the Google Groups "BOOST Archives" group.
To post to this group, send email to boost...@googlegroups.com.
To unsubscribe from this group, send email to boost-list+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/boost-list?hl=en.

Reply all
Reply to author
Forward
0 new messages