如何将C++提升posix_time::p时间的分辨率更改为毫秒?

How to change the C++ boost posix_time::ptime's resolution to millisecond?

本文关键字:分辨率 时间 C++ 提升 posix time      更新时间:2023-10-16

我试图以"YYYY-MM-DD hh:mm:ss "的格式打印boost::posix_time::ptime。fff",其中表示MM为2位数的月份,fff为3位数的毫秒。07年就是一个例子:2011 - -14 22:38:40。 123

我使用Boost 1.33.1

我尝试调用API"ptime::to_simple_string()",但它不满足我的需要:此方法以"yyyy - mm - dd hh:mm:ss"的格式打印ptime。其中"MMM"是一个3个字符的月名,例如"Jan","ffffff"是一个6位的微秒。这不是我想要的。

然后我尝试使用time_facet的东西:

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;

但是我得到了像"2011-07-14 22:38:40.123000"这样的输出。这也不是我想要的。

看起来ptime默认使用微秒作为时间分辨率。但是我只需要毫秒级的分辨率。经过一番研究我认为有两种方法可以解决这个问题:

1)。不知何故,我可以改变ptime的时间分辨率使用毫秒。我发现了一些与时间分辨率相关的枚举和模板类,但我不知道如何使用它们。

2)。不知何故,我可以改变我的时间面格式打印出毫秒。但是官方文档似乎说"%F"answers"%F"都使用微秒分辨率。

我倾向于第一个解决方案,但我需要帮助!我怎样才能得到一个ptime字符串在我想要的格式?

PS:

1)。虽然我试着在这个网站上搜索类似的问题,但我没有找到一个。如果你知道,请告诉我。

2)。Boost 1.33.1 Date Time文档(http://www.boost.org/doc/libs/1_33_1/doc/html/date_time.html)是我一直在阅读的唯一参考。


2011年7月15日更新:

经过进一步的研究,我的结论如下:

1)。可以将时间分辨率更改为毫秒级,这样在time_facet中使用"%f"将只打印毫秒级,而不是默认的微秒级。然而,你似乎需要定义一整套你自己的类,包括your_time_res_traits, your_time_duration, your_time_system_config, your_time_system和your_time。这对我的问题来说太复杂了。

2)。因此,我将采纳Mark的建议(见下文),在将微秒分辨率的ptime转换为字符串后,去掉最后3个字符。

简单实用的解决方案是使用%f并始终从结果中删除最后3个字符。

//
// 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();
}

这将截断小数秒。

#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;
}
结果

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