xtd 0.2.0
Loading...
Searching...
No Matches
__date_time_formatter.hpp
Go to the documentation of this file.
1
3#pragma once
5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
7#endif
9
10#include "../types.hpp"
12#include "__sprintf.hpp"
13
15#if defined(_WIN32)
16inline struct tm* __localtime_s(const time_t* timer, struct tm* buf) noexcept {
17 localtime_s(buf, timer);
18 return buf;
19}
20#else
21inline struct tm* __localtime_s(const time_t* timer, struct tm* buf) noexcept {
22 return localtime_r(timer, buf);
23}
24#endif
25
26template<typename char_t>
27inline std::basic_string<char_t> __to_string(const char_t* fmt, const std::tm& value, const std::locale& loc) {
28 std::basic_stringstream<char_t> result;
29 result.imbue(loc);
30 result << std::put_time(&value, fmt);
31 return result.str();
32}
33
34inline std::string __date_time_formatter(std::string fmt, const std::tm& time, xtd::uint32 nanoseconds, const std::locale& loc) {
35 if (fmt.empty()) fmt = "G";
36 if (fmt.size() > 1) __format_exception("Invalid format");
37
38 switch (fmt[0]) {
39 case 'a': return __sprintf("%s", time.tm_hour / 12 ? "PM" : "AM");
40 case 'b': return __sprintf("%03d", nanoseconds / 1000000);
41 case 'B': return __sprintf("%d", nanoseconds / 1000000);
42 case 'd': return __sprintf("%02d/%02d/%d", time.tm_mon + 1, time.tm_mday, time.tm_year + 1900);
43 case 'D': return __sprintf("%d/%02d/%d", time.tm_mon + 1, time.tm_mday, time.tm_year + 1900);
44 case 'f': return __to_string("%Ec", time, loc);
45 case 'F': return __to_string("%c", time, loc);
46 case 'g': return __to_string("%Ec", time, loc);
47 case 'G': return __to_string("%c", time, loc);
48 case 'h': return __sprintf("%s", __to_string("%a", time, loc).c_str());
49 case 'H': return __sprintf("%s", __to_string("%A", time, loc).c_str());
50 case 'i': return __sprintf("%02d", time.tm_mday);
51 case 'I': return __sprintf("%d", time.tm_mday);
52 case 'j': return __sprintf("%s", __to_string("%b", time, loc).c_str());
53 case 'J': return __sprintf("%s", __to_string("%B", time, loc).c_str());
54 case 'k': return __sprintf("%02d", time.tm_mon + 1);
55 case 'K': return __sprintf("%d", time.tm_mon + 1);
56 case 'l': return __sprintf("%02d", time.tm_year % 100);
57 case 'L': return __sprintf("%d", time.tm_year + 1900);
58 case 'm':
59 case 'M': return __sprintf("%s %d", __to_string("%B", time, loc).c_str(), time.tm_mday);
60 case 'n': return __sprintf("%s, %d %s %d", __to_string("%A", time, loc).c_str(), time.tm_mday, __to_string("%B", time, loc).c_str(), time.tm_year + 1900);
61 case 'N': return __sprintf("%s, %d %s %d %d:%02d:%02d", __to_string("%A", time, loc).c_str(), time.tm_mday, __to_string("%B", time, loc).c_str(), time.tm_year + 1900, time.tm_hour, time.tm_min, time.tm_sec);
62 case 'o':
63 case 'O': return __sprintf("%d %s %d", time.tm_mday, __to_string("%B", time, loc).c_str(), time.tm_year + 1900);
64 case 's': return __sprintf("%d-%02d-%02dT%02d:%02d:%02d.%07d", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec, nanoseconds);
65 case 't': return __sprintf("%02d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
66 case 'T': return __sprintf("%d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
67 case 'u': return __sprintf("%d-%02d-%02d %02d:%02d:%02d", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
68 case 'U': return __sprintf("%s, %d %s %d %d:%02d:%02d", __to_string("%A", time, loc).c_str(), time.tm_mday, __to_string("%B", time, loc).c_str(), time.tm_year + 1900, time.tm_hour, time.tm_min, time.tm_sec);
69 case 'v': return __sprintf("%02d:%02d", time.tm_hour, time.tm_min);
70 case 'V': return __sprintf("%d:%02d", time.tm_hour, time.tm_min);
71 case 'w': return __sprintf("%02d}", time.tm_hour);
72 case 'W': return __sprintf("%d", time.tm_hour);
73 case 'x': return __sprintf("%02d", time.tm_hour % 12);
74 case 'X': return __sprintf("%d", time.tm_hour % 12);
75 case 'y': return __sprintf("%s %d", __to_string("%B", time, loc).c_str(), time.tm_year % 100);
76 case 'Y': return __sprintf("%s %d", __to_string("%B", time, loc).c_str(), time.tm_year + 1900);
77 case 'z':
78 case 'Z': return __to_string("%Z", time, loc);
79 default: __format_exception("Invalid format"); return {};
80 }
81}
82
83inline std::wstring __date_time_formatter(std::wstring fmt, const std::tm& time, xtd::uint32 nanoseconds, const std::locale& loc) {
84 if (fmt.empty()) fmt = L"G";
85 if (fmt.size() > 1) __format_exception("Invalid format");
86
87 switch (fmt[0]) {
88 case 'a': return __sprintf(L"%s", time.tm_hour / 12 ? "PM" : "AM");
89 case 'b': return __sprintf(L"%03d", nanoseconds / 1000000);
90 case 'B': return __sprintf(L"%d", nanoseconds / 1000000);
91 case 'd': return __sprintf(L"%02d/%02d/%d", time.tm_mon + 1, time.tm_mday, time.tm_year + 1900);
92 case 'D': return __sprintf(L"%d/%02d/%d", time.tm_mon + 1, time.tm_mday, time.tm_year + 1900);
93 case 'f': return __to_string(L"%Ec", time, loc);
94 case 'F': return __to_string(L"%c", time, loc);
95 case 'g': return __to_string(L"%Ec", time, loc);
96 case 'G': return __to_string(L"%c", time, loc);
97 case 'h': return __sprintf(L"%ls", __to_string(L"%a", time, loc).c_str());
98 case 'H': return __sprintf(L"%ls", __to_string(L"%A", time, loc).c_str());
99 case 'i': return __sprintf(L"%02d", time.tm_mday);
100 case 'I': return __sprintf(L"%d", time.tm_mday);
101 case 'j': return __sprintf(L"%ls", __to_string(L"%b", time, loc).c_str());
102 case 'J': return __sprintf(L"%ls", __to_string(L"%B", time, loc).c_str());
103 case 'k': return __sprintf(L"%02d", time.tm_mon + 1);
104 case 'K': return __sprintf(L"%d", time.tm_mon + 1);
105 case 'l': return __sprintf(L"%02d", time.tm_year % 100);
106 case 'L': return __sprintf(L"%04d", time.tm_year + 1900);
107 case 'm':
108 case 'M': return __sprintf(L"%ls %d", __to_string(L"%B", time, loc).c_str(), time.tm_mday);
109 case 'n': return __sprintf(L"%ls, %d %ls %d", __to_string(L"%A", time, loc).c_str(), time.tm_mday, __to_string(L"%B", time, loc).c_str(), time.tm_year + 1900);
110 case 'N': return __sprintf(L"%ls, %d %ls %d %d:%02d:%02d", __to_string(L"%A", time, loc).c_str(), time.tm_mday, __to_string(L"%B", time, loc).c_str(), time.tm_year + 1900, time.tm_hour, time.tm_min, time.tm_sec);
111 case 'o':
112 case 'O': return __sprintf(L"%d %ls %d", time.tm_mday, __to_string(L"%B", time, loc).c_str(), time.tm_year + 1900);
113 case 's': return __sprintf(L"%d-%02d-%02dT%02d:%02d:%02d.%07d", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec, nanoseconds / 100);
114 case 't': return __sprintf(L"%02d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
115 case 'T': return __sprintf(L"%d:%02d:%02d", time.tm_hour, time.tm_min, time.tm_sec);
116 case 'u': return __sprintf(L"%d-%02d-%02d %02d:%02d:%02d", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
117 case 'U': return __sprintf(L"%ls, %d %ls %d %d:%02d:%02d", __to_string(L"%A", time, loc).c_str(), time.tm_mday, __to_string(L"%B", time, loc).c_str(), time.tm_year + 1900, time.tm_hour, time.tm_min, time.tm_sec);
118 case 'v': return __sprintf(L"%02d:%02d", time.tm_hour, time.tm_min);
119 case 'V': return __sprintf(L"%d:%02d", time.tm_hour, time.tm_min);
120 case 'w': return __sprintf(L"%02d}", time.tm_hour);
121 case 'W': return __sprintf(L"%d", time.tm_hour);
122 case 'x': return __sprintf(L"%02d", time.tm_hour % 12);
123 case 'X': return __sprintf(L"%d", time.tm_hour % 12);
124 case 'y': return __sprintf(L"%ls %d", __to_string(L"%B", time, loc).c_str(), time.tm_year % 100);
125 case 'Y': return __sprintf(L"%ls %d", __to_string(L"%B", time, loc).c_str(), time.tm_year + 1900);
126 case 'z':
127 case 'Z': return __to_string(L"%Z", time, loc);
128 default: __format_exception("Invalid format"); return {};
129 }
130}
131
132inline std::string __date_time_formatter(const std::string& fmt, time_t time, xtd::uint32 nanoseconds, const std::locale& loc) {
133 tm buf;
134 return __date_time_formatter(fmt, *__localtime_s(&time, &buf), nanoseconds, loc);
135}
136
137inline std::wstring __date_time_formatter(const std::wstring& fmt, time_t time, xtd::uint32 nanoseconds, const std::locale& loc) {
138 tm buf;
139 return __date_time_formatter(fmt, *__localtime_s(&time, &buf), nanoseconds, loc);
140}
Contains throw format exception method.
Contains __format method.
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
@ time
The date_time_picker control displays the date/time value in the time format set by the user's operat...