c++ - 将 Boost Ptime 转换为 EST UTC-5 :00

标签 c++ boost boost-date-time

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

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

这一次是错误的任何建议我哪里出错了?

最佳答案

据我所知,错误时间 意味着它与预期时间相差一小时,即 -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;
}

关于c++ - 将 Boost Ptime 转换为 EST UTC-5 :00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15891845/

相关文章:

c++ - OpenCV Mat.row() 的迭代器是否不适用于 stdlib 算法?

java - Opencv JNA&C++ FaceRecognizer导致JVM崩溃

c++ - 如何使函数返回指向函数的指针? (C++)

c++ - Boost.Log中的参数传递错误

c - 来自 C 的 Boost.DateTime?

c++ - rpc server 返回结果 char 会得到 segment fault

c++ - 我可以从 divide_typeof_helper 检索基本单位吗?

c++ - boost::shared_ptr<string> 标准集

c++ - 从周数和年份开始的月份

boost - 从 IANA 的时区数据库生成 boost::date_time 的 date_time_zonespec.csv?