I'm trying to print a boost::posix_time::ptime in the format of "YYYY-MM-DD hh:mm:ss.fff" where the MM is a 2-digit month and fff a 3-digit millisecond. An example is: 2011-07-14 22:38:40.123.
I'm using Boost 1.33.1.
I tried to call the API "ptime::to_simple_string()" but it doesn't meet my need: this method prints the ptime in a format of "YYYY-MMM-DD hh:mm:ss.ffffff" where the "MMM" is a 3-char month name, such as "Jan", and "ffffff" is a 6-digit microsecond. This is NOT what I want.
I then tried to use the time_facet thing:
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, either.
Looks like the ptime uses the microsecond as the time resolution by default. But I just need a millisecond resolution. After some study I think there could be two methods to solve this problem:
1). Somehow I can change the ptime's time resolution to use millisecond. I've found some enumation and template classes related to the time resolution but I don't know how to use them.
2). Somehow I can change my time facet format to print out just the millisecond. But the official documentation seems to say "%F" and "%f" both use the microsecond resolution.
I tend to prefer the first solution but I need help! How can I get a ptime string in the format I desire?
PS:
1). Though I've tried to search a similar question in this website but I don't find one. If you know it please tell me.
2). The Boost 1.33.1 Date Time documentation(http://www.boost.org/doc/libs/1_33_1/doc/html/date_time.html) is the only reference I've been reading.
Update on 07/15/2011:
After some further study, my conclusion is as follows:
1). It is possible to change the time resolution to millisecond so using the "%f" in a time_facet will print out just the milliseconds, instead of the default microseconds. However, you seems to need to define a whole suite 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). Thus I'd take Mark's suggestion(see below) to just strip out the last 3 chars after converting a microsecond-resolution ptime to a string.
The simple, pragmatic solution would be to use %f
and always strip the last 3 characters from the result.
//
// microsec_clock
//
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time_adjustor.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
#include <iostream>
////////////////////////////////////////////////////////////////////////////////
void main ()
{
boost::posix_time::ptime my_time(boost::posix_time::microsec_clock::local_time());
boost::date_time::time_facet<boost::posix_time::ptime, char> * my_time_facet = new boost::date_time::time_facet<boost::posix_time::ptime, char>();
std::cout.imbue(std::locale(std::locale::classic(), my_time_facet));
std::cout << my_time;
getchar();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With