2013-04-09 34 views
2

我用下面的代碼獲取當前日期時間(山地時間)轉換加速的ptime要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 

Thsi時間是錯誤的任何建議我哪裏出錯了?

+0

我想知道答案。 – user805547 2013-04-09 02:45:44

回答

0

據我瞭解wrong time意味着它有一小時的預期差異,即-4小時,而不是預期的-5小時。如果是,那麼問題是us_std類型被指定爲local_adjustor聲明的最後一個參數。如果指定no_dst而不是use_dst。該代碼作爲詳細闡述,差異是-5小時。下面的代碼演示它(link to online compiled version

#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; 
}