简体   繁体   English

如何将 C++ boost posix_time::ptime 的分辨率更改为毫秒?

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

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 .我正在尝试以 "YYYY-MM-DD hh:mm:ss.fff" 格式打印 boost::posix_time::ptime ,其中MM 是 2 位数的月份,fff 是 3 位数的毫秒 An example is: 2011- 07 -14 22:38:40.一个例子是:2011-07-14 22:38:40 123 . 123 .

I'm using Boost 1.33.1 .我正在使用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.我试图调用 API "ptime::to_simple_string()" 但它不能满足我的需要:此方法以 "YYYY-MMM-DD hh:mm:ss.ffffff" 的格式打印 ptime,其中 "MMM " 是一个 3 个字符的月份名称,例如“Jan”,而“ffffff”是一个 6 位数的微秒。 This is NOT what I want.这不是我想要的。

I then tried to use the time_facet thing:然后我尝试使用 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;

But I got an output like "2011-07-14 22:38:40.123000".但我得到了一个 output,例如“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.看起来 ptime 默认使用微秒作为时间分辨率。 But I just need a millisecond resolution.我只需要毫秒的分辨率。 After some study I think there could be two methods to solve this problem :经过一番研究,我认为可能有两种方法可以解决这个问题

1). 1)。 Somehow I can change the ptime's time resolution to use millisecond.不知何故,我可以将 ptime 的时间分辨率更改为使用毫秒。 I've found some enumation and template classes related to the time resolution but I don't know how to use them.我找到了一些与时间分辨率相关的枚举和模板类,但我不知道如何使用它们。

2). 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.但官方文档似乎说“%F”和“%f”都使用微秒分辨率。

I tend to prefer the first solution but I need help?我倾向于更喜欢第一个解决方案,但我需要帮助? How can I get a ptime string in the format I desire?如何获得所需格式的 ptime 字符串?

PS: PS:

1). 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). 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. Boost 1.33.1 日期时间文档(http://www.boost.org/doc/libs/1_33_1/doc/html/date_time.html)是我读过的唯一参考资料。


Update on 07/15/2011: 2011 年 7 月 15 日更新:

After some further study, my conclusion is as follows:经过进一步研究,我的结论如下:

1). 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.可以将时间分辨率更改为毫秒,因此在 time_facet 中使用“%f”将只打印出毫秒,而不是默认的微秒。 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.但是,您似乎需要定义一整套您自己的类,包括 your_time_res_traits、your_time_duration、your_time_system_config、your_time_system 和 your_time。 This is definitely too complicated for my problem.这对我的问题来说绝对太复杂了。

2). 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.因此,在将微秒分辨率的 ptime 转换为字符串之后,我会采纳 Mark 的建议(见下文)来去掉最后 3 个字符。

The simple, pragmatic solution would be to use %f and always strip the last 3 characters from the result.简单实用的解决方案是使用%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();
}

This will truncate the 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM