【问题标题】:Convert Boost Ptime To EST UTC-5:00将 Boost Ptime 转换为 EST UTC-5:00
【发布时间】:2013-04-09 01:32:14
【问题描述】:

我正在使用下面的代码来获取当前日期时间(山地时间)

const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();

    //In mountain time I get now = 2013-Apr-08 20:44:22

现在我使用下面的方法进行转换

ptime FeedConnector::MountaintToEasternConversion(ptime coloTime) 
{

      return boost::date_time::local_adjustor <ptime, -5, us_dst>::utc_to_local(coloTime);
} 

//这个函数假设给我纽约的时间(东部标准时间),我得到了

2013-Apr-08 16:44:22

这个时间是错误的任何建议我哪里出错了?

【问题讨论】:

  • 我也想知道答案。

标签: c++ boost boost-date-time


【解决方案1】:

据我了解wrong time 表示与预期相差一小时,即 -4 小时而不是预期的 -5 小时。如果是,那么问题在于us_std 类型被指向为local_adjustor 声明的最后一个参数。如果指定no_dst 而不是use_dst。该代码按说明工作,差异为 -5 小时。下面的代码演示了它(link to online compiled version

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/local_time_adjustor.hpp>
#include <iostream>

int main(void) {
   const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
   const boost::posix_time::ptime adjUSDST = boost::date_time::local_adjustor<boost::posix_time::ptime, -5, boost::posix_time::us_dst>::utc_to_local(now);
   const boost::posix_time::ptime adjNODST = boost::date_time::local_adjustor<boost::posix_time::ptime, -5, boost::posix_time::no_dst>::utc_to_local(now);
   std::cout << "now: " << now << std::endl;
   std::cout << "adjUSDST: " << adjUSDST << std::endl;
   std::cout << "adjNODST: " << adjNODST << std::endl;
   return 0;
}

【讨论】:

    猜你喜欢
    • 2011-10-20
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2011-05-26
    • 2016-04-11
    • 1970-01-01
    相关资源
    最近更新 更多