8#ifndef _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
9#define _TIME_SHIELD_TIME_FORMATTING_HPP_INCLUDED
54 std::string& result) {
57 if (repeat_count > 1)
break;
61 if (repeat_count > 1)
break;
65 if (repeat_count == 1) {
68 result += std::string(buffer);
72 if (repeat_count <= 2) {
74 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
hour);
75 result += std::string(buffer);
79 if (repeat_count == 2) {
81 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
hour);
82 result += std::string(buffer);
87 if (repeat_count > 1)
break;
91 if (repeat_count > 1)
break;
97 if (repeat_count <= 1){
104 std::fill(buffer, buffer +
sizeof(buffer),
'\0');
105 snprintf(buffer,
sizeof(buffer),
"%2d ", dt.
day);
106 result += std::string(buffer);
108 std::fill(buffer, buffer +
sizeof(buffer),
'\0');
109 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d ", dt.
hour, dt.
min, dt.
sec);
110 result += std::string(buffer);
112 result += std::to_string(dt.
year);
116 if (repeat_count > 1)
break;
117 result += std::to_string(dt.
year/100);
120 if (repeat_count < 2) {
121 char buffer[4] = {0};
122 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
day);
123 result += std::string(buffer);
127 if (repeat_count == 1) {
129 char buffer[16] = {0};
130 snprintf(buffer,
sizeof(buffer),
"%.2d/%.2d/%.2d", dt.
mon, dt.
day, (
int)(dt.
year % 100LL));
131 result += std::string(buffer);
133 if (repeat_count == 2) {
134 char buffer[4] = {0};
135 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
day);
136 result += std::string(buffer);
140 if (repeat_count == 1) {
141 char buffer[4] = {0};
142 snprintf(buffer,
sizeof(buffer),
"%2d", dt.
day);
143 result += std::string(buffer);
151 if (repeat_count == 1) {
153 char buffer[32] = {0};
154 if (dt.
year <= 9999 || dt.
year >= 0) {
155 snprintf(buffer,
sizeof(buffer),
"%.4d-%.2d-%.2d", (
int)dt.
year, dt.
mon, dt.
day);
158 snprintf(buffer,
sizeof(buffer),
"-%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
160 snprintf(buffer,
sizeof(buffer),
"+%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
162 result += std::string(buffer);
171 if (repeat_count == 1) {
172 char buffer[4] = {0};
174 result += std::string(buffer);
178 if (repeat_count == 1) {
179 char buffer[4] = {0};
180 snprintf(buffer,
sizeof(buffer),
"%2d", dt.
hour);
181 result += std::string(buffer);
185 if (repeat_count == 1) {
186 char buffer[4] = {0};
188 result += std::string(buffer);
192 if (repeat_count == 1) {
193 char buffer[4] = {0};
194 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
mon);
195 result += std::string(buffer);
197 if (repeat_count == 2) {
198 char buffer[4] = {0};
199 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
min);
200 result += std::string(buffer);
204 if (repeat_count == 1) {
205 char buffer[4] = {0};
206 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
min);
207 result += std::string(buffer);
209 if (repeat_count == 2) {
210 char buffer[4] = {0};
211 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
mon);
212 result += std::string(buffer);
214 if (repeat_count == 3) {
225 if (dt.
hour < 12) result +=
"AM";
229 if (dt.
hour < 12) result +=
"am";
233 if (repeat_count == 1) {
234 char buffer[16] = {0};
237 result += std::string(buffer);
243 if (repeat_count == 1) {
244 char buffer[8] = {0};
245 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d", dt.
hour, dt.
min);
246 result += std::string(buffer);
250 if (repeat_count == 1) {
251 result += std::to_string(
ts);
254 if (repeat_count == 3) {
255 result += std::to_string(dt.
ms);
260 if (repeat_count <= 2) {
261 char buffer[4] = {0};
262 snprintf(buffer,
sizeof(buffer),
"%.2d", dt.
sec);
263 result += std::string(buffer);
265 if (repeat_count == 3) {
266 result += std::to_string(dt.
ms);
271 if (repeat_count > 1)
break;
276 if (repeat_count == 1) {
277 char buffer[16] = {0};
278 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
279 result += std::string(buffer);
283 if (repeat_count == 1) {
287 result += std::to_string(dw);
298 if (repeat_count == 1) {
301 if (repeat_count == 3) {
307 if (repeat_count == 3) {
318 if (repeat_count == 1) {
319 result += std::to_string(dt.
year % 100);
323 if (repeat_count == 1) {
324 result += std::to_string(dt.
year);
326 if (repeat_count == 6) {
327 char buffer[32] = {0};
328 const int64_t mega_years = dt.
year / 1000000;
329 const int64_t millennia = (dt.
year - mega_years * 1000000) / 1000;
330 const int64_t centuries = dt.
year - mega_years * 1000000 - millennia * 1000;
333 snprintf(buffer,
sizeof(buffer),
"%lldM%lldK%.3lld", mega_years, std::abs(millennia), std::abs(centuries));
335 snprintf(buffer,
sizeof(buffer),
"%lldM%.3lld", mega_years, std::abs(centuries));
339 snprintf(buffer,
sizeof(buffer),
"%lldK%.3lld", millennia, std::abs(centuries));
341 snprintf(buffer,
sizeof(buffer),
"%.4lld", dt.
year);
343 result += std::string(buffer);
345 if (repeat_count == 4) {
346 char buffer[8] = {0};
347 snprintf(buffer,
sizeof(buffer),
"%.4d", (
int)(dt.
year % 10000));
348 result += std::string(buffer);
350 if (repeat_count == 2) {
351 char buffer[8] = {0};
352 snprintf(buffer,
sizeof(buffer),
"%.2d", (
int)(dt.
year % 100));
353 result += std::string(buffer);
358 if (repeat_count == 1) {
360 char buffer[16] = {0};
362 else snprintf(buffer,
sizeof(buffer),
"-%.2d%.2d", tz.
hour, tz.
min);
363 result += std::string(buffer);
404 template<
class T = ts_t>
406 const std::string& format_str,
408 tz_t utc_offset = 0) {
410 if (format_str.empty())
return result;
413 bool is_command =
false;
414 size_t repeat_count = 0;
415 char last_char = format_str[0];
416 if (last_char !=
'%') result += last_char;
417 for (
size_t i = 0; i < format_str.size(); ++i) {
418 const char& current_char = format_str[i];
420 if (current_char ==
'%') {
422 if (repeat_count == 2) {
423 result += current_char;
429 result += current_char;
432 last_char = current_char;
436 if (last_char == current_char) {
453 template<
class T = ts_t>
455 const std::string& format_str,
457 tz_t utc_offset = 0) {
487 template<
class T = ts_ms_t>
489 const std::string& format_str,
491 tz_t utc_offset = 0) {
493 if (format_str.empty())
return result;
496 bool is_command =
false;
497 size_t repeat_count = 0;
498 char last_char = format_str[0];
499 if (last_char !=
'%') result += last_char;
500 for (
size_t i = 0; i < format_str.size(); ++i) {
501 const char& current_char = format_str[i];
503 if (current_char ==
'%') {
505 if (repeat_count == 2) {
506 result += current_char;
512 result += current_char;
515 last_char = current_char;
519 if (last_char == current_char) {
536 template<
class T = ts_t>
538 const std::string& format_str,
540 tz_t utc_offset = 0) {
551 template<
class T = ts_t>
554 char buffer[32] = {0};
555 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
556 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);
558 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
560 return std::string(buffer);
570 template<
class T = ts_t>
573 char buffer[32] = {0};
574 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2d", dt.
year, dt.
mon, dt.
day);
575 return std::string(buffer);
585 template<
class T = ts_t>
588 char buffer[32] = {0};
589 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
590 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d.%.3d", dt.
hour, dt.
min, dt.
sec, dt.
ms);
592 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
594 return std::string(buffer);
604 template<
class T = ts_t>
607 char buffer[32] = {0};
608 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
609 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d.%.3dZ", dt.
hour, dt.
min, dt.
sec, dt.
ms);
611 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2dZ", dt.
hour, dt.
min, dt.
sec);
613 return std::string(buffer);
623 template<
class T = ts_t>
626 char buffer[32] = {0};
627 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
628 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);
630 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2dT%.2d:%.2d:%.2dZ", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
632 return std::string(buffer);
643 char buffer[32] = {0};
644 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);
645 return std::string(buffer);
656 char buffer[32] = {0};
657 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);
658 return std::string(buffer);
669 template<
class T = ts_t>
673 char buffer[32] = {0};
674 if TIME_SHIELD_IF_CONSTEXPR (std::is_floating_point<T>::value) {
676 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 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);
682 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);
684 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);
687 return std::string(buffer);
700 char buffer[32] = {0};
702 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);
704 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);
706 return std::string(buffer);
717 char buffer[32] = {0};
718 snprintf(buffer,
sizeof(buffer),
"%lld.%.2d.%.2d %.2d:%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
719 return std::string(buffer);
736 char buffer[32] = {0};
737 snprintf(buffer,
sizeof(buffer),
"%lld.%.2d.%.2d", dt.
year, dt.
mon, dt.
day);
738 return std::string(buffer);
749 char buffer[32] = {0};
750 snprintf(buffer,
sizeof(buffer),
"%.2d:%.2d:%.2d", dt.
hour, dt.
min, dt.
sec);
751 return std::string(buffer);
759 char buffer[32] = {0};
760 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2d_%.2d-%.2d-%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
761 return std::string(buffer);
769 char buffer[32] = {0};
770 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);
771 return std::string(buffer);
779 char buffer[32] = {0};
780 snprintf(buffer,
sizeof(buffer),
"%lld-%.2d-%.2d %.2d:%.2d:%.2d", dt.
year, dt.
mon, dt.
day, dt.
hour, dt.
min, dt.
sec);
781 return std::string(buffer);
789 char buffer[32] = {0};
790 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);
791 return std::string(buffer);
Header for date and time structure and related functions.
const TimeZoneStruct to_time_zone(tz_t offset)
Converts an integer to a time zone structure.
constexpr const T1 day_of_week(T2 year, T3 month, T3 day)
Alias for day_of_week_date function.
TIME_SHIELD_CONSTEXPR const T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
const TimeZoneStruct to_time_zone_struct(tz_t offset)
Converts an integer to a TimeZoneStruct.
const T day_of_year(ts_t ts=ts())
Get the day of the year.
Month
Enumeration of the months of the year.
const std::string & to_str(Weekday value, FormatType format=UPPERCASE_NAME)
Converts a Weekday enum value to a string.
@ UPPERCASE_NAME
Uppercase short name.
T to_date_time_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a date-time structure with milliseconds.
T1 to_date_time(T2 ts)
Converts a timestamp to a date-time structure.
int64_t ts_t
Type for representing timestamps in seconds.
int tz_t
Type for representing time zone offsets in minutes.
int64_t ts_ms_t
Type for representing timestamps in milliseconds.
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 ts_t ts() noexcept
Get the current UTC timestamp in seconds.
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.