C++ Logo

std-proposals

Advanced search

C++20 chrono library can't use max date?

From: Akira Takahashi <faithandbrave_at_[hidden]>
Date: Sat, 21 Dec 2019 02:22:15 +0900
Hi,
`std::chrono::year` class has 16-bits signed integer for year
value. [-32767, 32767]

However, `system_clock::time_point::max()` of libc++ implementation
has 294247 years (microseconds based). So, C++20 chrono library can't use
max date.
Is this defect or correct?

#include <iostream>
#include <chrono>

using namespace std::chrono;

int main() {
    auto tp = system_clock::time_point::max();
    // std::cout << tp << std::endl;

    auto dp = floor<days>(tp);
    year_month_day date{dp};

    // std::cout << date << std::endl;
    std::cout
        << static_cast<int>(date.year()) << '/' // overflow year class
(16-bits)
        << static_cast<unsigned int>(date.month()) << '/'
        << static_cast<unsigned int>(date.day())
        << std::endl;

    // only years
    std::cout << floor<years>(system_clock::duration::max()).count() + 1970
<< std::endl;

    // show as time_t (local time)
    std::time_t t = floor<seconds>(tp.time_since_epoch()).count();
    std::cout << std::ctime(&t) << std::endl;
}

outputs:

32103/1/10
294247
Sun Jan 10 13:00:54 294247

Received on 2019-12-20 11:24:54