将Boost Ptime转换为EST UTC-5:00
Convert Boost Ptime To EST UTC-5:00
我使用以下代码获取当前日期时间(山地时间)
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
这次错了,有什么建议吗?
据我所知,wrong time
意味着它与预期有一个小时的差异,即-4小时而不是预期的-5小时。如果是,那么问题是us_std
类型被指向local_adjustor
声明的最后一个参数。如果指定no_dst
而不是use_dst
。代码运行正常,误差为-5小时。下面的代码演示了它(链接到在线编译版本)
#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;
}
相关文章:
- Log4cpp:以UTC/GMT时区打印日期
- C++:floor unix时间戳到UTC月份
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1
- 提升,以分钟精度获得 UTC 时间
- 从ubuntu上的utc时间计算unix时间
- 为什么 std::p ut_time(std::gmtime 采用 %z 格式返回 +0100 表示 UTC?
- 如何使用闰秒以微秒为单位获取 UTC 时间
- 将具有 DST 的时间字符串转换为 UTC 时间戳
- 使用 std::chrono / date::gps_clock 将双 GPS 时间戳转换为 UTC/tai
- 高效生成 UTC 时间戳
- 如何在C++中以毫秒为单位获取今天的 UTC 午夜时间?
- 有没有办法在 C++ 中获取 uint64_t 日期的 UTC 纳秒
- C 自定义时间日期结构到UTC时期
- 如何将 UTC 时间戳转换为本地时间,即整点后的几秒钟
- 如何从 UTC 时间戳的组件(Y,M,D,H,M,S,MS)创建system_clock::time_point
- 无UTC时区中的正确时间总和
- Windows UTC时间夏令时是否独立
- 将 UTC 时差转换为字符串增强
- 输出boost :: chrono :: system_clock :: time_point作为UTC的本地时间
- 将Boost Ptime转换为EST UTC-5:00