共用方式為


ambiguous_local_time 類別

嘗試將 轉換成 local_time sys_time 時,會擲回這個例外狀況,而且結果可以是兩次的其中一個, choose::earliest 也沒有指定 或 choose::latest 來解決模棱兩可的情況。

語法

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

備註

從日光節約時間轉換到秋季的標準時間期間,時鐘基本上會增加一小時。 這可能會造成混淆,因為轉換到標準時間是否意味著失去一小時? 藉由倒退一小時,轉換前的小時會在時鐘調整標準時間之後重複。 請考慮紐約標準時間的變更,該時間發生在11月的第一個星期天上午2:00。 首先,上午 1:00 會過去。 淩晨 2 點,時鐘會轉換為標準時間,所以現在又是上午 1:00。 這表示上午 1 點到 2 點之間的時間將會「重複」,有效地增加了一個小時。

local_time如果 指定此「額外」小時內的時間,則不清楚如何轉換。 轉換的時間應該視為該小時發生的「第一次」時間,還是「第二次」? 如果未指定列舉 choose 來指出它應該是哪一個 ambiguous_local_time ,您將會收到例外狀況。

從標準時間轉換成日光節約時間時,並不存在此問題。 在此情況下,可能會發生不同的問題。 如需詳細資訊,請參閱nonexistent_local_time

下列範例顯示模棱兩可的轉換。

範例: ambiguous_local_time

#include <chrono>
#include <iostream>

using namespace std::chrono;
    
int main()
{
    try
    {
        // The following will throw an exception because converting 1:30am local time to system time could be interpreted as either 
        // 1:30 AM EDT or 1:30 AM EST. Which to choose isn't specified for the conversion, so an ambiguous_local_time
        // exception is thrown.
        auto zt = zoned_time{"America/New_York", local_days{Sunday[1]/November/2016} + 1h + 30min};
    } catch (const ambiguous_local_time& e)
    {
        std::cout << e.what() << '\n';
    }
    return 0;
}
2016-11-06 01:30:00 is ambiguous. It could be
2016-11-06 01:30:00 EDT == 2016-11-06 05:30:00 UTC or
2016-11-06 01:30:00 EST == 2016-11-06 06:30:00 UTC

成員

名稱 描述
建構函式 ambiguous_local_time建構 。
what 取得描述模棱兩可本質的字串。

需求

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

命名空間std::chrono

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

建構函式

建構 ambiguous_local_time

template<class Duration>
ambiguous_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-11-06 01:30:00 is ambiguous. It could be
2016-11-06 01:30:00 EDT == 2016-11-06 05:30:00 UTC or
2016-11-06 01:30:00 EST == 2016-11-06 06:30:00 UTC

另請參閱

<chrono>
to_sys
nonexistent_local_time
標頭檔參考