std::chrono::time_point<Clock,Duration>::max

来自cppreference.com
< cpp‎ | chrono‎ | time point
 
 
工具库
通用工具
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

初等字符串转换
(C++17)
(C++17)
栈踪
 
 
 
static constexpr time_point max();
(C++20 前)
static constexpr time_point max() noexcept;
(C++20 起)

返回拥有最大可能时长的 time_point ,即 time_point(std::chrono::duration::max())

参数

(无)

返回值

最大的可能的 time_point

示例

#include <chrono>
#include <vector>
#include <iostream>
 
int main() 
{
    std::chrono::time_point<std::chrono::system_clock> now =
        std::chrono::system_clock::now();
    std::vector<std::chrono::time_point<std::chrono::system_clock>> times {
        now - std::chrono::hours(24),
        now - std::chrono::hours(48),
        now + std::chrono::hours(24),
    };  
 
    std::chrono::time_point<std::chrono::system_clock> earliest =
        std::chrono::time_point<std::chrono::system_clock>::max();
 
    std::cout << "all times:\n";
    for (const auto &time : times) {
        std::time_t t = std::chrono::system_clock::to_time_t(time);
        std::cout << std::ctime(&t);
 
        if (time < earliest) earliest = time;
    }
 
    std::time_t t = std::chrono::system_clock::to_time_t(earliest);
    std::cout << "earliest:\n" << std::ctime(&t);
}

可能的输出:

all times:
Sun Oct  7 19:06:48 2012
Sat Oct  6 19:06:48 2012
Tue Oct  9 19:06:48 2012
earliest:
Sat Oct  6 19:06:48 2012