Using strptime to parse ISO 8601 formated timestamps
Saturday, April 15th, 2006A lot of dates that come back from XML based web services are in the ISO 8601 form. I found out recently that it isn’t straight forward to parse such a date using C functions and have the time come out in the correct timezone. It isn’t rocket science but it is a lot more convoluted than higher level languages like Java.
First of lets see an example of the ISO 8601 format:
2006-02-03T16:45:09.000Z
That breaks down into:
<date>T<time><timezone>
Where a timezone of Z is equal to UTC. I was only interested in parsing timestamps in UTC so the following only applies to that timezone.
If you want to know any more about the format check out this page.
The strptime function is a flexible way to turn a string into a struct tm given a specified format. It is like a sscanf for dates. For more information on it check here. I’m not exactly sure of the portability of this function but it seems to be fairly old now so it is probably reasonably portable.
The format used to parse the a ISO 8601 timestamp is: %FT%T%z