Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 actual system time with milliseconds

I've got a problem with getting actual system time with milliseconds. The only one good method I found is in Windows.h, but I can't use it. I'm supposed to use std::chrono. How can I do this?

I spent a lot of time trying to google it, but I found only second-precision examples.

I'm trying to get string like this:

[2014-11-25 22:15:38:449]
like image 542
ziggy Avatar asked Nov 25 '14 21:11

ziggy


Video Answer


2 Answers

Using code from this answer:

#include <chrono>
#include <ctime>
#include <iostream>

template <typename Duration>
void print_time(tm t, Duration fraction) {
    using namespace std::chrono;
    std::printf("[%04u-%02u-%02u %02u:%02u:%02u.%03u]\n", t.tm_year + 1900,
                t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec,
                static_cast<unsigned>(fraction / milliseconds(1)));

    // VS2013's library has a bug which may require you to replace
    // "fraction / milliseconds(1)" with
    // "duration_cast<milliseconds>(fraction).count()"
}

int main() {
    using namespace std;
    using namespace std::chrono;

    system_clock::time_point now = system_clock::now();
    system_clock::duration tp = now.time_since_epoch();

    tp -= duration_cast<seconds>(tp);

    time_t tt = system_clock::to_time_t(now);

    print_time(*gmtime(&tt), tp);
    print_time(*localtime(&tt), tp);
}

One thing to keep in mind is that the fact that the timer returns values of sub-millisecond denominations does not necessarily indicate that the timer has sub-millisecond resolution. I think Windows' implementation in VS2015 may finally be fixed, but the timer they've been using to back their chrono implementation so far has been sensitive to the OS timeBeginPeriod() setting, displaying varying resolution, and the default setting is I think 16 milliseconds.

Also the above code assumes that neither UTC nor your local timezone are offset from the epoch of std::chrono::system_clock by a fractional second value.


Example of using Howard's date functions to avoid ctime: http://coliru.stacked-crooked.com/a/98db840b238d3ce7

like image 196
bames53 Avatar answered Sep 21 '22 05:09

bames53


This answer still uses a bit of C API but is only used in the function, so you can forget about it:

template<typename T>
void print_time(std::chrono::time_point<T> time) {
    using namespace std;
    using namespace std::chrono;

    time_t curr_time = T::to_time_t(time);
    char sRep[100];
    strftime(sRep,sizeof(sRep),"%Y-%m-%d %H:%M:%S",localtime(&curr_time));

    typename T::duration since_epoch = time.time_since_epoch();
    seconds s = duration_cast<seconds>(since_epoch);
    since_epoch -= s;
    milliseconds milli = duration_cast<milliseconds>(since_epoch);

    cout << '[' << sRep << ":" << milli.count() << "]\n";
}

This is merely a rewrite of the code that bames53, but using strftime to shorten the code a bit.

like image 44
Alex Avatar answered Sep 18 '22 05:09

Alex