0

C++ で、GMT 時間と任意のタイム ゾーンの時間差を取得するためのソリューションが必要です。例 これは Java で 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++でこのタイムゾーンを取得する方法を教えてください

4

2 に答える 2

2

cctzライブラリを使用して、特定の時間における 2 つの異なるタイム ゾーン間の 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 オフセットが気になる場合は、次のことを確認することをお勧めします。

  • github ページの CCTZ の基本概念セクションを読む
  • CppCon 2015 の視聴時間プログラミングの基礎
  • cctz.h ヘッダー ファイルを読み取ります (短くて簡単です)。

[免責事項: 私は cctz の作成者です。]

于 2015-10-26T13:29:37.493 に答える
1

私はグレッグの良い答えを数日間見つめてきましたが、タイムゾーンライブラリに構文シュガーを追加することを考えています:

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れ、それを介してストリーミングして、あるタイムゾーンから別のタイムゾーンに変換できるようになります。これは非常に単純なデバイスであり、タイムゾーン ライブラリからいくつかの情報を削除することを犠牲にして、構文シュガーを追加するだけです。

次のように使用されます。

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';
}
于 2015-11-02T01:18:33.773 に答える