min/max time_points cannot convert to calendar times

Hi,

I am not being able to get the dates associated with:

std::chrono::system_clock::time_point::max();

or

std::chrono::system_clock::time_point::min();

When I try to get the calendar data associated with these extreme time_points, using the code:

1
2
3
std::time_t t = std::chrono::system_clock::to_time_t(tp);
char buffer[50]; 
ctime_s(buffer, sizeof(buffer), &t);    // convert to calendar time 


I get an empty string ("") in buffer.

What could I be doing wrong?

Thanks!
Juan
> get the calendar data associated with these extreme time_points, using the code

The range of time points supported by the chrono library is typically a superset of the time points supported by the C library. These extreme time points would typically be well outside the range of time points that the C library can handle in a sane manner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <chrono>
#include <limits>
#include <ctime>
#include <cstring>

void show_cal_time( std::time_t t )
{
    std::cout << "numeric value of t: " << t << "\nc-library calendar time: " ;
    errno = 0 ;
    const auto ct = std::ctime( std::addressof(t) ) ;
    if( errno ) std::cout << "*** error: " << std::strerror(errno) << '\n'  ;
    else std::cout << ct << '\n' ;
    
    // linux: print it anyway (with linux we get spurious/misleading errors; however, some printable 
    //                        characters are placed into the buffer even on a genuine error)
    #ifdef __linux__
        std::cout << ct << '\n' ;  
    #endif // __linux__
}

int main()
{
    std::cout << "C++ min time_point\n-------------\n" ;
    show_cal_time( std::chrono::system_clock::to_time_t( std::chrono::system_clock::time_point::min() ) ) ;

    std::cout << "\nC++ max time_point\n-------------\n" ;
    show_cal_time( std::chrono::system_clock::to_time_t( std::chrono::system_clock::time_point::max() ) ) ;

    std::cout << "\nC++ current time_point\n-------------\n" ;
    show_cal_time( std::chrono::system_clock::to_time_t( std::chrono::system_clock::now() ) ) ;
}

Linux: http://coliru.stacked-crooked.com/a/85a2822262a4eb93
Windows: http://rextester.com/KANQS19005
Since there is no "ctime_s" in C++, you'd have to say what library you're getting it from in case someone knows something about it that's not mentioned in its documentation.

Chances are, it produces an empty string where std::ctime would have resulted in undefined behavior, that is when the year requires more than 4 digits.

Also note that different libraries have different min and max for their system_lock:
With Clang's libc++ http://coliru.stacked-crooked.com/a/d46707a589e5aa58 I get
Sun Jan 10 04:00:54 294247
Sun Dec 21 19:59:06 -290308

and with GNU libstdc++ http://coliru.stacked-crooked.com/a/92718858c7321e5a I get
Fri Apr 11 23:47:16 2262
Tue Sep 21 00:12:44 1677

(the difference is that in libc++, system_clock is measured in 64-bit signed milliseconds, while in libstdc++ it is in 64-bit signed nanoseconds)
Last edited on
Topic archived. No new replies allowed.