命名空间
变体
操作

std::chrono::time_zone::to_local

来自 cppreference.cn
< cpp‎ | chrono‎ | time zone
 
 
 
 
template< class Duration >

auto to_local( const std::chrono::sys_time<Duration>& tp ) const

    -> std::chrono::local_time<std::common_type_t<Duration, std::chrono::seconds>>;
(since C++20)

sys_time tp 转换为此时区中对应的 local_time

目录

[编辑] 参数

tp - 要转换的时间点

[编辑] 返回值

tp 和此时区关联的 local_time

[编辑] 注解

结果的精度至少为 std::chrono::seconds,如果参数具有更高的精度,则会更精细。

[编辑] 示例

#include <chrono>
#include <iostream>
 
int main()
{
    const auto some_zone_name{"Australia/Sydney"};
    const auto time_pt_utc{std::chrono::system_clock::now()};
    std::cout << "Current time UTC is:\t\t " << time_pt_utc << '\n';
 
    try
    {
        std::cout << "Current time local is:\t\t "
                  << std::chrono::current_zone()-> // may throw
                      to_local(time_pt_utc) << '\n'
                  << "Current time " << some_zone_name << " is:\t "
                  << std::chrono::locate_zone(some_zone_name)-> // may throw
                      to_local(time_pt_utc) << '\n';
    }
    catch(const std::runtime_error& ex)
    {
        std::cout << ex.what() << '\n';
    }
}

可能的输出

Current time UTC is:              2025-02-10 13:38:13.233872158
Current time local is:            2025-02-10 16:38:13.233872158
Current time Australia/Sydney is: 2025-02-11 00:38:13.233872158