【问题标题】:How to change the C++ boost posix_time::ptime's resolution to millisecond?如何将 C++ boost posix_time::ptime 的分辨率更改为毫秒?
【发布时间】:2011-07-14 14:57:52
【问题描述】:

我正在尝试以“YYYY-MM-DD hh:mm:ss.fff”的格式打印 boost::posix_time::ptime,其中 MM 是 2 位数月份,fff a 3 位数毫秒。一个例子是:2011-07-14 22:38:40.123.

我正在使用 Boost 1.33.1

我尝试调用 API“ptime::to_simple_string()”,但它不能满足我的需要:此方法以“YYYY-MMM-DD hh:”的格式打印 ptime: mm:ss.ffffff” 其中“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”和“%f”都使用微秒分辨率。

我更喜欢第一种解决方案,但我需要帮助!如何获得所需格式的 ptime 字符串?

PS:

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

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

【问题讨论】:

  • 出于好奇,您为什么要使用近 6 年的 Boost 版本?
  • @ildjarn:哦,因为 1.33.1 版本已经在我手上,所以我现在不想麻烦升级到最新版本。这提醒了我:最新的 Boost 中有没有很好的解决方案来解决我的问题?嗯..我去看看文档..
  • 当前版本的 Boost.DateTime 默认具有微秒分辨率,这应该可以满足您的需求。但实际上,在 C++ 术语中,6 年是古老——升级。

标签: c++ boost resolution datetime-format


【解决方案1】:

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

【讨论】:

  • 是的,我之前想过这个:) 但是我认为应该有更好的方法直接以我需要的格式输出 ptime。我在这里等着,继续在文档中搜索。
  • @rob,定义“更好”。似乎有一种方法可以编写自己的输出转换函数,但对于这样一个简单的问题似乎有点过头了。
  • 同意。经过一些进一步的研究,我认为“条带”方式应该是最简单的:)
【解决方案2】:
//
// 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();
}

【讨论】:

  • Soory,这个答案没有给出任何解决方案......将输出微秒......而不是毫秒。
【解决方案3】:

这将截断小数秒。

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2011-05-26
    • 2017-10-13
    • 2011-10-24
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多