Vuo 2.4.4
Loading...
Searching...
No Matches
VuoTimeUtilities.cc
Go to the documentation of this file.
1
10#include "VuoTimeUtilities.hh"
11
12#include <chrono>
13#include <ctime>
14#include <sys/time.h>
15
20{
21 struct timeval t = getCurrentTime();
22 return t.tv_sec + t.tv_usec / 1000000.;
23}
24
28struct timeval VuoTimeUtilities::getCurrentTime(void)
29{
30 struct timeval now;
31 gettimeofday(&now, NULL);
32 return now;
33}
34
38struct timeval VuoTimeUtilities::getElapsedTime(const struct timeval &start, const struct timeval &end)
39{
40 long int diff = (end.tv_usec + 1000000 * end.tv_sec) - (start.tv_usec + 1000000 * start.tv_sec);
41 struct timeval result;
42 result.tv_sec = diff / 1000000;
43 result.tv_usec = diff % 1000000;
44 return result;
45}
46
51{
52 // https://stackoverflow.com/questions/15845505/how-to-get-higher-precision-fractions-of-a-second-in-a-printout-of-current-tim
53
54 auto now = std::chrono::system_clock::now();
55 auto secondsSinceEpoch(std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()));
56 std::time_t now_t(std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point(secondsSinceEpoch)));
57
58 const int length = 16;
59 char nowToSeconds[length] = {0};
60
61 strftime(nowToSeconds, length, "%Y%m%d_%H%M%S", std::localtime(&now_t));
62 auto nowMilliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch() - secondsSinceEpoch).count();
63
64 char nowFull[length+3] = {0};
65 snprintf(nowFull, length+4, "%s%03lld", nowToSeconds, nowMilliseconds);
66
67 return nowFull;
68}