Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_formatting.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
3#define _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
4
11
12#include "date_time_struct.hpp"
13#include "time_zone_struct.hpp"
14#include "time_conversions.hpp"
15
16namespace time_shield {
17
20
22 char last_char,
23 size_t repeat_count,
24 ts_t ts,
25 tz_t utc_offset,
26 const DateTimeStruct& dt,
27 std::string& result) {
28 switch (last_char) {
29 case 'a':
30 if (repeat_count > 1) break;
31 result += to_str(day_of_week(dt.year, dt.mon, dt.day), FormatType::SHORT_NAME);
32 break;
33 case 'A':
34 if (repeat_count > 1) break;
35 result += to_str(day_of_week(dt.year, dt.mon, dt.day), FormatType::FULL_NAME);
36 break;
37 case 'I':
38 if (repeat_count == 1) {
39 char buffer[4] = {0};
40 snprintf(buffer, sizeof(buffer), "%.2d", hour24_to_12(dt.hour));
41 result += std::string(buffer);
42 }
43 break;
44 case 'H':
45 if (repeat_count <= 2) {
46 char buffer[4] = {0};
47 snprintf(buffer, sizeof(buffer),"%.2d", dt.hour);
48 result += std::string(buffer);
49 }
50 break;
51 case 'h':
52 if (repeat_count == 2) {
53 char buffer[4] = {0};
54 snprintf(buffer, sizeof(buffer),"%.2d", dt.hour);
55 result += std::string(buffer);
56 break;
57 }
58 // %h: Equivalent to %b
59 case 'b':
60 if (repeat_count > 1) break;
61 result += to_str(static_cast<Month>(dt.mon), FormatType::SHORT_NAME);
62 break;
63 case 'B':
64 if (repeat_count > 1) break;
65 result += to_str(static_cast<Month>(dt.mon), FormatType::FULL_NAME);
66 break;
67 case 'c':
68 // Preferred date and time representation for the current locale.
69 // %a %b %e %H:%M:%S %Y
70 if (repeat_count <= 1){
71 char buffer[16];
72 result += to_str(day_of_week(dt.year, dt.mon, dt.day), FormatType::SHORT_NAME);
73 result += " ";
74 result += to_str(static_cast<Month>(dt.mon), FormatType::SHORT_NAME);
75 result += " ";
76 // added %e
77 std::fill(buffer, buffer + sizeof(buffer), '\0');
78 snprintf(buffer, sizeof(buffer),"%2d ", dt.day);
79 result += std::string(buffer);
80 // added %H:%M:%S
81 std::fill(buffer, buffer + sizeof(buffer), '\0');
82 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d ", dt.hour, dt.min, dt.sec);
83 result += std::string(buffer);
84 // added %Y
85 result += std::to_string(dt.year);
86 }
87 break;
88 case 'C':
89 if (repeat_count > 1) break;
90 result += std::to_string(dt.year/100);
91 break;
92 case 'd':
93 if (repeat_count < 2) {
94 char buffer[4] = {0};
95 snprintf(buffer, sizeof(buffer),"%.2d", dt.day);
96 result += std::string(buffer);
97 }
98 break;
99 case 'D':
100 if (repeat_count == 1) {
101 // %m/%d/%y
102 char buffer[16] = {0};
103 snprintf(buffer, sizeof(buffer), "%.2d/%.2d/%.2d", dt.mon, dt.day, (int)(dt.year % 100LL));
104 result += std::string(buffer);
105 } else
106 if (repeat_count == 2) {
107 char buffer[4] = {0};
108 snprintf(buffer, sizeof(buffer),"%.2d", dt.day);
109 result += std::string(buffer);
110 }
111 break;
112 case 'e':
113 if (repeat_count == 1) {
114 char buffer[4] = {0};
115 snprintf(buffer, sizeof(buffer),"%2d", dt.day);
116 result += std::string(buffer);
117 }
118 break;
119 case 'E':
120 // %E: Modifier for alternative ("era-based") format.
121 // https://help.hcltechsw.com/onedb/1.0.0.1/gug/ids_gug_086.html#ids_gug_086
122 break;
123 case 'F':
124 if (repeat_count == 1) {
125 // %Y-%m-%d ISO 8601 date format
126 char buffer[32] = {0};
127 if (dt.year <= 9999 || dt.year >= 0) {
128 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d", (int)dt.year, dt.mon, dt.day);
129 } else
130 if (dt.year < 0) {
131 snprintf(buffer, sizeof(buffer), "-%lld-%.2d-%.2d", dt.year, dt.mon, dt.day);
132 } else {
133 snprintf(buffer, sizeof(buffer), "+%lld-%.2d-%.2d", dt.year, dt.mon, dt.day);
134 }
135 result += std::string(buffer);
136 }
137 case 'g':
138 // ISO 8601 week-based year without century (2-digit year).
139 break;
140 case 'G':
141 // ISO 8601 week-based year with century as a decimal number.
142 break;
143 case 'j':
144 if (repeat_count == 1) {
145 char buffer[4] = {0};
146 snprintf(buffer, sizeof(buffer), "%.3d", day_of_year(ts));
147 result += std::string(buffer);
148 }
149 break;
150 case 'k':
151 if (repeat_count == 1) {
152 char buffer[4] = {0};
153 snprintf(buffer, sizeof(buffer), "%2d", dt.hour);
154 result += std::string(buffer);
155 }
156 break;
157 case 'l':
158 if (repeat_count == 1) {
159 char buffer[4] = {0};
160 snprintf(buffer, sizeof(buffer), "%2d", hour24_to_12(dt.hour));
161 result += std::string(buffer);
162 }
163 break;
164 case 'm':
165 if (repeat_count == 1) {
166 char buffer[4] = {0};
167 snprintf(buffer, sizeof(buffer), "%.2d", dt.mon);
168 result += std::string(buffer);
169 } else
170 if (repeat_count == 2) {
171 char buffer[4] = {0};
172 snprintf(buffer, sizeof(buffer), "%.2d", dt.min);
173 result += std::string(buffer);
174 }
175 break;
176 case 'M':
177 if (repeat_count == 1) {
178 char buffer[4] = {0};
179 snprintf(buffer, sizeof(buffer), "%.2d", dt.min);
180 result += std::string(buffer);
181 } else
182 if (repeat_count == 2) {
183 char buffer[4] = {0};
184 snprintf(buffer, sizeof(buffer), "%.2d", dt.mon);
185 result += std::string(buffer);
186 } else
187 if (repeat_count == 3) {
188 result += to_str(static_cast<Month>(dt.mon), FormatType::UPPERCASE_NAME);
189 }
190 break;
191 case 'n':
192 result += "\n";
193 break;
194 case 'O':
195 // Modifier for using alternative numeric symbols.
196 break;
197 case 'p':
198 if (dt.hour < 12) result += "AM";
199 else result += "PM";
200 break;
201 case 'P':
202 if (dt.hour < 12) result += "am";
203 else result += "pm";
204 break;
205 case 'r':
206 if (repeat_count == 1) {
207 char buffer[16] = {0};
208 if (dt.hour < 12) snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d AM", hour24_to_12(dt.hour), dt.min, dt.sec);
209 else snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d PM", hour24_to_12(dt.hour), dt.min, dt.sec);
210 result += std::string(buffer);
211 break;
212 }
213 break;
214 case 'R':
215 // %H:%M
216 if (repeat_count == 1) {
217 char buffer[8] = {0};
218 snprintf(buffer, sizeof(buffer), "%.2d:%.2d", dt.hour, dt.min);
219 result += std::string(buffer);
220 }
221 break;
222 case 's':
223 if (repeat_count == 1) {
224 result += std::to_string(ts);
225 break;
226 }
227 if (repeat_count == 3) {
228 result += std::to_string(dt.ms);
229 break;
230 }
231 // to '%ss'
232 case 'S':
233 if (repeat_count <= 2) {
234 char buffer[4] = {0};
235 snprintf(buffer, sizeof(buffer), "%.2d", dt.sec);
236 result += std::string(buffer);
237 }
238 if (repeat_count == 3) {
239 result += std::to_string(dt.ms);
240 break;
241 }
242 break;
243 case 't':
244 if (repeat_count > 1) break;
245 result += "\t";
246 break;
247 case 'T':
248 // %H:%M:%S
249 if (repeat_count == 1) {
250 char buffer[16] = {0};
251 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d", dt.hour, dt.min, dt.sec);
252 result += std::string(buffer);
253 }
254 break;
255 case 'u':
256 if (repeat_count == 1) {
257 // Day of the week as a decimal number (1 to 7, Monday being 1).
258 int dw = day_of_week(dt.year, dt.mon, dt.day);
259 if (dw == 0) dw = 7;
260 result += std::to_string(dw);
261 }
262 break;
263 case 'U':
264 // Week number of the current year (00 to 53, starting with the first Sunday as week 01).
265 break;
266 case 'V':
267 // ISO 8601 week number of the current year (01 to 53, with specific rules).
268 break;
269 case 'w':
270 // Day of the week as a decimal number (0 to 6, Sunday being 0).
271 if (repeat_count == 1) {
272 result += std::to_string(day_of_week(dt.year, dt.mon, dt.day));
273 } else
274 if (repeat_count == 3) {
275 result += to_str(day_of_week(dt.year, dt.mon, dt.day), FormatType::SHORT_NAME);
276 }
277 break;
278 case 'W':
279 // Week number of the current year (00 to 53, starting with the first Monday as week 01).
280 if (repeat_count == 3) {
281 result += to_str(day_of_week(dt.year, dt.mon, dt.day), FormatType::UPPERCASE_NAME);
282 }
283 break;
284 case 'x':
285 // Preferred date representation for the current locale without the time.
286 break;
287 case 'X':
288 // Preferred time representation for the current locale without the date.
289 break;
290 case 'y':
291 if (repeat_count == 1) {
292 result += std::to_string(dt.year % 100);
293 }
294 break;
295 case 'Y':
296 if (repeat_count == 1) {
297 result += std::to_string(dt.year);
298 } else
299 if (repeat_count == 6) {
300 char buffer[32] = {0};
301 const int64_t mega_years = dt.year / 1000000;
302 const int64_t millennia = (dt.year - mega_years * 1000000) / 1000;
303 const int64_t centuries = dt.year - mega_years * 1000000 - millennia * 1000;
304 if (mega_years) {
305 if (millennia) {
306 snprintf(buffer, sizeof(buffer), "%lldM%lldK%.3lld", mega_years, std::abs(millennia), std::abs(centuries));
307 } else {
308 snprintf(buffer, sizeof(buffer), "%lldM%.3lld", mega_years, std::abs(centuries));
309 }
310 } else
311 if (millennia) {
312 snprintf(buffer, sizeof(buffer), "%lldK%.3lld", millennia, std::abs(centuries));
313 } else {
314 snprintf(buffer, sizeof(buffer), "%.4lld", dt.year);
315 }
316 result += std::string(buffer);
317 } else
318 if (repeat_count == 4) {
319 char buffer[8] = {0};
320 snprintf(buffer, sizeof(buffer), "%.4d", (int)(dt.year % 10000));
321 result += std::string(buffer);
322 } else
323 if (repeat_count == 2) {
324 char buffer[8] = {0};
325 snprintf(buffer, sizeof(buffer), "%.2d", (int)(dt.year % 100));
326 result += std::string(buffer);
327 }
328 break;
329 case 'z':
330 // +hhmm or -hhmm numeric timezone offset from UTC.
331 if (repeat_count == 1) {
332 TimeZoneStruct tz = to_time_zone_struct(utc_offset);
333 char buffer[16] = {0};
334 if (tz.is_positive) snprintf(buffer, sizeof(buffer), "+%.2d%.2d", tz.hour, tz.min);
335 else snprintf(buffer, sizeof(buffer), "-%.2d%.2d", tz.hour, tz.min);
336 result += std::string(buffer);
337 }
338 break;
339 case 'Z':
340 // Timezone name or abbreviation.
341
342 result += "UTC";
343 break;
344 case '+':
345 // Date and time in date(1) format (not supported in glibc2).
346 // Tue Jun 4 04:07:43 UTC 2024
347 break;
348 };
349 }
350
377 template<class T = ts_t>
378 const std::string to_string(
379 const std::string& format_str,
380 T timestamp,
381 tz_t utc_offset = 0) {
382 std::string result;
383 if (format_str.empty()) return result;
385
386 bool is_command = false;
387 size_t repeat_count = 0;
388 char last_char = format_str[0];
389 if (last_char != '%') result += last_char;
390 for (size_t i = 0; i < format_str.size(); ++i) {
391 const char& current_char = format_str[i];
392 if (!is_command) {
393 if (current_char == '%') {
394 ++repeat_count;
395 if (repeat_count == 2) {
396 result += current_char;
397 repeat_count = 0;
398 }
399 continue;
400 }
401 if (!repeat_count) {
402 result += current_char;
403 continue;
404 }
405 last_char = current_char;
406 is_command = true;
407 continue;
408 }
409 if (last_char == current_char) {
410 ++repeat_count;
411 continue;
412 }
413 process_format_impl(last_char, repeat_count, timestamp, utc_offset, dt, result);
414 repeat_count = 0;
415 is_command = false;
416 --i;
417 }
418 if (is_command) {
419 process_format_impl(last_char, repeat_count, timestamp, utc_offset, dt, result);
420 }
421 return result;
422 }
423
426 template<class T = ts_t>
427 inline const std::string to_str(
428 const std::string& format_str,
429 T timestamp,
430 tz_t utc_offset = 0) {
431 return to_string<T>(format_str, timestamp, utc_offset);
432 }
433
460 template<class T = ts_ms_t>
461 const std::string to_string_ms(
462 const std::string& format_str,
463 T timestamp,
464 tz_t utc_offset = 0) {
465 std::string result;
466 if (format_str.empty()) return result;
468
469 bool is_command = false;
470 size_t repeat_count = 0;
471 char last_char = format_str[0];
472 if (last_char != '%') result += last_char;
473 for (size_t i = 0; i < format_str.size(); ++i) {
474 const char& current_char = format_str[i];
475 if (!is_command) {
476 if (current_char == '%') {
477 ++repeat_count;
478 if (repeat_count == 2) {
479 result += current_char;
480 repeat_count = 0;
481 }
482 continue;
483 }
484 if (!repeat_count) {
485 result += current_char;
486 continue;
487 }
488 last_char = current_char;
489 is_command = true;
490 continue;
491 }
492 if (last_char == current_char) {
493 ++repeat_count;
494 continue;
495 }
496 process_format_impl(last_char, repeat_count, timestamp, utc_offset, dt, result);
497 repeat_count = 0;
498 is_command = false;
499 --i;
500 }
501 if (is_command) {
502 process_format_impl(last_char, repeat_count, timestamp, utc_offset, dt, result);
503 }
504 return result;
505 }
506
509 template<class T = ts_t>
510 inline const std::string to_str_ms(
511 const std::string& format_str,
512 T timestamp,
513 tz_t utc_offset = 0) {
514 return to_string_ms<T>(format_str, timestamp, utc_offset);
515 }
516
524 template<class T = ts_t>
525 inline const std::string to_iso8601(T ts) {
527 char buffer[32] = {0};
528 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
529 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);
530 } else {
531 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2dT%.2d:%.2d:%.2d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec);
532 }
533 return std::string(buffer);
534 }
535
543 template<class T = ts_t>
544 inline const std::string to_iso8601_date(T ts) {
546 char buffer[32] = {0};
547 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2d", dt.year, dt.mon, dt.day);
548 return std::string(buffer);
549 }
550
558 template<class T = ts_t>
559 inline const std::string to_iso8601_time(T ts) {
561 char buffer[32] = {0};
562 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
563 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d.%.3d", dt.hour, dt.min, dt.sec, dt.ms);
564 } else {
565 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d", dt.hour, dt.min, dt.sec);
566 }
567 return std::string(buffer);
568 }
569
577 template<class T = ts_t>
578 inline const std::string to_iso8601_time_utc(T ts) {
580 char buffer[32] = {0};
581 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
582 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d.%.3dZ", dt.hour, dt.min, dt.sec, dt.ms);
583 } else {
584 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2dZ", dt.hour, dt.min, dt.sec);
585 }
586 return std::string(buffer);
587 }
588
596 template<class T = ts_t>
597 inline const std::string to_iso8601_utc(T ts) {
599 char buffer[32] = {0};
600 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
601 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);
602 } else {
603 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2dT%.2d:%.2d:%.2dZ", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec);
604 }
605 return std::string(buffer);
606 }
607
614 inline const std::string to_iso8601_utc_ms(ts_ms_t ts_ms) {
616 char buffer[32] = {0};
617 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);
618 return std::string(buffer);
619 }
620
627 inline const std::string to_iso8601_ms(ts_ms_t ts_ms) {
629 char buffer[32] = {0};
630 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);
631 return std::string(buffer);
632 }
633
642 template<class T = ts_t>
643 inline const std::string to_iso8601(T ts, tz_t utc_offset) {
644 TimeZoneStruct tz = to_time_zone(utc_offset);
646 char buffer[32] = {0};
647 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
648 if (tz.is_positive) {
649 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);
650 } else {
651 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);
652 }
653 } else {
654 if (tz.is_positive) {
655 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);
656 } else {
657 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);
658 }
659 }
660 return std::string(buffer);
661 }
662
670 inline const std::string to_iso8601_ms(ts_ms_t ts_ms, tz_t utc_offset) {
671 TimeZoneStruct tz = to_time_zone(utc_offset);
673 char buffer[32] = {0};
674 if (tz.is_positive) {
675 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);
676 } else {
677 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);
678 }
679 return std::string(buffer);
680 }
681
688 inline const std::string to_mql5_date_time(ts_t ts) {
690 char buffer[32] = {0};
691 snprintf(buffer, sizeof(buffer), "%lld.%.2d.%.2d %.2d:%.2d:%.2d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec);
692 return std::string(buffer);
693 }
694
697 inline const std::string to_mql5_full(ts_t ts) {
698 return to_mql5_date_time(ts);
699 }
700
707 inline const std::string to_mql5_date(ts_t ts) {
709 char buffer[32] = {0};
710 snprintf(buffer, sizeof(buffer), "%lld.%.2d.%.2d", dt.year, dt.mon, dt.day);
711 return std::string(buffer);
712 }
713
720 inline const std::string to_mql5_time(ts_t ts) {
722 char buffer[32] = {0};
723 snprintf(buffer, sizeof(buffer), "%.2d:%.2d:%.2d", dt.hour, dt.min, dt.sec);
724 return std::string(buffer);
725 }
726
730 inline const std::string to_windows_filename(ts_t ts) {
732 char buffer[32] = {0};
733 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2d_%.2d-%.2d-%.2d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec);
734 return std::string(buffer);
735 }
736
740 inline const std::string to_windows_filename_ms(ts_ms_t ts) {
742 char buffer[32] = {0};
743 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2d_%.2d-%.2d-%.2d-%.3d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec, dt.ms);
744 return std::string(buffer);
745 }
746
750 std::string to_human_readable(ts_t ts) {
752 char buffer[32] = {0};
753 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2d %.2d:%.2d:%.2d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec);
754 return std::string(buffer);
755 }
756
762 char buffer[32] = {0};
763 snprintf(buffer, sizeof(buffer), "%lld-%.2d-%.2d %.2d:%.2d:%.2d.%.3d", dt.year, dt.mon, dt.day, dt.hour, dt.min, dt.sec, dt.ms);
764 return std::string(buffer);
765 }
766
768
769}; // namespace time_shield
770
771#endif // _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
Header for date and time structure and related functions.
TIME_SHIELD_CONSTEXPR ts_t timestamp(year_t year, int month, int day)
Alias for to_timestamp.
const T to_time_zone(tz_t offset)
Converts an integer to a time zone structure.
T to_date_time_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
constexpr const T1 day_of_week(year_t year, int month, int day)
Alias for day_of_week_date.
TIME_SHIELD_CONSTEXPR const T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
constexpr ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
const T day_of_year(ts_t ts=time_shield::ts())
Get the day of the year.
const std::string & to_str(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
Definition enums.hpp:69
const std::string to_string(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp to string with custom format.
const std::string to_iso8601_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string.
const std::string to_mql5_date_time(ts_t ts)
Converts a timestamp to a string in MQL5 date and time format.
const std::string to_iso8601_time(T ts)
Converts a timestamp to an ISO8601 time string.
void process_format_impl(char last_char, size_t repeat_count, ts_t ts, tz_t utc_offset, const DateTimeStruct &dt, std::string &result)
const std::string to_windows_filename(ts_t ts)
Converts a timestamp in seconds to a Windows-compatible filename format.
const std::string to_iso8601_date(T ts)
Converts a timestamp to an ISO8601 date string.
const std::string to_mql5_date(ts_t ts)
Converts a timestamp to a string in MQL5 date format.
const std::string to_mql5_full(ts_t ts)
Alias for to_mql5_date_time_str function.
const std::string to_mql5_time(ts_t ts)
Converts a timestamp to a string in MQL5 time format.
const std::string to_str_ms(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Alias for to_string function.
std::string to_human_readable(ts_t ts)
Converts a timestamp in seconds to a human-readable format.
const std::string to_iso8601_utc_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string in UTC format.
const std::string to_iso8601(T ts)
Converts a timestamp to an ISO8601 string.
std::string to_human_readable_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a human-readable format.
const std::string to_iso8601_time_utc(T ts)
Converts a timestamp to an ISO8601 UTC time string.
const std::string to_string_ms(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp in milliseconds to string with custom format.
const std::string to_windows_filename_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a Windows-compatible filename format.
const std::string to_iso8601_utc(T ts)
Converts a timestamp to an ISO8601 string in UTC format.
const TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:45
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:57
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:46
Main namespace for the Time Shield library.
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.