-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrono.cpp
246 lines (211 loc) · 6.74 KB
/
chrono.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "infra/chrono.h"
#include <sstream>
#include <fmt/chrono.h>
namespace inf::chrono {
date_point today()
{
#ifdef HAVE_CPP20_CHRONO
return std::chrono::year_month_day(std::chrono::sys_days(std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())));
#else
return date::year_month_day(date::sys_days(std::chrono::floor<date::days>(std::chrono::system_clock::now())));
#endif
}
local_date_point today_local()
{
#ifdef HAVE_CPP20_CHRONO
const auto now = std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::system_clock::now());
return std::chrono::floor<std::chrono::days>(now.get_local_time());
#else
const auto now = date::zoned_time(date::current_zone(), std::chrono::system_clock::now());
return std::chrono::floor<date::days>(now.get_local_time());
#endif
}
time_point now()
{
return std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
}
local_time_point now_local()
{
#ifdef HAVE_CPP20_CHRONO
const auto now = std::chrono::zoned_time(std::chrono::current_zone(), std::chrono::system_clock::now());
return std::chrono::floor<std::chrono::milliseconds>(now.get_local_time());
#else
const auto now = date::zoned_time(date::current_zone(), std::chrono::system_clock::now());
return std::chrono::floor<std::chrono::milliseconds>(now.get_local_time());
#endif
}
date_point date_from_time_point(time_point tp)
{
#ifdef HAVE_CPP20_CHRONO
return std::chrono::floor<std::chrono::days>(tp);
#else
return std::chrono::floor<date::days>(tp);
#endif
}
local_date_point date_from_time_point(local_time_point tp)
{
#ifdef HAVE_CPP20_CHRONO
return std::chrono::floor<std::chrono::days>(tp);
#else
return std::chrono::floor<date::days>(tp);
#endif
}
#ifdef HAVE_CPP20_CHRONO
std::chrono::hh_mm_ss<std::chrono::milliseconds> time_of_day(time_point tp)
{
auto dp = std::chrono::floor<std::chrono::days>(tp);
return std::chrono::hh_mm_ss(tp - dp); // Yields time_of_day type
}
std::chrono::hh_mm_ss<std::chrono::milliseconds> time_of_day(local_time_point tp)
{
auto dp = std::chrono::floor<std::chrono::days>(tp);
return std::chrono::hh_mm_ss(tp - dp); // Yields time_of_day type
}
std::chrono::year_month_day to_year_month_day(time_point tp)
{
return std::chrono::year_month_day(std::chrono::sys_days(std::chrono::floor<std::chrono::days>(tp)));
}
std::chrono::year_month_day to_year_month_day(local_time_point tp)
{
return std::chrono::year_month_day(std::chrono::local_days(std::chrono::floor<std::chrono::days>(tp)));
}
std::string to_string(std::chrono::local_seconds tp)
{
return to_string("%Y_%m_%d_%H.%M", tp);
}
std::string to_string(std::string_view format, std::chrono::local_seconds tp)
{
return fmt::format(fmt::runtime(fmt::format("{{:{}}}", format)), tp);
}
std::string to_string(local_time_point tp)
{
return fmt::format("{:%F}", tp);
}
#else
date::hh_mm_ss<std::chrono::milliseconds> time_of_day(time_point tp)
{
auto dp = date::floor<date::days>(tp);
return date::make_time(tp - dp); // Yields time_of_day type
}
date::hh_mm_ss<std::chrono::milliseconds> time_of_day(local_time_point tp)
{
auto dp = std::chrono::floor<date::days>(tp);
return date::hh_mm_ss(tp - dp); // Yields time_of_day type
}
date::year_month_day to_year_month_day(time_point tp)
{
return date::year_month_day(date::sys_days(std::chrono::floor<date::days>(tp)));
}
date::year_month_day to_year_month_day(local_time_point tp)
{
return date::year_month_day(date::local_days(std::chrono::floor<date::days>(tp)));
}
std::string to_string(date::local_seconds tp)
{
return to_string("%Y_%m_%d_%H.%M", tp);
}
std::string to_string(std::string_view format, date::local_seconds tp)
{
std::ostringstream dateStr;
dateStr << date::format(std::string(format).c_str(), tp);
return dateStr.str();
}
std::string to_string(local_time_point tp)
{
std::stringstream ss;
ss << tp;
return ss.str();
}
#endif
template <typename TimeType>
std::optional<TimeType> time_point_from_string(std::string_view str1, const char* format)
{
TimeType tp;
#if defined(HAVE_CPP20_CHRONO) && !defined(NO_CHRONO_PARSE_SUPPORT)
std::istringstream ss;
ss.str(std::string(str1));
ss >> std::chrono::parse(format, tp);
if (ss.fail()) {
return {};
}
#else
std::istringstream ss;
ss.str(std::string(str1));
ss >> date::parse(format, tp);
if (ss.fail()) {
return {};
}
#endif
return tp;
}
std::optional<time_point> system_time_point_from_string(std::string_view str1, const char* format)
{
return time_point_from_string<time_point>(str1, format);
}
std::optional<local_time_point> local_time_point_from_string(std::string_view str1, const char* format)
{
return time_point_from_string<local_time_point>(str1, format);
}
#ifdef HAVE_CPP20_CHRONO
std::optional<time_point> localtime_to_utc(local_time_point dt, std::optional<choose> choice)
{
if (choice.has_value()) {
return localtime_to_utc(zoned_time(std::chrono::current_zone(), dt, *choice));
} else {
return localtime_to_utc(zoned_time(std::chrono::current_zone(), dt));
}
}
std::optional<time_point> localtime_to_utc(zoned_time zt)
{
auto localTimePoint = zt.get_local_time();
std::optional<time_point> utcTime;
auto i = zt.get_time_zone()->get_info(localTimePoint);
switch (i.result) {
case std::chrono::local_info::unique: {
utcTime = std::optional<time_point>(zt.get_sys_time());
break;
}
case std::chrono::local_info::ambiguous: {
Log::warn("Ambiguous time point, taking the latest option");
utcTime = std::optional<time_point>(i.second.begin);
break;
}
case std::chrono::local_info::nonexistent:
Log::error("Non existing timepoint");
break;
default:
break;
}
return utcTime;
}
#else
std::optional<time_point> localtime_to_utc(local_time_point dt, std::optional<choose> choice)
{
auto ymd = chrono::to_year_month_day(dt);
auto tod = chrono::time_of_day(dt); // Yields time_of_day type
auto tp = date::local_days{ymd} + tod.hours() + tod.minutes() + tod.seconds();
auto z = date::current_zone();
std::optional<time_point> utcTime;
auto i = z->get_info(tp);
switch (i.result) {
case date::local_info::unique: {
date::zoned_time<std::chrono::seconds> zt(z, tp); //"Europe/Brussels"
utcTime = std::optional<time_point>(zt.get_sys_time());
break;
}
case date::local_info::ambiguous: {
if (choice) {
date::zoned_time<std::chrono::seconds> zt(z, tp, *choice);
utcTime = std::optional<time_point>(zt.get_sys_time());
}
break;
}
case date::local_info::nonexistent:
break;
default:
break;
}
return utcTime;
}
#endif
}