8#ifndef _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
9#define _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
18 const char& last_char,
19 const size_t& repeat_count,
21 const tz_t& utc_offset,
23 std::string& result) {
26 if (repeat_count > 1)
break;
30 if (repeat_count > 1)
break;
34 if (repeat_count == 1) {
37 result += std::string(buffer);
41 if (repeat_count <= 2) {
43 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
hour);
44 result += std::string(buffer);
48 if (repeat_count == 2) {
50 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
hour);
51 result += std::string(buffer);
56 if (repeat_count > 1)
break;
60 if (repeat_count > 1)
break;
66 if (repeat_count <= 1){
73 std::fill(buffer, buffer +
sizeof(buffer),
'\0');
74 snprintf(buffer,
sizeof(buffer),
"%2d ", dt.
day);
75 result += std::string(buffer);
77 std::fill(buffer, buffer +
sizeof(buffer),
'\0');
78 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d ", dt.
hour, dt.
min, dt.
sec);
79 result += std::string(buffer);
81 result += std::to_string(dt.
year);
85 if (repeat_count > 1)
break;
86 result += std::to_string(dt.
year/100);
89 if (repeat_count < 2) {
91 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
day);
92 result += std::string(buffer);
96 if (repeat_count == 1) {
98 char buffer[16] = {0};
99 snprintf(buffer,
sizeof(buffer),
"%.2d/%.2d/%.2d", dt.
mon, dt.
day, (
int)(dt.
year % 100LL));
100 result += std::string(buffer);
102 if (repeat_count == 2) {
103 char buffer[4] = {0};
104 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
day);
105 result += std::string(buffer);
109 if (repeat_count == 1) {
110 char buffer[4] = {0};
111 snprintf(buffer,
sizeof(buffer),
"%2d", dt.
day);
112 result += std::string(buffer);
120 if (repeat_count == 1) {
122 char buffer[32] = {0};
123 if (dt.
year <= 9999 || dt.
year >= 0) {
124 snprintf(buffer,
sizeof(buffer),
"%.4d-%.2d-%.2d", (
int)dt.
year, dt.
mon, dt.
day);
127 snprintf(buffer,
sizeof(buffer),
"-%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
129 snprintf(buffer,
sizeof(buffer),
"+%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
131 result += std::string(buffer);
140 if (repeat_count == 1) {
141 char buffer[4] = {0};
143 result += std::string(buffer);
147 if (repeat_count == 1) {
148 char buffer[4] = {0};
149 snprintf(buffer,
sizeof(buffer),
"%2d", dt.
hour);
150 result += std::string(buffer);
154 if (repeat_count == 1) {
155 char buffer[4] = {0};
157 result += std::string(buffer);
161 if (repeat_count == 1) {
162 char buffer[4] = {0};
163 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
mon);
164 result += std::string(buffer);
166 if (repeat_count == 2) {
167 char buffer[4] = {0};
168 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
min);
169 result += std::string(buffer);
173 if (repeat_count == 1) {
174 char buffer[4] = {0};
175 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
min);
176 result += std::string(buffer);
178 if (repeat_count == 2) {
179 char buffer[4] = {0};
180 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
mon);
181 result += std::string(buffer);
183 if (repeat_count == 3) {
194 if (dt.
hour < 12) result +=
"AM";
198 if (dt.
hour < 12) result +=
"am";
202 if (repeat_count == 1) {
203 char buffer[16] = {0};
206 result += std::string(buffer);
212 if (repeat_count == 1) {
213 char buffer[8] = {0};
214 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d", dt.
hour, dt.
min);
215 result += std::string(buffer);
219 if (repeat_count == 1) {
220 result += std::to_string(
ts);
223 if (repeat_count == 4) {
224 result += std::to_string(dt.
ms);
229 if (repeat_count <= 2) {
230 char buffer[4] = {0};
231 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
sec);
232 result += std::string(buffer);
236 if (repeat_count > 1)
break;
241 if (repeat_count == 1) {
242 char buffer[16] = {0};
243 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
244 result += std::string(buffer);
248 if (repeat_count == 1) {
252 result += std::to_string(dw);
263 if (repeat_count == 1) {
266 if (repeat_count == 3) {
272 if (repeat_count == 3) {
283 if (repeat_count == 1) {
284 result += std::to_string(dt.
year % 100);
288 if (repeat_count == 1) {
289 result += std::to_string(dt.
year);
291 if (repeat_count == 6) {
292 char buffer[32] = {0};
293 const int64_t mega_years = dt.
year / 1000000;
294 const int64_t millennia = (dt.
year - mega_years * 1000000) / 1000;
295 const int64_t centuries = dt.
year - mega_years * 1000000 - millennia * 1000;
298 snprintf(buffer,
sizeof(buffer),
"%lldM%lldK%.3lld", mega_years, std::abs(millennia), std::abs(centuries));
300 snprintf(buffer,
sizeof(buffer),
"%lldM%.3lld", mega_years, std::abs(centuries));
304 snprintf(buffer,
sizeof(buffer),
"%lldK%.3lld", millennia, std::abs(centuries));
306 snprintf(buffer,
sizeof(buffer),
"%.4lld", dt.
year);
308 result += std::string(buffer);
310 if (repeat_count == 4) {
311 char buffer[8] = {0};
312 snprintf(buffer,
sizeof(buffer),
"%.4d", (
int)(dt.
year % 10000));
313 result += std::string(buffer);
315 if (repeat_count == 2) {
316 char buffer[8] = {0};
317 snprintf(buffer,
sizeof(buffer),
"%.2d", (
int)(dt.
year % 100));
318 result += std::string(buffer);
323 if (repeat_count == 1) {
325 char buffer[16] = {0};
327 else snprintf(buffer,
sizeof(buffer),
"-%.2d%.2d", tz.
hour, tz.
min);
328 result += std::string(buffer);
369 template<
class T = ts_t>
371 const std::string& format_str,
373 const tz_t& utc_offset = 0) {
375 if (format_str.empty())
return result;
380 bool is_command =
false;
381 size_t repeat_count = 0;
382 char last_char = format_str[0];
383 if (last_char !=
'%') result += last_char;
384 for (
size_t i = 0; i < format_str.size(); ++i) {
385 const char& current_char = format_str[i];
387 if (current_char ==
'%') {
389 if (repeat_count == 2) {
390 result += current_char;
396 result += current_char;
399 last_char = current_char;
403 if (last_char == current_char) {
420 template<
class T = ts_t>
422 const std::string& format_str,
424 const tz_t& utc_offset = 0) {
435 template<
class T = ts_t>
438 char buffer[32] = {0};
440 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms);
442 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
444 return std::string(buffer);
454 template<
class T = ts_t>
457 char buffer[32] = {0};
458 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
459 return std::string(buffer);
469 template<
class T = ts_t>
472 char buffer[32] = {0};
474 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d.%.3d", dt.
hour, dt.
min, dt.
sec, dt.
ms);
476 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
478 return std::string(buffer);
488 template<
class T = ts_t>
491 char buffer[32] = {0};
493 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d.%.3dZ", dt.
hour, dt.
min, dt.
sec, dt.
ms);
495 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2dZ", dt.
hour, dt.
min, dt.
sec);
497 return std::string(buffer);
507 template<
class T = ts_t>
510 char buffer[32] = {0};
512 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms);
514 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2dZ", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
516 return std::string(buffer);
527 char buffer[32] = {0};
528 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms);
529 return std::string(buffer);
540 char buffer[32] = {0};
541 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms);
542 return std::string(buffer);
553 template<
class T = ts_t>
557 char buffer[32] = {0};
560 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d+%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms, tz.
hour, tz.
min);
562 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d-%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms, tz.
hour, tz.
min);
566 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d+%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, tz.
hour, tz.
min);
568 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d-%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, tz.
hour, tz.
min);
571 return std::string(buffer);
584 char buffer[32] = {0};
586 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d+%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms, tz.
hour, tz.
min);
588 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d.%.3d-%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec, dt.
ms, tz.
hour, tz.
min);
590 return std::string(buffer);
601 char buffer[32] = {0};
602 snprintf(buffer,
sizeof(buffer),
"%lld.%.2d.%.2d %.2d:%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
603 return std::string(buffer);
620 char buffer[32] = {0};
621 snprintf(buffer,
sizeof(buffer),
"%lld.%.2d.%.2d", dt.
year, dt.
mon, dt.
day);
622 return std::string(buffer);
633 char buffer[32] = {0};
634 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
635 return std::string(buffer);
#define TIME_SHIELD_CONSTEXPR
Header for date and time structure and related functions.
Main namespace for the Time Shield library.
const std::string & to_str(const Weekday &value, const FormatType &format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
int64_t ts_t
Integer timestamp type.
const std::string to_iso8601_time_str(const T &ts)
Converts a timestamp to an ISO8601 time string.
const std::string to_mql5_time_str(const ts_t &ts)
Converts a timestamp to a string in MQL5 time format.
const std::string to_string(const std::string &format_str, const T ×tamp, const tz_t &utc_offset=0)
Convert timestamp to string with custom format.
TIME_SHIELD_CONSTEXPR const T hour24_to_12(const T &hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
int64_t ts_ms_t
Integer timestamp milliseconds type.
const TimeZoneStruct to_time_zone_struct(const tz_t &offset)
Converts an integer to a TimeZoneStruct.
const std::string to_iso8601_utc_str(const T &ts)
Converts a timestamp to an ISO8601 string in UTC format.
const TimeZoneStruct to_time_zone(const tz_t &offset)
Converts an integer to a time zone structure.
const std::string to_mql5_date_time_str(const ts_t &ts)
Converts a timestamp to a string in MQL5 date and time format.
const std::string to_iso8601_utc_str_ms(const ts_ms_t &ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string in UTC format.
const ts_t timestamp() noexcept
Get the current UTC timestamp in seconds.
const ts_ms_t ts_ms() noexcept
Get the current UTC timestamp in milliseconds.
const T day_of_year(const ts_t &ts=ts())
Get the day of the year.
void process_format_impl(const char &last_char, const size_t &repeat_count, const ts_t &ts, const tz_t &utc_offset, const DateTimeStruct &dt, std::string &result)
const std::string to_mql5_full_str(const ts_t &ts)
Alias for to_mql5_date_time_str function.
constexpr const T1 day_of_week(const T2 &year, const T3 &month, const T3 &day)
Alias for day_of_week_date function.
@ UPPERCASE_NAME
Uppercase short name.
T to_date_time_ms(const ts_ms_t &ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
const std::string to_iso8601_time_utc_str(const T &ts)
Converts a timestamp to an ISO8601 UTC time string.
T1 to_date_time(const T2 &ts)
Converts a timestamp to a date-time structure.
const std::string to_mql5_date_str(const ts_t &ts)
Converts a timestamp to a string in MQL5 date format.
Month
Enumeration of the months of the year.
const std::string to_iso8601_str(const T &ts)
Converts a timestamp to an ISO8601 string.
const ts_t ts() noexcept
Get the current UTC timestamp in seconds.
const std::string to_iso8601_str_ms(const ts_ms_t &ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string.
const std::string to_iso8601_date_str(const T &ts)
Converts a timestamp to an ISO8601 date string.
Structure to represent date and time.
int ms
Millisecond component of time (0-999)
int hour
Hour component of time (0-23)
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int min
Minute component of time (0-59)
int mon
Month component of the date (1-12).
int sec
Second component of time (0-59)
Structure to represent time zone information.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
bool is_positive
True if the time zone offset is positive, false if negative.
Header file for time conversion functions.
Header for time zone structure and related functions.