| 1 |
/* |
| 2 |
* mptTime.h |
| 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 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
#if defined(MODPLUG_TRACKER) |
| 16 |
#include "../misc/mptOS.h" |
| 17 |
#include <stdexcept> |
| 18 |
#endif |
| 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 |
#include <exception> |
| 23 |
#elif MPT_CXX_AT_LEAST(17) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO) && defined(MODPLUG_TRACKER) |
| 24 |
#include <chrono> |
| 25 |
#endif |
| 26 |
|
| 27 |
#if MPT_CXX_BEFORE(20) || defined(MPT_LIBCXX_QUIRK_NO_CHRONO) || defined(MPT_LIBCXX_QUIRK_NO_CHRONO_DATE) |
| 28 |
#include <ctime> |
| 29 |
#endif |
| 30 |
|
| 31 |
#if MPT_WINNT_AT_LEAST(MPT_WIN_8) |
| 32 |
#define MPT_FALLBACK_TIMEZONE_WINDOWS_HISTORIC |
| 33 |
#define MPT_FALLBACK_TIMEZONE_WINDOWS_CURRENT |
| 34 |
#define MPT_FALLBACK_TIMEZONE_C |
| 35 |
#elif MPT_WINNT_AT_LEAST(MPT_WIN_XP) |
| 36 |
#define MPT_FALLBACK_TIMEZONE_WINDOWS_CURRENT |
| 37 |
#define MPT_FALLBACK_TIMEZONE_C |
| 38 |
#else |
| 39 |
#define MPT_FALLBACK_TIMEZONE_C |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
OPENMPT_NAMESPACE_BEGIN |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#if defined(MODPLUG_TRACKER) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO) |
| 49 |
|
| 50 |
namespace mpt { |
| 51 |
namespace chrono { |
| 52 |
#if MPT_CXX_AT_LEAST(20) |
| 53 |
using days = std::chrono::days; |
| 54 |
using weeks = std::chrono::weeks; |
| 55 |
using years = std::chrono::years; |
| 56 |
using months = std::chrono::months; |
| 57 |
#else |
| 58 |
using days = std::chrono::duration<int, std::ratio_multiply<std::ratio<24>, std::chrono::hours::period>>; |
| 59 |
using weeks = std::chrono::duration<int, std::ratio_multiply<std::ratio<7>, mpt::chrono::days::period>>; |
| 60 |
using years = std::chrono::duration<int, std::ratio_multiply<std::ratio<146097, 400>, mpt::chrono::days::period>>; |
| 61 |
using months = std::chrono::duration<int, std::ratio_divide<mpt::chrono::years::period, std::ratio<12>>>; |
| 62 |
#endif |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#endif // !MPT_LIBCXX_QUIRK_NO_CHRONO |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
namespace mpt |
| 71 |
{ |
| 72 |
namespace Date |
| 73 |
{ |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#if defined(MODPLUG_TRACKER) |
| 78 |
|
| 79 |
#if MPT_OS_WINDOWS |
| 80 |
|
| 81 |
namespace ANSI |
| 82 |
{ |
| 83 |
// uint64 counts 100ns since 1601-01-01T00:00Z |
| 84 |
|
| 85 |
uint64 Now(); |
| 86 |
|
| 87 |
mpt::ustring ToUString(uint64 time100ns); // i.e. 2015-01-15 18:32:01.718 |
| 88 |
|
| 89 |
} // namespacee ANSI |
| 90 |
|
| 91 |
#endif // MPT_OS_WINDOWS |
| 92 |
|
| 93 |
#endif // MODPLUG_TRACKER |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
enum class LogicalTimezone |
| 98 |
{ |
| 99 |
Unspecified, |
| 100 |
UTC, |
| 101 |
#if defined(MODPLUG_TRACKER) |
| 102 |
Local, |
| 103 |
#endif // MODPLUG_TRACKER |
| 104 |
}; |
| 105 |
|
| 106 |
template <LogicalTimezone tz> |
| 107 |
struct Gregorian |
| 108 |
{ |
| 109 |
int year = 0; |
| 110 |
unsigned int month = 0; |
| 111 |
unsigned int day = 0; |
| 112 |
int32 hours = 0; |
| 113 |
int32 minutes = 0; |
| 114 |
int64 seconds = 0; |
| 115 |
friend bool operator==(const Gregorian<tz>& lhs, const Gregorian<tz>& rhs) |
| 116 |
{ |
| 117 |
return std::tie(lhs.year, lhs.month, lhs.day, lhs.hours, lhs.minutes, lhs.seconds) |
| 118 |
== std::tie(rhs.year, rhs.month, rhs.day, rhs.hours, rhs.minutes, rhs.seconds); |
| 119 |
} |
| 120 |
friend bool operator!=(const Gregorian<tz>& lhs, const Gregorian<tz>& rhs) |
| 121 |
{ |
| 122 |
return std::tie(lhs.year, lhs.month, lhs.day, lhs.hours, lhs.minutes, lhs.seconds) |
| 123 |
!= std::tie(rhs.year, rhs.month, rhs.day, rhs.hours, rhs.minutes, rhs.seconds); |
| 124 |
} |
| 125 |
friend bool operator<(const Gregorian<tz> &lhs, const Gregorian<tz> &rhs) |
| 126 |
{ |
| 127 |
return std::tie(lhs.year, lhs.month, lhs.day, lhs.hours, lhs.minutes, lhs.seconds) |
| 128 |
< std::tie(rhs.year, rhs.month, rhs.day, rhs.hours, rhs.minutes, rhs.seconds); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
using AnyGregorian = Gregorian<LogicalTimezone::Unspecified>; |
| 133 |
|
| 134 |
using UTC = Gregorian<LogicalTimezone::UTC>; |
| 135 |
|
| 136 |
#if defined(MODPLUG_TRACKER) |
| 137 |
using Local = Gregorian<LogicalTimezone::Local>; |
| 138 |
#endif // MODPLUG_TRACKER |
| 139 |
|
| 140 |
template <LogicalTimezone TZ> |
| 141 |
inline Gregorian<TZ> interpret_as_timezone(AnyGregorian gregorian) |
| 142 |
{ |
| 143 |
Gregorian<TZ> result; |
| 144 |
result.year = gregorian.year; |
| 145 |
result.month = gregorian.month; |
| 146 |
result.day = gregorian.day; |
| 147 |
result.hours = gregorian.hours; |
| 148 |
result.minutes = gregorian.minutes; |
| 149 |
result.seconds = gregorian.seconds; |
| 150 |
return result; |
| 151 |
} |
| 152 |
|
| 153 |
template <LogicalTimezone TZ> |
| 154 |
inline Gregorian<LogicalTimezone::Unspecified> forget_timezone(Gregorian<TZ> gregorian) |
| 155 |
{ |
| 156 |
Gregorian<LogicalTimezone::Unspecified> result; |
| 157 |
result.year = gregorian.year; |
| 158 |
result.month = gregorian.month; |
| 159 |
result.day = gregorian.day; |
| 160 |
result.hours = gregorian.hours; |
| 161 |
result.minutes = gregorian.minutes; |
| 162 |
result.seconds = gregorian.seconds; |
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
namespace nochrono |
| 167 |
{ |
| 168 |
|
| 169 |
// int64 counts 1s since 1970-01-01T00:00Z |
| 170 |
struct Unix |
| 171 |
{ |
| 172 |
int64 value{}; |
| 173 |
friend bool operator==(const Unix &a, const Unix &b) |
| 174 |
{ |
| 175 |
return a.value == b.value; |
| 176 |
} |
| 177 |
friend bool operator!=(const Unix &a, const Unix &b) |
| 178 |
{ |
| 179 |
return a.value != b.value; |
| 180 |
} |
| 181 |
}; |
| 182 |
|
| 183 |
#if MPT_CXX_BEFORE(20) || defined(MPT_LIBCXX_QUIRK_NO_CHRONO) || defined(MPT_LIBCXX_QUIRK_NO_CHRONO_DATE) |
| 184 |
|
| 185 |
inline Unix UnixNow() |
| 186 |
{ |
| 187 |
return Unix{static_cast<int64>(std::time(nullptr))}; |
| 188 |
} |
| 189 |
|
| 190 |
#endif |
| 191 |
|
| 192 |
inline int64 UnixAsSeconds(Unix tp) |
| 193 |
{ |
| 194 |
return tp.value; |
| 195 |
} |
| 196 |
|
| 197 |
inline Unix UnixFromSeconds(int64 seconds) |
| 198 |
{ |
| 199 |
return Unix{seconds}; |
| 200 |
} |
| 201 |
|
| 202 |
Unix UnixFromUTC(UTC timeUtc); |
| 203 |
|
| 204 |
UTC UnixAsUTC(Unix tp); |
| 205 |
|
| 206 |
#if defined(MODPLUG_TRACKER) |
| 207 |
|
| 208 |
Unix UnixFromLocal(Local timeLocal); |
| 209 |
|
| 210 |
Local UnixAsLocal(Unix tp); |
| 211 |
|
| 212 |
#endif // MODPLUG_TRACKER |
| 213 |
|
| 214 |
} // namespace nochrono |
| 215 |
|
| 216 |
#if MPT_CXX_AT_LEAST(20) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO) && !defined(MPT_LIBCXX_QUIRK_NO_CHRONO_DATE) |
| 217 |
|
| 218 |
using Unix = std::chrono::system_clock::time_point; |
| 219 |
|
| 220 |
inline Unix UnixNow() |
| 221 |
{ |
| 222 |
return std::chrono::system_clock::now(); |
| 223 |
} |
| 224 |
|
| 225 |
inline int64 UnixAsSeconds(Unix tp) |
| 226 |
{ |
| 227 |
return std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count(); |
| 228 |
} |
| 229 |
|
| 230 |
inline Unix UnixFromSeconds(int64 seconds) |
| 231 |
{ |
| 232 |
return std::chrono::system_clock::time_point{std::chrono::seconds{seconds}}; |
| 233 |
} |
| 234 |
|
| 235 |
inline mpt::Date::Unix UnixFromUTC(UTC utc) |
| 236 |
{ |
| 237 |
try |
| 238 |
{ |
| 239 |
return std::chrono::system_clock::time_point{ |
| 240 |
std::chrono::sys_days { |
| 241 |
std::chrono::year{ utc.year } / |
| 242 |
std::chrono::month{ utc.month } / |
| 243 |
std::chrono::day{ utc.day } |
| 244 |
} + |
| 245 |
std::chrono::hours{ utc.hours } + |
| 246 |
std::chrono::minutes{ utc.minutes } + |
| 247 |
std::chrono::seconds{ utc.seconds }}; |
| 248 |
} catch(const std::exception &) |
| 249 |
{ |
| 250 |
return mpt::Date::UnixFromSeconds(mpt::Date::nochrono::UnixAsSeconds(mpt::Date::nochrono::UnixFromUTC(utc))); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
inline mpt::Date::UTC UnixAsUTC(Unix tp) |
| 255 |
{ |
| 256 |
try |
| 257 |
{ |
| 258 |
std::chrono::sys_days dp = std::chrono::floor<std::chrono::days>(tp); |
| 259 |
std::chrono::year_month_day ymd{dp}; |
| 260 |
std::chrono::hh_mm_ss hms{tp - dp}; |
| 261 |
mpt::Date::UTC result; |
| 262 |
result.year = static_cast<int>(ymd.year()); |
| 263 |
result.month = static_cast<unsigned int>(ymd.month()); |
| 264 |
result.day = static_cast<unsigned int>(ymd.day()); |
| 265 |
result.hours = static_cast<int32>(hms.hours().count()); |
| 266 |
result.minutes = static_cast<int32>(hms.minutes().count()); |
| 267 |
result.seconds = static_cast<int64>(hms.seconds().count()); |
| 268 |
return result; |
| 269 |
} catch(const std::exception &) |
| 270 |
{ |
| 271 |
return mpt::Date::nochrono::UnixAsUTC(mpt::Date::nochrono::UnixFromSeconds(mpt::Date::UnixAsSeconds(tp))); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#if defined(MODPLUG_TRACKER) |
| 276 |
|
| 277 |
inline mpt::Date::Unix UnixFromLocal(Local local) |
| 278 |
{ |
| 279 |
#if !defined(MPT_LIBCXX_QUIRK_CHRONO_DATE_NO_ZONED_TIME) |
| 280 |
try |
| 281 |
{ |
| 282 |
#if defined(MODPLUG_TRACKER) |
| 283 |
if(mpt::OS::Windows::IsWine()) |
| 284 |
{ |
| 285 |
// <https://bugs.openmpt.org/view.php?id=1933> |
| 286 |
throw std::runtime_error("zoned_time is blacklisted on Wine"); |
| 287 |
} |
| 288 |
#endif |
| 289 |
std::chrono::time_point<std::chrono::local_t, std::chrono::seconds> local_tp = |
| 290 |
std::chrono::local_days { |
| 291 |
std::chrono::year{ local.year } / |
| 292 |
std::chrono::month{ local.month } / |
| 293 |
std::chrono::day{ local.day } |
| 294 |
} + |
| 295 |
std::chrono::hours{ local.hours } + |
| 296 |
std::chrono::minutes{ local.minutes } + |
| 297 |
std::chrono::seconds{ local.seconds }; |
| 298 |
#if defined(MPT_LIBCXX_QUIRK_CHRONO_DATE_BROKEN_ZONED_TIME) |
| 299 |
return std::chrono::zoned_time{std::chrono::current_zone(), std::chrono::current_zone()->to_sys(local_tp)}.get_sys_time(); |
| 300 |
#else |
| 301 |
return std::chrono::zoned_time{std::chrono::current_zone(), local_tp}.get_sys_time(); |
| 302 |
#endif |
| 303 |
} catch(const std::exception &) |
| 304 |
#endif |
| 305 |
{ |
| 306 |
return mpt::Date::UnixFromSeconds(mpt::Date::nochrono::UnixAsSeconds(mpt::Date::nochrono::UnixFromLocal(local))); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
inline mpt::Date::Local UnixAsLocal(Unix tp) |
| 311 |
{ |
| 312 |
#if !defined(MPT_LIBCXX_QUIRK_CHRONO_DATE_NO_ZONED_TIME) |
| 313 |
try |
| 314 |
{ |
| 315 |
#if defined(MODPLUG_TRACKER) |
| 316 |
if(mpt::OS::Windows::IsWine()) |
| 317 |
{ |
| 318 |
// <https://bugs.openmpt.org/view.php?id=1933> |
| 319 |
throw std::runtime_error("zoned_time is blacklisted on Wine"); |
| 320 |
} |
| 321 |
#endif |
| 322 |
std::chrono::zoned_time local_tp{ std::chrono::current_zone(), tp }; |
| 323 |
std::chrono::local_days dp = std::chrono::floor<std::chrono::days>(local_tp.get_local_time()); |
| 324 |
std::chrono::year_month_day ymd{dp}; |
| 325 |
std::chrono::hh_mm_ss hms{local_tp.get_local_time() - dp}; |
| 326 |
mpt::Date::Local result; |
| 327 |
result.year = static_cast<int>(ymd.year()); |
| 328 |
result.month = static_cast<unsigned int>(ymd.month()); |
| 329 |
result.day = static_cast<unsigned int>(ymd.day()); |
| 330 |
result.hours = static_cast<int32>(hms.hours().count()); |
| 331 |
result.minutes = static_cast<int32>(hms.minutes().count()); |
| 332 |
result.seconds = static_cast<int64>(hms.seconds().count()); |
| 333 |
return result; |
| 334 |
} catch(const std::exception &) |
| 335 |
#endif |
| 336 |
{ |
| 337 |
return mpt::Date::nochrono::UnixAsLocal(mpt::Date::nochrono::UnixFromSeconds(mpt::Date::UnixAsSeconds(tp))); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
#endif // MODPLUG_TRACKER |
| 342 |
|
| 343 |
#else |
| 344 |
|
| 345 |
using Unix = nochrono::Unix; |
| 346 |
|
| 347 |
using nochrono::UnixNow; |
| 348 |
using nochrono::UnixAsSeconds; |
| 349 |
using nochrono::UnixFromSeconds; |
| 350 |
|
| 351 |
using nochrono::UnixFromUTC; |
| 352 |
using nochrono::UnixAsUTC; |
| 353 |
|
| 354 |
#if defined(MODPLUG_TRACKER) |
| 355 |
|
| 356 |
using nochrono::UnixFromLocal; |
| 357 |
using nochrono::UnixAsLocal; |
| 358 |
|
| 359 |
#endif // MODPLUG_TRACKER |
| 360 |
|
| 361 |
#endif |
| 362 |
|
| 363 |
mpt::ustring ToShortenedISO8601(AnyGregorian date); // i.e. 2015-01-15T18:32:01 |
| 364 |
|
| 365 |
mpt::ustring ToShortenedISO8601(UTC date); // i.e. 2015-01-15T18:32:01Z |
| 366 |
|
| 367 |
#ifdef MODPLUG_TRACKER |
| 368 |
mpt::ustring ToShortenedISO8601(Local date); // i.e. 2015-01-15T18:32:01 |
| 369 |
#endif // MODPLUG_TRACKER |
| 370 |
|
| 371 |
} // namespace Date |
| 372 |
} // namespace mpt |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
OPENMPT_NAMESPACE_END |