-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibTime.cpp
More file actions
130 lines (96 loc) · 2.75 KB
/
LibTime.cpp
File metadata and controls
130 lines (96 loc) · 2.75 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
This file is part of the DSP-Crowd project
https://www.dsp-crowd.com
Author(s):
- Johannes Natter, office@dsp-crowd.com
File created on 17.01.2023
Copyright (C) 2023, Johannes Natter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <iomanip>
#include "LibTime.h"
using namespace std;
using namespace chrono;
uint32_t millis()
{
auto now = steady_clock::now();
auto nowMs = time_point_cast<milliseconds>(now);
return nowMs.time_since_epoch().count();
}
string nowUtc()
{
TimePoint tp = system_clock::now();
string res = tpToStr(tp, "%Y-%m-%dT%H:%M:%S.");
auto timeSinceEpoch = tp.time_since_epoch();
auto sec = duration_cast<seconds>(timeSinceEpoch);
auto nanoSec = duration_cast<nanoseconds>(timeSinceEpoch) - duration_cast<nanoseconds>(sec);
stringstream ss;
ss << setw(7) << setfill('0') << nanoSec.count() << "Z";
res += ss.str();
return res;
}
TimePoint nowTp()
{
return system_clock::now();
}
string nowToStr(const char *pFmt)
{
TimePoint tp = system_clock::now();
return tpToStr(tp, pFmt);
}
// https://cplusplus.com/reference/ctime/strftime/
string tpToStr(const TimePoint &tp, const char *pFmt)
{
time_t tt_t = system_clock::to_time_t(tp);
const tm *tm_t = ::localtime(&tt_t);
char timeBuf[32];
size_t res;
if (!pFmt)
pFmt = "%d.%m.%y %H:%M:%S";
timeBuf[0] = 0;
res = strftime(timeBuf, sizeof(timeBuf), pFmt, tm_t);
if (!res)
return "";
return timeBuf;
}
TimePoint strToTp(const string &str, const char *pFmt)
{
TimePoint tp;
tm tm = {};
if (!pFmt)
pFmt = "%d.%m.%y %H:%M:%S";
stringstream ss(str);
ss >> get_time(&tm, pFmt);
if (ss.fail())
return tp;
tp = system_clock::from_time_t(mktime(&tm));
return tp;
}
size_t tpDiffSec(const TimePoint &tpEnd, const TimePoint &tpStart)
{
if (tpStart > tpEnd)
return 0;
return duration_cast<seconds>(tpEnd - tpStart).count();
}
size_t tpDiffMs(const TimePoint &tpEnd, const TimePoint &tpStart)
{
if (tpStart > tpEnd)
return 0;
return duration_cast<milliseconds>(tpEnd - tpStart).count();
}
TimePoint utcToCet(const TimePoint &tpUtc)
{
time_t tTt = system_clock::to_time_t(tpUtc);
tm tTm = *localtime(&tTt);
return tpUtc + hours(tTm.tm_isdst ? 2 : 1);
}