While doing the data/time parsing in c++ (converting a string in any format to a date), i found the following useful methods
1) strptime() - here the %d, %m etc can have either 1 or 2 characters. The function will take care of that. As a result of this it will enforce that we use a separator between two conversion specifiers. Ex: Its not valid to give %d%m it has be to %d/%m or any other separator. Also this does not support timezones.
2) Boost date IO - here the %d, %m has to have 2 characters. Now, the input string i get is not guaranteed to have this. As a result, its not possible to use this successfully. However, this does seem to support timezone, but not sure. because it says for inputs it does support timezone
So i am planning to use both in conjunction to determine the date. But i would like to get one where i can take into account the timezone as well. But none seems to be supporting that.
Does anybody have a suggestion? Rgds, AJ
As you noticed
strptime
doesn't directly support timezones. All it does is read formatted timezone-less data into atm
structure. You'll have to parse an optional timezone indicator yourself (unless it's fixed), and then setTZ
and use mktime to convert into atime_t
. If you settm.is_dst
to -1 you can even ask thatmktime
try to figure out the DST for you automatically.Alternately you could construct your own parser using steams (boost supports a few formats, but may not be general enough). Again like above you can use
mktime
to compose atime_t
.Depends on the libc version, I'd say, and of course what you mean by `taking into account': Is it supposed to read the additional info and then ignore it, is it supposed to store the parsed info in the tm struct, or is it meant to convert to system/environment time?
glibc 2.10.1's strptime() does time zones, from the info page
@AJ: Added another answer so the code gets formatted
And a run looks like (glibc 2.10.1):