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++支援 time_zone 從 Visual Studio 2019 16.10 版開始的 類別。 類別 time_zone 是 C++20 功能。 必須有/std:c++latest編譯程式選項。
成員
公用成員函式和函式範本
| 名稱 | 描述 |
|---|---|
get_info |
sys_info取得這個time_zone或 local_info 。 |
name |
取得這個 time_zone 的名稱。 |
to_local |
轉換 sys_time local_time 成這個 time_zone中的 。 |
to_sys |
轉換 local_time sys_time 成這個 time_zone中的 。 |
需求
標頭: <chrono>
時區數據僅適用於 Windows 10 版本 1903/19H1 和更新版本,以及 Windows Server 2022 和更新版本。
命名空間:std::chrono
get_info
函式範本 get_info 有兩個多載,可取得 sys_info 或 local_info 這個 time_zone。
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
duration或 local_time 參數的 sys_time 類別。
st
sys_time用來取得sys_info結果的時間點。
lt
local_time用來取得local_info結果的時間點。
傳回值
在需要時間點的函式範本中get_info,它會傳sys_info回範圍 中的[i.begin, i.end)物件sti。stsys_time
在需要local_time時間點lt的函式範本中get_info,它會傳local_info回 物件。
備註
Microsoft C++支援 time_zone::get_info 從 Visual Studio 2019 16.10 版開始。 函式是需要 /std:c++latest 編譯程式選項的 C++20 功能。
name
取得這個 time_zone 的名稱。
string_view name() const noexcept; // Since C++20
傳回值
以傳回時區 string_view的名稱。
備註
Microsoft C++支援 time_zone::name 從 Visual Studio 2019 16.10 版開始。
to_local
函式樣本to_local會轉換成sys_timelocal_time這個 time_zone中的 。
template<class Duration>
local_time<common_type_t<Duration, seconds>>
to_local(const sys_time<Duration>& st) const; // Since C++20
參數
Duration
duration或 local_time 參數的 sys_time 類別。
st
sys_time用來取得sys_info結果的時間點。
傳回值
to_local傳local_time回這個 time_zone中與 st 相關聯的 。
備註
Microsoft C++支援 time_zone::to_local 從 Visual Studio 2019 16.10 版開始。 函式是需要 /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有兩個多載,可將 轉換成local_timesys_time這個 time_zone中的 。
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
參數 duration 的 local_time 類別。
lt
要 local_time 轉換的時間點。
z
或choose::latest的值choose::earliest。 它用來解析否則模棱兩可的結果。
傳回值
to_sys 傳 sys_time 回至少和 一樣 seconds精細的 。 如果自變數 lt 具有更精細的精確度,則會更精細。 傳回 sys_time 的 會根據這個time_zone的規則,相當於 lt 的UTC。
如果從 lt 轉換為 sys_time 的 轉換模棱兩可,則單參數多載會ambiguous_local_time擲回例外狀況,如果nonexistent_local_time本機時間點代表不存在的本機時間點,則會擲回例外狀況。 在日光節約時間到標準時間轉換期間,可能會發生模棱兩可的情況。 同一個當地時間點的兩個實例可能會在一天內發生。 不存在的當地時間點代表從標準時間轉換為日光節約時間的時間點。
在這些情況下,雙參數多載不會擲回例外狀況。 如果 從 lt 轉換成 模棱兩可,則如果 為 ,to_sys則會傳回先前zsys_time的 ,如果 為 choose::earliest,則會傳回稍後sys_time的 choose::latest。z sys_time lt如果表示兩個 UTC 時間點之間不存在的時間,則兩個 UTC 時間點相同,因此to_sys會傳回該 UTC 時間點。
備註
Microsoft C++支援 time_zone::to_sys 從 Visual Studio 2019 16.10 版開始。 函式是需要 /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