Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should use system_clock instead of high_resolution_clock for timestamp_now #11

Open
BTNC opened this issue Nov 23, 2016 · 5 comments
Open

Comments

@BTNC
Copy link

BTNC commented Nov 23, 2016

Only system_clock's epoch is guaranteed to be January 1, 1970, while steady_clock's epoch maybe not. high_resolution_clock maybe a synonym for system_clock or steady_clock. So system_clock should be used to get time_since_epoch if the desired epoch is January 1, 1970.

@Iyengar111
Copy link
Owner

Hi,

Can you give me a reference where it mentions steady_clock epoch is not Jan 1, 1970.

Thanks.

@BTNC
Copy link
Author

BTNC commented Nov 26, 2016

Search steady_clock from google could result lots of discussions about system_clock, steady_clock, etc. Below are 2 of those references: cppreference,stackoverflow

@sandym
Copy link

sandym commented Jan 25, 2017

With libc++, high_resolution_clock epoch is the last boot time.

@ghost
Copy link

ghost commented Feb 2, 2017

Also affected on windows VS2015, I get time stamps as [1970-01-02 06:01:03.451556]. system_clock works as expected [2017-02-02 12:22:14.434341].

If high_resolution_clock is important for time stamps, may be offset can be used?

    uint64_t timestamp_now()
    {
	    static auto hrc_offset = std::chrono::system_clock::now().time_since_epoch() - std::chrono::high_resolution_clock::now().time_since_epoch();
	    return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch() + hrc_offset).count();
    }

@alanthie
Copy link

alanthie commented May 2, 2018

uint64_t timestamp_now()
{
  return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

void format_timestamp(std::ostream & os, uint64_t timestamp)
{
  auto usecs = std::chrono::microseconds(timestamp);
  auto secs = std::chrono::duration_cast<std::chrono::seconds> (usecs);
  usecs -= secs;
  std::time_t time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point{ secs });

  os << '[' << std::put_time(std::localtime(&time_t), "%Y-%m-%d %X");
  os << "." << usecs.count() << ']';
}

[2018-05-01 21:17:45.272498][INFO][20072][c:\work\nanolog\main.cpp:main:20] Sample NanoLog: 0
[2018-05-01 21:17:45.272508][INFO][20072][c:\work\nanolog\main.cpp:main:20] Sample NanoLog: 1
[2018-05-01 21:17:45.272513][INFO][20072][c:\work\nanolog\main.cpp:main:20] Sample NanoLog: 2
[2018-05-01 21:17:45.272517][INFO][20072][c:\work\nanolog\main.cpp:main:20] Sample NanoLog: 3
[2018-05-01 21:17:45.272522][INFO][20072][c:\work\nanolog\main.cpp:main:20] Sample NanoLog: 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants