共用方式為


nonexistent_local_time 類別

嘗試將 轉換成 local_time 不存在 sys_time的 時,會擲回這個例外狀況。

語法

class nonexistent_local_time : public runtime_error; // C++20

備註

在從標準時間到春季日光節約時間的轉換期間,時鐘基本上會失去一小時。 這可能會造成混淆,因為轉換到日光節約時間是否意味著增加一小時? 通過「向前跳動」一小時,轉換後的小時會有效移除。 請考慮紐約日光節約時間的變更,該時間發生在3月的第二個星期天淩晨2點。 上午 2 點,時鐘會轉換為日光節約時間,現在上午 3:00 讀取。 例如,如果轉換的 local_time 是上午 2:30,則該時間是在「移除」期間,因此為「不存在」。

下列範例示範不存在的時間轉換錯誤。 在此範例中,紐約的日光節約時間從上午 2:00 開始。 轉換的時間是上午 2:30。 那是在從標準時間轉換為日光節約時間而移除的一小時內。 因此, nonexistent_local_time 擲回例外狀況。

範例: nonexistent_local_time

#include <chrono>
#include <iostream>

using namespace std::chrono;
    
int main()
{
    try
    {
        // The following will throw an exception because the local time being converted is during
        // the hour that is "removed" when the clock advances an hour for daylight saving time.
        auto zt = zoned_time{"America/New_York", local_days{Sunday[2]/March/2016} + 2h + 30min};
    } catch (const nonexistent_local_time& e)
    {
        std::cout << e.what() << '\n';
    }
    return 0;
}
2016-03-13 02:30:00 is in a gap between
2016-03-13 02:00:00 EST and
2016-03-13 03:00:00 EDT which are both equivalent to
2016-03-13 07:00:00 UTC

成員

名稱 描述
建構函式 nonexistent_local_time建構 。
what 取得描述不存在時間的字串。

需求

標頭: <chrono> (自C++20起)

命名空間std::chrono

編譯程序選項: /std:c++latest

建構函式

建構 nonexistent_local_time

template<class Duration>
nonexistent_local_time(const local_time<Duration>& tp, const local_info& i);

參數

tp
轉換失敗的local_time。

i
嘗試轉換的相關信息。 如需詳細資訊,請參閱local_info

備註

您通常不會建立此例外狀況。 它會由將轉換成 local_time sys_time的函式擲回。

what

取得字串,描述時間不存在的原因。

[[nodiscard]] virtual const char* what() const noexcept;

傳回值

字串,描述時間不存在的原因。 例如:

2016-03-13 02:30:00 is in a gap between
2016-03-13 02:00:00 EST and
2016-03-13 03:00:00 EDT which are both equivalent to
2016-03-13 07:00:00 UTC

另請參閱

<chrono>
to_sys
ambiguous_local_time
標頭檔參考