Share via


time_zone クラス

time_zone は、特定の地域のタイム ゾーンを表します。 ここには、各領域のタイム ゾーン ルールの完全な履歴が含まれています。そのため、タイム ゾーンのルールが現在のルールとは異なる時点の日付に遡って時刻を変換した場合に、時間の変換が正確になります。

構文

class time_zone;  // Since C++20

解説

<chrono> ライブラリは、タイム ゾーン データベースの初期化の一部として time_zone オブジェクトを作成します。 作成されたオブジェクトへの const アクセスを提供します。

time_zone オブジェクトを構築またはコピーすることはできません。既定の移動コンストラクターまたは既定の移動代入演算子を使用すると、未定義の動作が発生します。

time_zone インスタンスを取得する方法を次に示します。

const auto& timeZoneDatabase = get_tzdb(); // initialize the time zone database
const auto& currentZone = timeZoneDatabase.current_zone();

Microsoft C++ では、Visual Studio 2019 バージョン 16.10 以降の time_zone クラスをサポートしています。 time_zone クラスは C++20 機能です。 /std:c++latest コンパイラ オプションが必要です。

メンバー

パブリック メンバー関数と関数テンプレート

名前 説明
get_info この time_zonesys_info または local_info を取得します。
name この time_zone の名前を取得します。
to_local この time_zonesys_timelocal_time に変換します。
to_sys この time_zonelocal_timesys_time に変換します。

必要条件

ヘッダー:<chrono>

タイム ゾーン データは、Windows 10 バージョン 1903/19H1 以降および Windows Server 2022 以降でのみ使用できます。

名前空間std::chrono:

get_info

関数テンプレート get_info には、この time_zone に対して sys_info または local_info を取得する 2 つのオーバーロードがあります。

template<class Duration>
sys_info get_info(const sys_time<Duration>& st) const;  // Since C++20
    
template<class Duration>
local_info get_info(const local_time<Duration>& lt) const;  // Since C++20

パラメーター

Duration
sys_time パラメーターまたは local_time パラメーターの duration クラス。

st
sys_info の結果を取得するために使用される sys_time 時間ポイント。

lt
local_info の結果を取得するために使用される local_time 時間ポイント。

戻り値

sys_time 時間ポイント st を受け取る get_info 関数テンプレートでは、st が範囲 [i.begin, i.end) 内になるように sys_info オブジェクト i が返されます。

local_time 時間ポイント lt を受け取る get_info 関数テンプレートでは、local_info オブジェクトが返されます。

解説

Microsoft C++ では、Visual Studio 2019 バージョン 16.10 以降の time_zone::get_info をサポートしています。 関数は、/std:c++latest コンパイラ オプションを必要とする C++20 機能です。

name

この time_zone の名前を取得します。

string_view name() const noexcept;  // Since C++20

戻り値

タイム ゾーンの名前を string_view として返します。

解説

Microsoft C++ では、Visual Studio 2019 バージョン 16.10 以降の time_zone::name をサポートしています。

to_local

関数テンプレート to_local は、この time_zonesys_timelocal_time に変換します。

template<class Duration>
local_time<common_type_t<Duration, seconds>>
to_local(const sys_time<Duration>& st) const;  // Since C++20

パラメーター

Duration
sys_time パラメーターまたは local_time パラメーターの duration クラス。

st
sys_info の結果を取得するために使用される sys_time 時間ポイント。

戻り値

to_local は、この time_zonest に関連付けられている local_time を返します。

解説

Microsoft C++ では、Visual Studio 2019 バージョン 16.10 以降の time_zone::to_local をサポートしています。 関数は、/std:c++latest コンパイラ オプションを必要とする C++20 機能です。

例: sys_timelocal_time に変換

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    const auto& timeZoneDatabase = get_tzdb();
    const auto& currentZone = timeZoneDatabase.current_zone();
    local_time<system_clock::duration> lt = currentZone->to_local(system_clock::now());

    std::cout << "local_time: " << lt << "\n";
   
    return 0;
}
local_time: 2021-09-08 15:15:53.1830646

to_sys

関数テンプレート to_sys には、この time_zonelocal_timesys_time に変換する 2 つのオーバーロードがあります。

template<class Duration>
sys_time<common_type_t<Duration, seconds>>
to_sys(const local_time<Duration>& lt) const;  // Since C++20

template<class Duration>
sys_time<common_type_t<Duration, seconds>>
to_sys(const local_time<Duration>& lt, choose z) const;  // Since C++20

パラメーター

Duration
local_time パラメーターの duration クラス。

lt
変換する local_time 時間ポイント。

z
または choose::earliestchoose::latest 値。 これは、あいまいな結果を解決するために使用されます。

戻り値

to_sys は、少なくとも seconds と同程度に詳細な sys_time を返します。 引数 lt の精度がより高い場合は、より詳細になります。 返される sys_time は、この time_zone の規則に従った lt の UTC 相当です。

lt から sys_time への変換があいまいである場合、1 つのパラメーターのオーバーロードは ambiguous_local_time 例外をスローし、ローカルの時間ポイントが存在しないローカル時間ポイントを表している場合は nonexistent_local_time 例外をスローします。 あいまいさは、夏時間から標準時への移行中に発生する可能性があります。 同じローカル時間ポイントの 2 つのインスタンスが 1 日に発生する場合があります。 存在しないローカル時間ポイントは、標準時間から夏時間への移行の時間ポイントを表します。

このような場合、2 つのパラメーターをオーバーロードしても例外はスローされません。 lt から sys_time への変換があいまいである場合、to_sys は、zchoose::earliest の場合は前の sys_time を返し、zchoose::latest の場合は後の sys_time を返します。 lt が 2 つの UTC 時間ポイント間の存在しない時刻を表している場合、2 つの UTC の時間ポイントは同じであるため、to_sys はその UTC 時間ポイントを返します。

解説

Microsoft C++ では、Visual Studio 2019 バージョン 16.10 以降の time_zone::to_sys をサポートしています。 関数は、/std:c++latest コンパイラ オプションを必要とする C++20 機能です。

例: local_timesys_time に変換

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    const auto& timeZoneDatabase = get_tzdb();
    const auto& currentZone = timeZoneDatabase.current_zone();

    auto st = currentZone->to_sys(local_days{2021y/September/15d}+16h+45min, choose::earliest);

    std::cout << "sys_time: " << st << "\n";
   
    return 0;
}
sys_time: 2021-09-15 23:45:00.0000000

関連項目

<chrono>
ヘッダー ファイル リファレンス