c++ - 如何使用 boost::date_time 在本地时间和世界时间之间进行转换?

标签 c++ boost timezone

如何使用 current 使用 boost::date_time 在本地时间和 UTC 时间之间转换(特别是,从本地时间到 UTC)系统时区?我知道 boost::date_time::local_adjustor,但它需要一个模板参数,它是一个依赖于时区的偏移量。

Failing platform-independent way要做到这一点,我将如何专门在 Linux 上做到这一点?

顺便说一句,在转换过程中如何处理不存在的时间点?例如,如果由于夏令时,一天少了一小时,我尝试从缺少的小时转换一个时间点,那么最终的世界时是多少?

最佳答案

我正在使用以下代码查找本地时间和 UTC 时间之间的差异:


    using namespace boost::posix_time;
    using namespace boost::gregorian;

    time_duration UTC_Diff;
    {
        ptime someUTC_Time(date(2008, Jan, 1), time_duration(0, 0, 0, 0));
        ptime someLocalTime = boost::date_time::c_local_adjustor::utc_to_local(someUTC_Time);
        UTC_Diff = someLocalTime - someUTC_Time;
    }

既然你发现了不同,就不容易计算出 UTC 时间。

关于c++ - 如何使用 boost::date_time 在本地时间和世界时间之间进行转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2294166/

相关文章:

c++ - 错误 C2182 : '*' : illegal use of type 'void' c:\boost_1_53_0\boost\smart_ptr\intrusive_ptr. hpp

c++ - boost::container::vector 和 std::vector 有什么区别

mysql - 在不访问时区表的情况下转换 MySQL 中的时区

java - 获取 EST 时区的 XMLGregorianCalendar 日期

c++ - 由于eof,在C++中读取文件两次?

c++ - 比较两个整数数组并检查它们是否具有相等的值

c++ - 如何在 C++ 中测试条件变量?

c++ - QProcess,无法创建管道

c++ - C++中Boost Bimap的使用

java - 可与 TimeZone.getTimeZone API 一起使用的 ID 列表在哪里?