58 inline static std::time_t
makeTimeT(
int year,
int month,
int day)
61 tm.tm_year = year - 1900;
62 tm.tm_mon = month - 1;
69 return std::mktime(&tm);
72#ifdef MUSX_RUNNING_ON_WINDOWS
88 st.wYear = WORD(tm.tm_year + 1900);
89 st.wMonth = WORD(tm.tm_mon + 1);
90 st.wDay = WORD(tm.tm_mday);
91 st.wHour = WORD(tm.tm_hour);
92 st.wMinute = WORD(tm.tm_min);
93 st.wSecond = WORD(tm.tm_sec);
99 const wchar_t* customFormat =
nullptr;
103 case DateFormat::Short:
104 flags = DATE_SHORTDATE;
107 case DateFormat::Long:
108 flags = DATE_LONGDATE;
111 case DateFormat::Abbrev:
113 customFormat = L
"MMM d, yyyy";
118 int requiredSize = GetDateFormatEx(
119 nullptr, flags, &st, customFormat,
nullptr, 0,
nullptr);
121 if (requiredSize == 0) {
122 throw std::system_error(GetLastError(), std::system_category());
126 std::wstring wbuf(requiredSize, L
'\0');
129 int result = GetDateFormatEx(
130 nullptr, flags, &st, customFormat, wbuf.data(), requiredSize,
nullptr);
133 throw std::system_error(GetLastError(), std::system_category());
137 int utf8len = WideCharToMultiByte(CP_UTF8, 0, wbuf.c_str(), -1,
nullptr, 0,
nullptr,
nullptr);
138 std::string output(utf8len,
'\0');
139 WideCharToMultiByte(CP_UTF8, 0, wbuf.c_str(), -1, &output[0], utf8len,
nullptr,
nullptr);
140 output.resize(utf8len - 1);
146#ifndef MUSX_RUNNING_ON_WINDOWS
160 localtime_r(&t, &tm);
162 const char* fmt =
nullptr;
164 case DateFormat::Short:
167 case DateFormat::Long:
170 case DateFormat::Abbrev:
175 std::ostringstream oss;
176 oss.imbue(std::locale(
""));
178 oss << std::put_time(&tm, fmt);
193 inline static std::string
formatTime(std::time_t t,
bool includeSeconds)
195#ifdef MUSX_RUNNING_ON_WINDOWS
199 localtime_s(&tm, &t);
200 st.wYear = WORD(tm.tm_year + 1900);
201 st.wMonth = WORD(tm.tm_mon + 1);
202 st.wDay = WORD(tm.tm_mday);
203 st.wHour = WORD(tm.tm_hour);
204 st.wMinute = WORD(tm.tm_min);
205 st.wSecond = WORD(tm.tm_sec);
206 st.wMilliseconds = 0;
210 if (!includeSeconds) {
211 flags = TIME_NOSECONDS;
215 int requiredSize = GetTimeFormatEx(
216 nullptr, flags, &st,
nullptr,
nullptr, 0);
218 if (requiredSize == 0) {
219 throw std::system_error(GetLastError(), std::system_category());
223 std::wstring wbuf(requiredSize, L
'\0');
226 int result = GetTimeFormatEx(
227 nullptr, flags, &st,
nullptr, wbuf.data(), requiredSize);
230 throw std::system_error(GetLastError(), std::system_category());
234 int utf8len = WideCharToMultiByte(CP_UTF8, 0, wbuf.c_str(), -1,
nullptr, 0,
nullptr,
nullptr);
235 std::string output(utf8len,
'\0');
236 WideCharToMultiByte(CP_UTF8, 0, wbuf.c_str(), -1, &output[0], utf8len,
nullptr,
nullptr);
237 output.resize(utf8len - 1);
244 localtime_r(&t, &tm);
246 const char* fmt =
nullptr;
248 if (includeSeconds) {
253 const char* tfmt = nl_langinfo(T_FMT);
254 if (tfmt && std::strchr(tfmt,
'H')) {
258 fmt = is24h ?
"%H:%M" :
"%I:%M %p";
261 std::ostringstream oss;
262 oss.imbue(std::locale(
""));
263 oss << std::put_time(&tm, fmt);
static std::time_t makeTimeT(int year, int month, int day)
Creates a std::time_t from integer year, month, and day.
Definition DateTimeFormat.h:58
static std::string formatDate(std::time_t t, DateFormat style)
Formats a date according to the current locale on POSIX systems.
Definition DateTimeFormat.h:156
static std::string formatTime(std::time_t t, bool includeSeconds)
Formats a time according to the current locale.
Definition DateTimeFormat.h:193