C++ 如何更改C++;boost posix_time::ptime';秒的分辨率是多少?

C++ 如何更改C++;boost posix_time::ptime';秒的分辨率是多少?,c++,boost,resolution,datetime-format,C++,Boost,Resolution,Datetime Format,我正在尝试以“YYYY-MM-DD hh:MM:ss.fff”格式打印boost::posix_time::ptime,其中MM是2位数的月份,fff是3位数的毫秒。例如:2011-07-14 22:38:40.123 我正在使用Boost 1.33.1 我尝试调用API“ptime::to_simple_string()”,但它不能满足我的需要:此方法以“YYYY-MMM-DD hh:mm:ss.ffffffff”格式打印ptime,其中“MMM”是3个字符的月名,如“Jan”,而“fffff

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

我正在使用Boost 1.33.1

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

然后我尝试使用时间方面的东西:

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;
ptime my_time(秒时钟::本地时间());
时间面*我的时间面=新时间面();
std::cout.imbue(std::locale(std::locale::classic(),my_time_facet));

std::cout简单实用的解决方案是使用
%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();
}
//微秒钟 // #包括“boost/date\u time/posix\u time/posix\u time.hpp” #包括“增压/日期\时间/本地\时间\调节器.hpp” #包括“增压/日期\时间/c\本地\时间\调节器.hpp” #包括 //////////////////////////////////////////////////////////////////////////////// 空干管() { boost::posix_time::ptime my_time(boost::posix_time::microsec_clock::local_time()); boost::date\u time::time\u facet*my\u time\u facet=新的boost::date\u time::time\u facet(); std::cout.imbue(std::locale(std::locale::classic(),my_time_facet));
std::cout这将截断小数秒

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

出于好奇,你为什么要使用一个将近6年的Boost版本?@ildjarn:哦,因为1.33.1版本已经在我手里了,所以我现在不想麻烦升级到最新版本。这提醒我:在最新的Boost中是否有一个很好的解决方案来解决我的问题?嗯……我会去看看文档……当前版本DoTeTimes的默认值微秒级,这应该比你的需求更合适。但是,C++中的6年是很古老的。升级。是的,我以前曾考虑过这个问题:但是我认为应该有更好的方法直接输出我需要的格式。我会在这里继续搜索文档。@rob,定义“更好”。似乎有一种方法可以编写您自己的输出转换函数,但对于这样一个简单的问题来说似乎有些过分。同意。经过进一步研究,我认为“剥离”方法应该是最简单的:)Soory,这个答案没有给出任何解决方案……将输出微秒……而不是毫秒。
2015-10-27 1:2:7:567899

01:02:07:567