I am trying to print boost :: posix_time :: ptime in the format "YYYY-MM-DD hh: mm: ss.fff", where MM is a 2-digit month and fff a 3-digit milliseconds . Example: 2011-07 -14 22:38:40. 123 .
I am using Boost 1.33.1 .
I tried calling the API "ptime :: to_simple_string ()", but this does not meet my need: this method prints ptime in the format "YYYY-MMM-DD hh: mm: ss.ffffff", where "MMM" is the name of the month, for example, "Jan" and "ffffff" is a six-digit microsecond. This is NOT what I want.
Then I tried using the time_facet function:
ptime my_time(second_clock::local_time()); time_facet<ptime, char> * my_time_facet = new time_facet<ptime, char>(); std::cout.imbue(std::locale(std::locale::classic(), my_time_facet)); std::cout << my_time;
But I got an output like "2011-07-14 22: 38: 40.123000". This is NOT what I want.
It seems that ptime uses the microsecond as the default temporary resolution. But I just need a resolution in milliseconds. After some study, I think there can be two methods to solve this problem :
1). Somehow I can change the ptime time resolution to use the millisecond. I found some enumeration and template classes related to temporal resolution, but I don't know how to use them.
2). Somehow I can change my time facet format to print in just a millisecond. But the official documentation seems to say that â% Fâ and â% fâ use microsecond resolution.
I prefer the first solution, but I need help! How can I get the ptime string in the desired format?
PS:
1). Although I tried to find a similar question on this site, but I did not find it. If you know this, tell me.
2). The Boost documentation 1.33.1 Date Time (http://www.boost.org/doc/libs/1_33_1/doc/html/date_time.html) is the only link I read.
Update as of July 15, 2011:
After some further study, my conclusion is this:
1). You can change the time resolution to milliseconds, so using "% f" in time_facet only milliseconds will be displayed instead of the default microseconds. However, you seem to need to define a whole set of your own classes, including your_time_res_traits, your_time_duration, your_time_system_config, your_time_system and your_time. This is definitely too complicated for my problem.
2). So, I would suggest Mark (see below) just delete the last 3 characters after converting ptime to microsecond to string.