我使用以下代码来获取当前日期时间(Mountain 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);
} //此函数假定为我提供以NewYork (东部标准时间)表示的时间,我将得到
2013-Apr-08 16:44:22这个时间是错的,有什么建议我哪里错了吗?
发布于 2013-04-09 15:32:54
据我所知,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;
}https://stackoverflow.com/questions/15891845
复制相似问题