https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92616

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I see the same result when using clock_gettime directly instead of
system_clock::now()

#include <time.h>
#include <iostream>
#include <iomanip>
#include <thread>

void dumpNow() {
  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  auto const nowSc = ts.tv_sec;
  auto const now = time(nullptr);
  tm nowTm;
  localtime_r(&nowSc, &nowTm);
  std::cout << "Now (sc) time is " << ts.tv_sec << std::setw(9) <<
std::setfill('0') << ts.tv_nsec
            << " (system clock)\n=>" << nowSc << "(time_t)\n=>" <<
nowTm.tm_hour
            << ":" << nowTm.tm_min << ":" << nowTm.tm_sec << std::endl;

  localtime_r(&now, &nowTm);
  std::cout << "Now (time(nullptr)) time is " << now << "(time_t)\n=>"
            << nowTm.tm_hour << ":" << nowTm.tm_min << ":" << nowTm.tm_sec
            << std::endl;
}
int main() {
  dumpNow();
  std::this_thread::sleep_until(
      std::chrono::system_clock::from_time_t(time(nullptr) + 2));
  dumpNow();
}

Now (sc) time is 1574343394343888130 (system clock)
=>1574343394(time_t)
=>13:36:34
Now (time(nullptr)) time is 1574343394(time_t)
=>13:36:34
Now (sc) time is 1574343396000083583 (system clock)
=>1574343396(time_t)
=>13:36:36
Now (time(nullptr)) time is 1574343395(time_t)
=>13:36:35

Reply via email to