C ++ 11 virtual system time with milliseconds

I had a problem getting the actual system time in milliseconds. The only good method I found is on Windows.h , but I can't use it. I have to use std::chrono . How can i do this?

I spent a lot of time on this, but found only examples of second accuracy.

I am trying to get a string like this:

 [2014-11-25 22:15:38:449] 
+5
source share
4 answers

Using the 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 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); } 

It should be borne in mind that the fact that the timer returns the values โ€‹โ€‹of submillisecond denominations does not necessarily indicate that the timer has submillisecond resolution. I think that the Windows implementation in VS2015 can be finally fixed, but the timer they used to support their chrono implementation so far has been sensitive to the timeBeginPeriod() OS, displaying different resolutions, and the default value is 16 milliseconds.

Also, the above code assumes that neither UTC nor your local time zone is offset from the era of std::chrono::system_clock fractional second value.


An example of using the Howard date functions to avoid ctime: http://coliru.stacked-crooked.com/a/98db840b238d3ce7

+7
source

This answer still uses a little C API, but is only used in a 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"; } 

It is simple to rewrite the code that is bames53, but using strftime to shorten the code a bit.

+3
source

std::chrono provides you with utilities to represent a point in time or elapsed duration between two time points. This allows you to receive information about these time intervals.

It does not provide any calendar information. Unfortunately, there are currently no tools for them in the C ++ standard. boost::date_time may be useful here.

+1
source

Has anyone noticed that to_time_t rounds seconds, not truncates

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

so when you make a mark in 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 now_s, that's right

 now_s {_MyRep=1510709197 } 
0
source

Source: https://habr.com/ru/post/1207696/


All Articles