Parsing microseconds

I am looking for boost::posix_time::time_input_facet , which will allow me to parse milliseconds. This does not seem to be the same as for formatting microseconds, which is "%f"

So, if I have 2011-12-11 08:29:53.123000 , I would like to have the correct formatting to parse it, something like "%Y-%m-%d %H:%M:%S" .

+4
source share
1 answer

If you have a date / time string, you can convert it to a ptime object as follows:

 using boost::posix_time; ptime t = time_from_string(datetimeString); 

With this, you can easily get time_duration, which contains fractional seconds.

 time_duration td = t.time_of_day(); long fs = td.fractional_seconds(); 

You can also get total milliseconds or microseconds as follows:

 long ms = td.total_milliseconds(); long us = td.total_microseconds(); 

Read more about what you can do in the documentation .

UPDATE

If the input format may be different, so you want to use time_input_facet , you can check time_facet.hpp for the appropriate format. Here is what you probably want to choose from:

  static const char_type fractional_seconds_format[3]; // f static const char_type fractional_seconds_or_none_format[3]; // F static const char_type seconds_with_fractional_seconds_format[3]; // s 

UPDATE2

In time_facet.hpp (Boost 1.45) When analyzing, I see the following:

 case 'f': { // check for decimal, check special_values if missing if(*sitr == '.') { ++sitr; parse_frac_type(sitr, stream_end, frac); ... 

I do not understand why this will require anything but a point between seconds and fractional seconds. Maybe you are using a different version of Boost?

+3
source

All Articles