【问题标题】:Boost Date time conversion and adding seconds提升日期时间转换并添加秒
【发布时间】:2013-11-19 14:02:03
【问题描述】:

我需要加载格式为 16/11/2012 11:00:00 的字符串,即 %d/%m/%Y %H:%M,然后重复添加 900 秒。下面的代码是一个例子,你能帮我让它工作吗?

    #include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>

#include "boost/date_time/local_time/local_date_time.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
  unsigned int seconds=900;
  using namespace boost::local_time;
  std::stringstream ss;

  // Set up the input datetime format.
  local_time_input_facet *input_facet = new local_time_input_facet("%d/%m/%Y %H:%M");
  ss.imbue(std::locale(ss.getloc(), input_facet));

  local_date_time date(not_a_date_time);
  ss.str("16/11/2012 11:00:00");
  ss >>date;

  local_date_time now=date+seconds;
  std::cout << now << std::endl;
  return 0;
}

任何帮助将不胜感激, 詹姆斯

【问题讨论】:

  • 如果您需要精度并且您的架构支持闰秒,则很危险。改为添加分钟。

标签: c++ boost


【解决方案1】:

我现在手头没有提升,请尝试以下方法:

date + boost::posix_time::seconds(900)

【讨论】:

  • 好的,太好了,谢谢。我想我仍然缺少一些标题; local_time_input_facet 未定义。
  • 您好,我包含了#include "boost/date_time/time_facet.hpp" 但在 local_time_input_facet 方面仍然存在问题
猜你喜欢
  • 1970-01-01
  • 2011-05-22
  • 2017-10-10
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多