2012-07-17 88 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'; 
} 
相关问题