| 1 |
/* |
| 2 |
* mptTime.cpp |
| 3 |
* ----------- |
| 4 |
* Purpose: Various time utility functions. |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#include "stdafx.h" |
| 12 |
#include "mptTime.h" |
| 13 |
|
| 14 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 15 |
#include "mpt/osinfo/windows_wine_version.hpp" |
| 16 |
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS |
| 17 |
|
| 18 |
#include "mptStringBuffer.h" |
| 19 |
|
| 20 |
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO_DATE) |
| 21 |
#include <chrono> |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 25 |
#include <optional> |
| 26 |
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS |
| 27 |
|
| 28 |
#if defined(MPT_FALLBACK_TIMEZONE_C) |
| 29 |
#include <ctime> |
| 30 |
#endif // MPT_FALLBACK_TIMEZONE_C |
| 31 |
|
| 32 |
#if MPT_OS_WINDOWS |
| 33 |
#include <windows.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
OPENMPT_NAMESPACE_BEGIN |
| 38 |
|
| 39 |
|
| 40 |
namespace mpt |
| 41 |
{ |
| 42 |
namespace Date |
| 43 |
{ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#if defined(MODPLUG_TRACKER) |
| 48 |
|
| 49 |
#if MPT_OS_WINDOWS |
| 50 |
|
| 51 |
namespace ANSI |
| 52 |
{ |
| 53 |
|
| 54 |
uint64 Now() |
| 55 |
{ |
| 56 |
FILETIME filetime; |
| 57 |
#if MPT_WIN_AT_LEAST(MPT_WIN_8) |
| 58 |
GetSystemTimePreciseAsFileTime(&filetime); |
| 59 |
#else |
| 60 |
GetSystemTimeAsFileTime(&filetime); |
| 61 |
#endif |
| 62 |
return ((uint64)filetime.dwHighDateTime << 32 | filetime.dwLowDateTime); |
| 63 |
} |
| 64 |
|
| 65 |
mpt::ustring ToUString(uint64 time100ns) |
| 66 |
{ |
| 67 |
constexpr std::size_t bufsize = 256; |
| 68 |
|
| 69 |
mpt::ustring result; |
| 70 |
|
| 71 |
FILETIME filetime; |
| 72 |
SYSTEMTIME systime; |
| 73 |
filetime.dwHighDateTime = (DWORD)(((uint64)time100ns) >> 32); |
| 74 |
filetime.dwLowDateTime = (DWORD)((uint64)time100ns); |
| 75 |
FileTimeToSystemTime(&filetime, &systime); |
| 76 |
|
| 77 |
TCHAR buf[bufsize]; |
| 78 |
|
| 79 |
GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systime, TEXT("yyyy-MM-dd"), buf, bufsize); |
| 80 |
result.append(mpt::ToUnicode(mpt::String::ReadWinBuf(buf))); |
| 81 |
|
| 82 |
result.append(U_(" ")); |
| 83 |
|
| 84 |
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &systime, TEXT("HH:mm:ss"), buf, bufsize); |
| 85 |
result.append(mpt::ToUnicode(mpt::String::ReadWinBuf(buf))); |
| 86 |
|
| 87 |
result.append(U_(".")); |
| 88 |
|
| 89 |
result.append(mpt::ufmt::dec0<3>((unsigned)systime.wMilliseconds)); |
| 90 |
|
| 91 |
return result; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
} // namespace ANSI |
| 96 |
|
| 97 |
#endif // MPT_OS_WINDOWS |
| 98 |
|
| 99 |
#endif // MODPLUG_TRACKER |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
namespace nochrono |
| 104 |
{ |
| 105 |
|
| 106 |
static int32 ToDaynum(int32 year, int32 month, int32 day) |
| 107 |
{ |
| 108 |
month = (month + 9) % 12; |
| 109 |
year = year - (month / 10); |
| 110 |
int32 daynum = year*365 + year/4 - year/100 + year/400 + (month*306 + 5)/10 + (day - 1); |
| 111 |
return daynum; |
| 112 |
} |
| 113 |
|
| 114 |
static void FromDaynum(int32 d, int32 & year, int32 & month, int32 & day) |
| 115 |
{ |
| 116 |
int64 g = d; |
| 117 |
int64 y,ddd,mi,mm,dd; |
| 118 |
|
| 119 |
y = (10000*g + 14780)/3652425; |
| 120 |
ddd = g - (365*y + y/4 - y/100 + y/400); |
| 121 |
if(ddd < 0) |
| 122 |
{ |
| 123 |
y = y - 1; |
| 124 |
ddd = g - (365*y + y/4 - y/100 + y/400); |
| 125 |
} |
| 126 |
mi = (100*ddd + 52)/3060; |
| 127 |
mm = (mi + 2)%12 + 1; |
| 128 |
y = y + (mi + 2)/12; |
| 129 |
dd = ddd - (mi*306 + 5)/10 + 1; |
| 130 |
|
| 131 |
year = static_cast<int32>(y); |
| 132 |
month = static_cast<int32>(mm); |
| 133 |
day = static_cast<int32>(dd); |
| 134 |
} |
| 135 |
|
| 136 |
Unix UnixFromUTC(UTC timeUtc) |
| 137 |
{ |
| 138 |
int32 daynum = ToDaynum(timeUtc.year, timeUtc.month, timeUtc.day); |
| 139 |
int64 seconds = static_cast<int64>(daynum - ToDaynum(1970, 1, 1)) * 24 * 60 * 60 + timeUtc.hours * 60 * 60 + timeUtc.minutes * 60 + timeUtc.seconds; |
| 140 |
return Unix{seconds}; |
| 141 |
} |
| 142 |
|
| 143 |
UTC UnixAsUTC(Unix tp) |
| 144 |
{ |
| 145 |
int64 tmp = tp.value; |
| 146 |
int64 seconds = tmp % 60; tmp /= 60; |
| 147 |
int64 minutes = tmp % 60; tmp /= 60; |
| 148 |
int64 hours = tmp % 24; tmp /= 24; |
| 149 |
int32 year = 0, month = 0, day = 0; |
| 150 |
FromDaynum(static_cast<int32>(tmp) + ToDaynum(1970,1,1), year, month, day); |
| 151 |
UTC result = {}; |
| 152 |
result.year = year; |
| 153 |
result.month = month; |
| 154 |
result.day = day; |
| 155 |
result.hours = static_cast<int32>(hours); |
| 156 |
result.minutes = static_cast<int32>(minutes); |
| 157 |
result.seconds = seconds; |
| 158 |
return result; |
| 159 |
} |
| 160 |
|
| 161 |
#if defined(MODPLUG_TRACKER) |
| 162 |
|
| 163 |
struct tz_error |
| 164 |
{ |
| 165 |
}; |
| 166 |
|
| 167 |
Unix UnixFromLocal(Local timeLocal) |
| 168 |
{ |
| 169 |
#if defined(MPT_FALLBACK_TIMEZONE_WINDOWS_HISTORIC) |
| 170 |
try |
| 171 |
{ |
| 172 |
if(mpt::osinfo::windows::current_is_wine()) |
| 173 |
{ |
| 174 |
throw tz_error{}; |
| 175 |
} |
| 176 |
SYSTEMTIME sys_local{}; |
| 177 |
sys_local.wYear = static_cast<uint16>(timeLocal.year); |
| 178 |
sys_local.wMonth = static_cast<uint16>(timeLocal.month); |
| 179 |
sys_local.wDay = static_cast<uint16>(timeLocal.day); |
| 180 |
sys_local.wHour = static_cast<uint16>(timeLocal.hours); |
| 181 |
sys_local.wMinute = static_cast<uint16>(timeLocal.minutes); |
| 182 |
sys_local.wSecond = static_cast<uint16>(timeLocal.seconds); |
| 183 |
sys_local.wMilliseconds = 0; |
| 184 |
DYNAMIC_TIME_ZONE_INFORMATION dtzi{}; |
| 185 |
if(GetDynamicTimeZoneInformation(&dtzi) == TIME_ZONE_ID_INVALID) // WinVista |
| 186 |
{ |
| 187 |
throw tz_error{}; |
| 188 |
} |
| 189 |
SYSTEMTIME sys_utc{}; |
| 190 |
if(TzSpecificLocalTimeToSystemTimeEx(&dtzi, &sys_local, &sys_utc) == FALSE) // Win7/Win8 |
| 191 |
{ |
| 192 |
throw tz_error{}; |
| 193 |
} |
| 194 |
FILETIME ft{}; |
| 195 |
if(SystemTimeToFileTime(&sys_utc, &ft) == FALSE) // Win 2000 |
| 196 |
{ |
| 197 |
throw tz_error{}; |
| 198 |
} |
| 199 |
ULARGE_INTEGER time_value{}; |
| 200 |
time_value.LowPart = ft.dwLowDateTime; |
| 201 |
time_value.HighPart = ft.dwHighDateTime; |
| 202 |
return UnixFromSeconds(static_cast<int64>((time_value.QuadPart - 116444736000000000LL) / 10000000LL)); |
| 203 |
} catch(const tz_error &) |
| 204 |
{ |
| 205 |
// nothing |
| 206 |
} |
| 207 |
#endif |
| 208 |
#if defined(MPT_FALLBACK_TIMEZONE_WINDOWS_CURRENT) |
| 209 |
try |
| 210 |
{ |
| 211 |
SYSTEMTIME sys_local{}; |
| 212 |
sys_local.wYear = static_cast<uint16>(timeLocal.year); |
| 213 |
sys_local.wMonth = static_cast<uint16>(timeLocal.month); |
| 214 |
sys_local.wDay = static_cast<uint16>(timeLocal.day); |
| 215 |
sys_local.wHour = static_cast<uint16>(timeLocal.hours); |
| 216 |
sys_local.wMinute = static_cast<uint16>(timeLocal.minutes); |
| 217 |
sys_local.wSecond = static_cast<uint16>(timeLocal.seconds); |
| 218 |
sys_local.wMilliseconds = 0; |
| 219 |
SYSTEMTIME sys_utc{}; |
| 220 |
if(TzSpecificLocalTimeToSystemTime(NULL, &sys_local, &sys_utc) == FALSE) // WinXP |
| 221 |
{ |
| 222 |
throw tz_error{}; |
| 223 |
} |
| 224 |
FILETIME ft{}; |
| 225 |
if(SystemTimeToFileTime(&sys_utc, &ft) == FALSE) // Win 2000 |
| 226 |
{ |
| 227 |
throw tz_error{}; |
| 228 |
} |
| 229 |
ULARGE_INTEGER time_value{}; |
| 230 |
time_value.LowPart = ft.dwLowDateTime; |
| 231 |
time_value.HighPart = ft.dwHighDateTime; |
| 232 |
return UnixFromSeconds(static_cast<int64>((time_value.QuadPart - 116444736000000000LL) / 10000000LL)); |
| 233 |
} catch(const tz_error &) |
| 234 |
{ |
| 235 |
// nothing |
| 236 |
} |
| 237 |
#endif |
| 238 |
#if defined(MPT_FALLBACK_TIMEZONE_C) |
| 239 |
std::tm tmp{}; |
| 240 |
tmp.tm_year = timeLocal.year - 1900; |
| 241 |
tmp.tm_mon = timeLocal.month - 1; |
| 242 |
tmp.tm_mday = timeLocal.day; |
| 243 |
tmp.tm_hour = timeLocal.hours; |
| 244 |
tmp.tm_min = timeLocal.minutes; |
| 245 |
tmp.tm_sec = static_cast<int>(timeLocal.seconds); |
| 246 |
return UnixFromSeconds(static_cast<int64>(std::mktime(&tmp))); |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
Local UnixAsLocal(Unix tp) |
| 251 |
{ |
| 252 |
#if defined(MPT_FALLBACK_TIMEZONE_WINDOWS_HISTORIC) |
| 253 |
try |
| 254 |
{ |
| 255 |
if(mpt::osinfo::windows::current_is_wine()) |
| 256 |
{ |
| 257 |
throw tz_error{}; |
| 258 |
} |
| 259 |
ULARGE_INTEGER time_value{}; |
| 260 |
time_value.QuadPart = static_cast<int64>(UnixAsSeconds(tp)) * 10000000LL + 116444736000000000LL; |
| 261 |
FILETIME ft{}; |
| 262 |
ft.dwLowDateTime = time_value.LowPart; |
| 263 |
ft.dwHighDateTime = time_value.HighPart; |
| 264 |
SYSTEMTIME sys_utc{}; |
| 265 |
if(FileTimeToSystemTime(&ft, &sys_utc) == FALSE) // WinXP |
| 266 |
{ |
| 267 |
throw tz_error{}; |
| 268 |
} |
| 269 |
DYNAMIC_TIME_ZONE_INFORMATION dtzi{}; |
| 270 |
if(GetDynamicTimeZoneInformation(&dtzi) == TIME_ZONE_ID_INVALID) // WinVista |
| 271 |
{ |
| 272 |
throw tz_error{}; |
| 273 |
} |
| 274 |
SYSTEMTIME sys_local{}; |
| 275 |
if(SystemTimeToTzSpecificLocalTimeEx(&dtzi, &sys_utc, &sys_local) == FALSE) // Win7/Win8 |
| 276 |
{ |
| 277 |
throw tz_error{}; |
| 278 |
} |
| 279 |
Local result{}; |
| 280 |
result.year = sys_local.wYear; |
| 281 |
result.month = sys_local.wMonth; |
| 282 |
result.day = sys_local.wDay; |
| 283 |
result.hours = sys_local.wHour; |
| 284 |
result.minutes = sys_local.wMinute; |
| 285 |
result.seconds = sys_local.wSecond; |
| 286 |
return result; |
| 287 |
} catch(const tz_error&) |
| 288 |
{ |
| 289 |
// nothing |
| 290 |
} |
| 291 |
#endif |
| 292 |
#if defined(MPT_FALLBACK_TIMEZONE_WINDOWS_CURRENT) |
| 293 |
try |
| 294 |
{ |
| 295 |
ULARGE_INTEGER time_value{}; |
| 296 |
time_value.QuadPart = static_cast<int64>(UnixAsSeconds(tp)) * 10000000LL + 116444736000000000LL; |
| 297 |
FILETIME ft{}; |
| 298 |
ft.dwLowDateTime = time_value.LowPart; |
| 299 |
ft.dwHighDateTime = time_value.HighPart; |
| 300 |
SYSTEMTIME sys_utc{}; |
| 301 |
if(FileTimeToSystemTime(&ft, &sys_utc) == FALSE) // WinXP |
| 302 |
{ |
| 303 |
throw tz_error{}; |
| 304 |
} |
| 305 |
SYSTEMTIME sys_local{}; |
| 306 |
if(SystemTimeToTzSpecificLocalTime(NULL, &sys_utc, &sys_local) == FALSE) // Win2000 |
| 307 |
{ |
| 308 |
throw tz_error{}; |
| 309 |
} |
| 310 |
Local result{}; |
| 311 |
result.year = sys_local.wYear; |
| 312 |
result.month = sys_local.wMonth; |
| 313 |
result.day = sys_local.wDay; |
| 314 |
result.hours = sys_local.wHour; |
| 315 |
result.minutes = sys_local.wMinute; |
| 316 |
result.seconds = sys_local.wSecond; |
| 317 |
return result; |
| 318 |
} catch(const tz_error&) |
| 319 |
{ |
| 320 |
// nothing |
| 321 |
} |
| 322 |
#endif |
| 323 |
#if defined(MPT_FALLBACK_TIMEZONE_C) |
| 324 |
std::time_t time_tp = static_cast<std::time_t>(UnixAsSeconds(tp)); |
| 325 |
std::tm *tmp = std::localtime(&time_tp); |
| 326 |
if(!tmp) |
| 327 |
{ |
| 328 |
return Local{}; |
| 329 |
} |
| 330 |
std::tm local = *tmp; |
| 331 |
Local result{}; |
| 332 |
result.year = local.tm_year + 1900; |
| 333 |
result.month = local.tm_mon + 1; |
| 334 |
result.day = local.tm_mday; |
| 335 |
result.hours = local.tm_hour; |
| 336 |
result.minutes = local.tm_min; |
| 337 |
result.seconds = local.tm_sec; |
| 338 |
return result; |
| 339 |
#endif |
| 340 |
} |
| 341 |
|
| 342 |
#endif // MODPLUG_TRACKER |
| 343 |
|
| 344 |
} // namespace nochrono |
| 345 |
|
| 346 |
template <LogicalTimezone TZ> |
| 347 |
static mpt::ustring ToShortenedISO8601Impl(mpt::Date::Gregorian<TZ> date) |
| 348 |
{ |
| 349 |
mpt::ustring result; |
| 350 |
mpt::ustring tz; |
| 351 |
if constexpr(TZ == LogicalTimezone::Unspecified) |
| 352 |
{ |
| 353 |
tz = U_(""); |
| 354 |
} else if constexpr(TZ == LogicalTimezone::UTC) |
| 355 |
{ |
| 356 |
tz = U_("Z"); |
| 357 |
} else |
| 358 |
{ |
| 359 |
tz = U_(""); |
| 360 |
} |
| 361 |
if(date.year == 0) |
| 362 |
{ |
| 363 |
return result; |
| 364 |
} |
| 365 |
result += mpt::ufmt::dec0<4>(date.year); |
| 366 |
result += U_("-") + mpt::ufmt::dec0<2>(date.month); |
| 367 |
result += U_("-") + mpt::ufmt::dec0<2>(date.day); |
| 368 |
if(date.hours == 0 && date.minutes == 0 && date.seconds) |
| 369 |
{ |
| 370 |
return result; |
| 371 |
} |
| 372 |
result += U_("T"); |
| 373 |
result += mpt::ufmt::dec0<2>(date.hours) + U_(":") + mpt::ufmt::dec0<2>(date.minutes); |
| 374 |
if(date.seconds == 0) |
| 375 |
{ |
| 376 |
return result + tz; |
| 377 |
} |
| 378 |
result += U_(":") + mpt::ufmt::dec0<2>(date.seconds); |
| 379 |
result += tz; |
| 380 |
return result; |
| 381 |
} |
| 382 |
|
| 383 |
mpt::ustring ToShortenedISO8601(mpt::Date::AnyGregorian date) |
| 384 |
{ |
| 385 |
return ToShortenedISO8601Impl(date); |
| 386 |
} |
| 387 |
|
| 388 |
mpt::ustring ToShortenedISO8601(mpt::Date::UTC date) |
| 389 |
{ |
| 390 |
return ToShortenedISO8601Impl(date); |
| 391 |
} |
| 392 |
|
| 393 |
#ifdef MODPLUG_TRACKER |
| 394 |
mpt::ustring ToShortenedISO8601(Local date) |
| 395 |
{ |
| 396 |
return ToShortenedISO8601Impl(date); |
| 397 |
} |
| 398 |
#endif // MODPLUG_TRACKER |
| 399 |
|
| 400 |
} // namespace Date |
| 401 |
} // namespace mpt |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
OPENMPT_NAMESPACE_END |