2012-07-17 33 views
0

我希望C++能夠在GMT時間和任何時區之間獲得小時分鐘。 例如 這是Java我想在C++如何在格林威治標準時間和C++中的任何時區之間獲得分歧

// New York 
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); 

// Alaska 
c = new GregorianCalendar(TimeZone.getTimeZone("America/Anchorage")); 
// Difference between New York and Alaska 

請告訴我,我怎麼能在C++中得到這個時區

+0

現在你們都可以很容易理解 – 2012-07-17 11:56:59

+0

使用boost:http://www.boost.org/doc/libs/1_50_0/doc/html/date_time/examples.html#date_time.examples.simple_time_zone – Nim 2012-07-17 12:06:28

+0

準確地說明您的操作系統是什麼,或者如果您想使用便攜式庫(例如提升) – 2012-07-17 13:42:38

回答

1

可以使用cctz庫來計算兩個不同之間的偏移在UTC的差異在某些特定時間的時區。現在

#include <chrono> 
#include "cctz.h" 
using namespace std::chrono; 

cctz::TimeZone nyc; 
cctz::LoadTimeZone("America/New_York", &nyc); 

cctz::TimeZone anc; 
cctz::LoadTimeZone("America/Anchorage", &anc); 

const auto now = system_clock::now(); 
const auto nyc_off = cctz::BreakTime(now, nyc).offset; 
const auto anc_off = cctz::BreakTime(now, anc).offset; 

const auto off_diff = nyc_off - anc_off; 

,事實是,你真的不希望這樣做。真。健康,現代的代碼應該永遠不會(我從來沒有說過,因爲我的意思是從來沒有)關心UTC偏移量。 UTC時差計算是時區庫的工作,如果您的時區庫不能爲您處理,那麼您使用的是錯誤的時區庫。如果你覺得你在乎UTC偏移,那麼我鼓勵你看看下面:

  • 閱讀CCTZ的基本概念部分的GitHub的頁面
  • 觀看Time Programming Fundamentals從2015年CppCon
  • 閱讀cctz.h頭文件(這是短期和簡單)

[免責聲明:我cctz的作者]

1

我在Greg's good answer一直盯着一個幾天了,和我考慮加入一些語法糖my timezone library

namespace date 
{ 

class zoneverter 
{ 
    const Zone* zp1_; 
    const Zone* zp2_; 

public: 
    zoneverter(const Zone* z1, const Zone* z2) 
     : zp1_(z1) 
     , zp2_(z2) 
     {} 

    zoneverter(const Zone* z1, const std::string& z2) 
     : zoneverter(z1, locate_zone(z2)) 
     {} 

    zoneverter(const std::string& z1, const Zone* z2) 
     : zoneverter(locate_zone(z1), z2) 
     {} 

    zoneverter(const std::string& z1, const std::string& z2) 
     : zoneverter(locate_zone(z1), locate_zone(z2)) 
     {} 

    template <class Rep, class Period> 
    auto 
    operator<<(std::chrono::time_point<std::chrono::system_clock, 
             std::chrono::duration<Rep, Period>> tp) const 
    { 
     return zp1_->to_local(zp2_->to_sys(tp)).first; 
    } 
}; 

} // namespace date 

這增加了一個「流狀物體」,它允許人們通過它傳輸一個std::chrono::time_point將其從一個時區轉換爲另一種。這是一個非常簡單的設備,除了添加一些語法糖之外,其他任何東西都不會丟失一些信息,從my timezone library

它會像這樣使用:

int 
main() 
{ 
    // So things don't get overly verbose 
    using namespace date; 
    using namespace std::chrono; 

    // Set up the zone converters: 
    zoneverter nyc_from_utc{"America/New_York", "UTC"}; 
    zoneverter anc_from_nyc{"America/Anchorage", "America/New_York"}; 

    // Get the current time in New York and convert that to the current time in Anchorage 
    auto now_nyc = nyc_from_utc << system_clock::now(); 
    auto now_anc = anc_from_nyc << now_nyc; 

    // Output the difference 
    std::cout << make_time(now_nyc - now_anc) << '\n'; 
} 

這目前輸出對我來說:

04:00:00.000000

我也不能確定這是否語法糖是不是足夠好目前的語法來保證它的存在:

int 
main() 
{ 
    // So things don't get overly verbose 
    using namespace date; 
    using namespace std::chrono; 

    // Set up the zones: 
    auto nyc_zone = locate_zone("America/New_York"); 
    auto anc_zone = locate_zone("America/Anchorage"); 

    // Get the current time in New York and the current time in Anchorage 
    auto now_utc = system_clock::now(); 
    auto now_nyc = nyc_zone->to_local(now_utc).first; 
    auto now_anc = anc_zone->to_local(now_utc).first; 

    // Output the difference 
    std::cout << make_time(now_nyc - now_anc) << '\n'; 
} 
相關問題