[LLVMbugs] [Bug 22193] New: invalid high_resolution_clock::now behavior

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Jan 11 10:58:01 PST 2015


http://llvm.org/bugs/show_bug.cgi?id=22193

            Bug ID: 22193
           Summary: invalid high_resolution_clock::now behavior
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: FreeBSD
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: edouard at quasardb.net
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    Classification: Unclassified

See this test program:

#include <chrono>
#include <iostream>

int main(int argc, char ** argv)
{

    // although system_clock should be less precise the returned values should
be pretty close
    // on my machine I have
    // 2689496100548 (this is in the 70', YEAH BABY YEAH)
    // 1421001821241019

    std::cout <<
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()
<< std::endl;

    std::cout <<
std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count()
<< std::endl;

    return 0;
}

Looking at the source code, the problem is obvious:

// from chrono.cpp

system_clock::time_point
system_clock::now() _NOEXCEPT
{
    timeval tv;
    gettimeofday(&tv, 0);
    return time_point(seconds(tv.tv_sec) + microseconds(tv.tv_usec));
}

time_t
system_clock::to_time_t(const time_point& t) _NOEXCEPT
{
    // correct because gettimeofday is epoch based
    return time_t(duration_cast<seconds>(t.time_since_epoch()).count());
}

steady_clock::time_point
steady_clock::now() _NOEXCEPT
{
    // incorrect, CLOCK_MONOTONIC returns an arbitrary value when calling
time_since_epoch it will fail

    struct timespec tp;

    if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
        __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");

    return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150111/5bf602c8/attachment.html>


More information about the llvm-bugs mailing list