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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
gettimeofday agrees with clock_gettime(CLOCK_REALTIME):

void dumpNow() {
  struct timespec ts;
  clock_gettime(CLOCK_REALTIME, &ts);
  auto const now = time(nullptr);
  struct timeval tv;
  gettimeofday(&tv, NULL);
  tm nowTm;
  localtime_r(&ts.tv_sec, &nowTm);
  std::cout << "Now (clock_gettime) time is " << ts.tv_sec << std::setw(9) <<
std::setfill('0') << ts.tv_nsec
            << "\n=>" << ts.tv_sec << "(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;

  localtime_r(&tv.tv_sec, &nowTm);
  std::cout << "Now (gettimeofday) time is " << tv.tv_sec << std::setw(6) <<
std::setfill('0') << tv.tv_usec << "\n=>" << tv.tv_sec << "(time_t)\n=>"
            << nowTm.tm_hour << ":" << nowTm.tm_min << ":" << nowTm.tm_sec
            << std::endl;
}

Now (clock_gettime) time is 1574344361836948044
=>1574344361(time_t)
=>13:52:41
Now (time(nullptr)) time is 1574344361(time_t)
=>13:52:41
Now (gettimeofday) time is 1574344361836949
=>1574344361(time_t)
=>13:52:41
Now (clock_gettime) time is 1574344363000108423
=>1574344363(time_t)
=>13:52:43
Now (time(nullptr)) time is 1574344362(time_t)
=>13:52:42
Now (gettimeofday) time is 1574344363000108
=>1574344363(time_t)
=>13:52:43

Reply via email to