我正在使用以下代码获取当前日期时间(山地时间)
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/