Reputation: 41
I'm having some issues using chrono after 2038. My system is a 32Bit system with the following configuration Kernel: 5.15.118 GCC: 13.2.0 GLIBC: 2.38
I have compiled my program with the flags _TIME_BITS=64 and _FILE_OFFSET_BITS=64 to enable the support of 64Bits time. This works on some places but I keep having issues with chrono.
I'm using the following
#include <iostream>
#include <time.h>
#include <chrono>
int main() {
// Using chrono library
auto now_chrono = std::chrono::system_clock::now();
auto duration_since_epoch_chrono = now_chrono.time_since_epoch();
auto seconds_since_epoch_chrono = std::chrono::duration_cast<std::chrono::seconds>(duration_since_epoch_chrono).count();
// Using time.h library
std::time_t now_time = std::time(nullptr);
auto seconds_since_epoch_time = static_cast<long long>(now_time);
// Print results
std::cout << "Seconds since epoch using chrono: " << seconds_since_epoch_chrono << std::endl;
std::cout << "Seconds since epoch using time.h: " << seconds_since_epoch_time << std::endl;
return 0;
}
The results:
Seconds since epoch using chrono: -1248915079
Seconds since epoch using time.h: 2213771158
Using time directly it seems to work fine but through chrono it rolls over after 2038. Before 2038 they both count up every second but after chrono is stuck on a negative value that doesn't change.
Am I missing a crucial step in configuration here?
Upvotes: 4
Views: 98