This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
forked from salange/libffmpeg-zmq-streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_functions.hpp
61 lines (53 loc) · 1.83 KB
/
time_functions.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef TIMEUTILS_HPP_SQ9GVNJU
#define TIMEUTILS_HPP_SQ9GVNJU
#include <chrono>
#include <iomanip>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <time.h>
using namespace std::chrono;
static double current_millis() {
return duration_cast<milliseconds>(system_clock::now().time_since_epoch())
.count() /
1000.0;
}
static int _get_millis_from_tp(const system_clock::time_point &t) {
auto duration_ms = duration_cast<milliseconds>(t.time_since_epoch());
return duration_ms.count() % 1000;
}
static std::tm _tm_from_tp(const system_clock::time_point &t) {
std::tm calendar_time_utc{};
const std::time_t as_time_t = system_clock::to_time_t(t);
auto return_value = gmtime_r(&as_time_t, &calendar_time_utc);
if (!return_value) {
throw std::runtime_error(
"Could not convert TimePoint to UTC calendar time.");
} else {
return *return_value;
}
}
static std::string format_timepoint_iso8601(const system_clock::time_point &t,
bool add_zone = true,
bool add_millis = true) {
const std::tm tm = _tm_from_tp(t);
std::stringstream ss;
ss << std::put_time(&tm, "%Y-%m-%dT%H:%M:%S");
if (add_millis) {
ss << "." << std::setw(3) << std::setfill('0') << _get_millis_from_tp(t);
}
if (add_zone) {
ss << "Z";
}
return ss.str();
}
static void stamp_image(cv::Mat &image,
system_clock::time_point t = system_clock::now(),
float ypos = 0.2) {
auto stamp = format_timepoint_iso8601(t);
cv::putText(image, stamp, cv::Point(10, ypos * image.rows),
cv::FONT_HERSHEY_SIMPLEX, 2, cv::Scalar(255, 255, 255), 2);
}
#endif /* end of include guard: TIMEUTILS_HPP_SQ9GVNJU */