How to change C ++ acceleration posix_time :: ptime to milliseconds?

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.

+6
source share
3 answers

A simple pragmatic solution would be to use %f and always separate the last 3 characters from the result.

+5
source
 // // 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(); } 
+1
source

This will reduce fractional seconds.

 #include <iostream> #include <iomanip> #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp> int main() { boost::gregorian::date dayte(boost::gregorian::day_clock::local_day()); boost::posix_time::ptime midnight(dayte); boost::posix_time::ptime now(boost::posix_time::microsec_clock::local_time()); // boost::posix_time::time_duration td = now - midnight; boost::posix_time::time_duration td(1,2,3,4567899); std::stringstream sstream; sstream << td.fractional_seconds(); std::string trunc = std::string(sstream.str()).substr(0,3); std::cout << dayte.year() << "-" << dayte.month().as_number() << "-" << dayte.day() << " "; std::cout << td.hours() << ":" << td.minutes() << ":" << td.seconds() << ":" << td.fractional_seconds() << std::endl; std::cout << std::endl; std::cout << std::setfill('0') << std::setw(2) << td.hours() << ":"; std::cout << std::setfill('0') << std::setw(2) << td.minutes() << ":"; std::cout << std::setfill('0') << std::setw(2) << td.seconds() << ":"; std::cout << trunc << std::endl; } 

results

 2015-10-27 1:2:7:567899 01:02:07:567 
0
source

Source: https://habr.com/ru/post/892759/


All Articles