From 6b0bf1ad387bc9b32cb76ff4519da09d82cd085d Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Wed, 20 Oct 2021 14:43:47 +0200 Subject: DateTime::Now() for POSIX platforms --- zencore/compactbinary.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'zencore/compactbinary.cpp') diff --git a/zencore/compactbinary.cpp b/zencore/compactbinary.cpp index ab0904b2b..041ad8861 100644 --- a/zencore/compactbinary.cpp +++ b/zencore/compactbinary.cpp @@ -18,6 +18,8 @@ #if ZEN_PLATFORM_WINDOWS # include +#else +# include #endif #if ZEN_WITH_TESTS @@ -46,15 +48,31 @@ IsLeapYear(int Year) return false; } +static constexpr uint64_t +GetPlatformToDateTimeBiasInSeconds() +{ +#if ZEN_PLATFORM_WINDOWS + const uint64_t PlatformEpochYear = 1601; +#else + const uint64_t PlatformEpochYear = 1970; +#endif + const uint64_t DateTimeEpochYear = 1; + return uint64_t(double(PlatformEpochYear - DateTimeEpochYear) * 365.2425) * 86400; +} + DateTime DateTime::Now() { + static const uint64_t EpochBias = GetPlatformToDateTimeBiasInSeconds(); + static const uint64_t SecsTo100nsTicks = int64_t(10e9 / 100); + #if ZEN_PLATFORM_WINDOWS FILETIME SysTime; GetSystemTimeAsFileTime(&SysTime); - return DateTime{504911232000000000ull + (uint64_t(SysTime.dwHighDateTime) << 32) | SysTime.dwLowDateTime}; + return DateTime{(EpochBias * SecsTo100nsTicks) + (uint64_t(SysTime.dwHighDateTime) << 32) | SysTime.dwLowDateTime}; #else -# error Needs implementation + int64_t SecondsSinceUnixEpoch = time(nullptr); + return DateTime{(EpochBias + SecondsSinceUnixEpoch) * SecsTo100nsTicks}; #endif } -- cgit v1.2.3