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
头文件引用