如何将C++提升posix_time::p时间的分辨率更改为毫秒?
How to change the C++ boost posix_time::ptime's resolution to millisecond?
我试图以"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
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- 日历时间,纳秒分辨率为纳秒纪元
- 高分辨率程序时间
- 如何获得高分辨率时间戳作为双精度
- 将高分辨率时钟时间转换为整数(Chrono)
- C++11如何打印出高分辨率时钟时间点
- 增加c++中的时间分辨率
- C++,时间函数(Linux)-纳秒分辨率
- 如何将C++提升posix_time::p时间的分辨率更改为毫秒?