10

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]
425nesp
  • 6,936
  • 9
  • 50
  • 61
ziggy
  • 117
  • 1
  • 1
  • 7
  • There is no standard way to print date with milliseconds. Using `std::chrono` I think all you can do is converting a `time_point` on your own. – TNA Nov 25 '14 at 21:32
  • You will need to use platform specific calls to get millisecond accuracy. – Thomas Matthews Nov 25 '14 at 21:39
  • 3
    @ThomasMatthews I'm pretty sure that chrono can pull an accuracy of up to a few microseconds, if the system allows it. For example try running this code: http://en.cppreference.com/w/cpp/chrono/time_point – Alex Nov 25 '14 at 21:40

4 Answers4

15

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

Community
  • 1
  • 1
bames53
  • 86,085
  • 15
  • 179
  • 244
  • You only need to subtract the seconds from tp because it converts the duration in second, however many they might be. Only `seconds s = duration_cast(tp); tp -= s;` – Alex Nov 25 '14 at 22:05
  • That's true, the other values are only used in the 'debug' code for displaying the full duration. – bames53 Nov 25 '14 at 22:08
  • 3
    And if you don't want to trudge through the C API you can do something like this: http://howardhinnant.github.io/date_algorithms.html#What%20can%20I%20do%20with%20that%20chrono%20compatibility? – Howard Hinnant Nov 25 '14 at 22:15
  • 1
    I was pulling together a work-around like this and saw "If std::time_t has lower precision, it is implementation-defined whether the value is rounded or truncated". That implies that we don't know if we can just add the milliseconds because the seconds out of to_time_t() may be one tick higher. Am I right about that? – Jay Miller Nov 25 '14 at 22:19
  • @JayMiller Yeah, that's true. I'm not certain what most implementations do. You may want to just go ahead and use Howard's date functions to replace `to_time_t()` yourself. – bames53 Nov 25 '14 at 22:23
  • The `%03u` conversion assumes the `duration<...>::rep` is 32-bit. Another way to do it is `std::cout << std::put_time(&t, "[%Y-%m-%d %H:%M:%S.") << std::setprecision(3) << std::setfill('0') << fraction / milliseconds(1) << "]\n";` – Jonathan Wakely Nov 26 '14 at 19:42
  • @JonathanWakely True, and the spec actually requires more bits for `std::chrono::milliseconds`. I'm actually relying on printf format checking to catch that, which unfortunately VS doesn't yet provide (except in their static analysis tool I think). You can see in my second example where I caught this thanks to compiler warnings. I've fixed the first example now. – bames53 Nov 26 '14 at 19:51
4

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.

Alex
  • 467
  • 4
  • 13
0

std::chrono give you utilities to represent a point in time or the elapsed duration between two points in time. It allows you to get information about these time intervals.

It does not provide any calendar information. Unfortunately, at this time there are no tools in the C++ standard for these. boost::date_time may be helpful here.

Jay Miller
  • 2,184
  • 12
  • 11
0

Did anybody notice that to_time_t rounds the seconds, instead of truncating

auto now = system_clock::now();
time_t secs = system_clock::to_time_t(now);
now {_MyDur={_MyRep=15107091978759765 } }
secs = 1510709198

so when you tack on the milliseconds

auto tse = now.time_since_epoch();
auto now_ms = duration_cast<milliseconds>(tse);
auto now_s = duration_cast<seconds>(tse);
auto jst_ms = now_ms - now_s;
DWORD msecs = jst_ms.count();
msecs = 875

secs should be 1510709197, but look at now_s, it's right

now_s {_MyRep=1510709197 }  
mbc
  • 1