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

            Bug ID: 92616
           Summary: Inconsistency in time between system_clock::now() and
                    time(nullptr)
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anthony.ajw at gmail dot com
  Target Milestone: ---

Created attachment 47321
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=47321&action=edit
system_clock::now vs time(nullptr) inconsistency

I have some code which uses std::this_thread::sleep_until to wait until a
specific time

The target time is set from a time_t value using
std::chrono::system_clock::from_time_t(). Consistently, after waking, the value
returned by time(nullptr) is less than the target time. However, the time
returned by std::chrono::system_clock::now() is greater than the current time.

e.g. the attached code creates the following output:

Now (sc) time is 1574342217233406476 (system clock)
=>1574342217(time_t)
=>13:16:57
Now (time(nullptr)) time is 1574342217(time_t)
=>13:16:57
Now (sc) time is 1574342219000231775 (system clock)
=>1574342219(time_t)
=>13:16:59
Now (time(nullptr)) time is 1574342218(time_t)
=>13:16:58

As you can see, when woken, the time return by time(nullptr) is earlier than
the corresponding time_t conversion from system_clock::now().

FYI, this is compiled using the g++-8.3.0 from the ubuntu 18.04 repos.

g++ --version outputs:

g++ (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0

Reply via email to