【问题标题】:How to get the current Boost time zone for Windows?如何获取 Windows 的当前 Boost 时区?
【发布时间】:2018-06-06 12:41:31
【问题描述】:

这类似于How to get the current time zone?

#include <ctime>
tzset();
time_zone_ptr zone(new posix_time_zone(tzname[localtime(0)->tm_isdst]));

以上代码抛出“bad cast”异常。来自 tzname 或 GetTimeZoneInformation 的时区是 'China Standard Time' 之类的字符串,Boost 时区不接受。

有没有简单的方法来解决这样一个基本问题?

【问题讨论】:

    标签: c++ boost boost-date-time


    【解决方案1】:

    使用Howard Hinnant's time zone library,以下将在Windows上打印出当前IANA time zone名称:

    #include "date/tz.h"
    #include <iostream>
    
    int
    main()
    {
        std::cout << date::current_zone()->name() << '\n';
    }
    

    在 Windows 上需要一些 installation。对我来说,上面的程序输出:

    America/New_York
    

    implementation 使用 GetDynamicTimeZoneInformation 查找当前 Windows 时区,然后使用 this Unicode mapping 将本机 Windows 时区名称映射到 IANA time zone 名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多