2011-08-23 78 views
0

我想將一個boost::posix_time::ptime轉換爲boost::local_time::local_date_time使用時區,而不指定夏令時時間,其偏移量爲UTC。我正在使用boost 1.47.0在沒有夏令時的情況下創建boost :: local_time :: custom_time_zone

當前我正在嘗試創建一個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參數呢?或者還有另一類更適合我的需求,因爲我還提供zone_names作爲boost::local_time::time_zone_names用4個空字符串初始化。

回答

1

日期時間文檔中的示例顯示如何創建不含DST的自定義時區。你可以在http://www.boost.org/doc/libs/1_47_0/doc/html/date_time/examples.html#date_time.examples.simple_time_zone看到它。

看看phx_1時區的代碼。你基本上傳入了一個dst_adjustment_offsets,初始化爲time_duration(0,0,0)(第三個參數)的三個副本,並傳遞一個空的共享指針作爲計算規則(第四個參數)。

(從例子:)

// 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)); 
+0

哎,這正是我想出了自己,我可能已經寫在了問題。但我認爲,必須有一個不那麼多餘的方式來做到這一點。謝謝! – Feuermurmel

+0

@Feuermurmel:你可以從custom_time_zone派生你自己的類,它會自動完成大部分工作,至少你不需要用冗餘偏移量,空規則和空字符串來污染你的所有代碼。但我想你會做到這一點。 – tinman

相關問題