c++ - 创建没有夏令时的 boost::local_time::custom_time_zone

标签 c++ boost timezone

我想转换一个 boost::posix_time::ptimeboost::local_time::local_date_time使用没有夏令时的时区仅由它与 UTC 的偏移量指定。我正在使用 boost 1.47.0 .

目前我正在尝试创建一个 boost::local_time::custom_time_zone (又名 custom_time_zone_base<char> )。该类模板的唯一构造函数如下:

custom_time_zone_base(
    const time_zone_names& zone_names,   
    const time_duration_type& utc_offset,
    const dst_adjustment_offsets& dst_shift,
    boost::shared_ptr<dst_calc_rule> calc_rule);

我应该提供什么作为 dst_shiftcalc_rule争论?或者还有其他更适合我需要的类(class)吗,因为我还提供了 zone_names作为boost::local_time::time_zone_names用 4 个空字符串初始化。

最佳答案

日期时间文档中的示例向您展示了如何创建没有 DST 的自定义时区。你可以在http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/examples.html#date_time.examples.simple_time_zone看到它.

查看 phx_1 时区的代码。您基本上传递了一个初始化为 time_duration(0,0,0) 的三个拷贝的 dst_adjustment_offsets(第三个参数),并为计算规则(第四个参数)传递了一个空的共享指针。

(来自示例:)

// create more dependent objects for a non-dst custom_time_zone
time_zone_names tzn2("Mountain Standard Time", "MST", "", ""); // no dst means empty dst strings
time_duration utc_offset2(-7,0,0);
dst_adjustment_offsets adj_offsets2(time_duration(0,0,0), 
                                    time_duration(0,0,0), 
                                    time_duration(0,0,0));
// no dst means we need a null pointer to the rules
shared_ptr<dst_calc_rule> phx_rules;

// create the custom_time_zones
time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2, 
                                         adj_offsets2, phx_rules));

关于c++ - 创建没有夏令时的 boost::local_time::custom_time_zone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7161089/

相关文章:

C++ 计数动物和与基类的随机交互

c++ - boost::transform_iterator 和 boost::bind

c++ - Boost Asio - 使用 shared_ptr 处理解析器和套接字

html - 确定用户的时区

javascript - 创建具有设定时区的日期而不使用字符串表示

C++如何从函数循环动态参数?

c++ - 多重分派(dispatch)如何违反 C++ 中的封装

java - 像时区一样用 "America/New_York"解析日期

c++ - 为什么 const char[] 总是转换为 const char* ?

c++ - boost make_shared 接受一个 const 引用。有什么办法可以解决这个问题?