tools/source/datetime/systemdatetime.cxx | 67 ++++++------------------------- 1 file changed, 15 insertions(+), 52 deletions(-)
New commits: commit 53f889780f6182de875675ac8e6f2e74131a04e6 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Tue Oct 8 15:39:36 2024 +0500 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Tue Oct 8 17:10:32 2024 +0200 Reimplement GetSystemDateTime using std::chrono Change-Id: I15bce244f96a8940df73f4b5933bcb95a547673e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174691 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/tools/source/datetime/systemdatetime.cxx b/tools/source/datetime/systemdatetime.cxx index 2115f4b6205a..5bc785b42cfb 100644 --- a/tools/source/datetime/systemdatetime.cxx +++ b/tools/source/datetime/systemdatetime.cxx @@ -16,23 +16,12 @@ * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ -#if defined(_WIN32) -#if !defined WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include <windows.h> -#elif defined UNX -#include <sys/time.h> -#endif +#include <sal/config.h> + +#include <chrono> #include <time.h> -#ifdef __MACH__ -#include <mach/clock.h> -#include <mach/mach.h> -#include <mach/mach_time.h> -#endif -#include <osl/diagnose.h> #include <systemdatetime.hxx> namespace @@ -51,57 +40,31 @@ constexpr sal_Int64 ConvertHMSnToInt(sal_Int64 nHour, sal_Int64 nMin, sal_Int64 bool GetSystemDateTime(sal_Int32* pDate, sal_Int64* pTime) { + auto tp = std::chrono::system_clock::now(); + const time_t nTmpTime = std::chrono::system_clock::to_time_t(tp); + struct tm aTime; #if defined(_WIN32) - SYSTEMTIME aDateTime; - GetLocalTime(&aDateTime); - - if (pDate) - *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aDateTime.wYear), - static_cast<sal_Int32>(aDateTime.wMonth), - static_cast<sal_Int32>(aDateTime.wDay)); - if (pTime) - *pTime = ConvertHMSnToInt(aDateTime.wHour, aDateTime.wMinute, aDateTime.wSecond, - aDateTime.wMilliseconds * 1000000); - - return true; -#else - struct timespec tsTime; -#if defined(__MACH__) - // macOS does not have clock_gettime, use clock_get_time - clock_serv_t cclock; - mach_timespec_t mts; - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - tsTime.tv_sec = mts.tv_sec; - tsTime.tv_nsec = mts.tv_nsec; + bool ok = localtime_s(&aTime, &nTmpTime) == 0; #else - // CLOCK_REALTIME should be supported - // on any modern Unix, but be extra cautious - if (clock_gettime(CLOCK_REALTIME, &tsTime) != 0) - { - struct timeval tvTime; - OSL_VERIFY(gettimeofday(&tvTime, nullptr) != 0); - tsTime.tv_sec = tvTime.tv_sec; - tsTime.tv_nsec = tvTime.tv_usec * 1000; - } + bool ok = localtime_r(&nTmpTime, &aTime) != nullptr; #endif - - struct tm aTime; - time_t nTmpTime = tsTime.tv_sec; - if (localtime_r(&nTmpTime, &aTime)) + if (ok) { if (pDate) *pDate = ConvertYMDToInt(static_cast<sal_Int32>(aTime.tm_year + 1900), static_cast<sal_Int32>(aTime.tm_mon + 1), static_cast<sal_Int32>(aTime.tm_mday)); if (pTime) - *pTime = ConvertHMSnToInt(aTime.tm_hour, aTime.tm_min, aTime.tm_sec, tsTime.tv_nsec); + { + auto hms = std::chrono::hh_mm_ss(std::chrono::floor<std::chrono::nanoseconds>( + tp - std::chrono::floor<std::chrono::days>(tp))); + auto ns = hms.subseconds().count(); + *pTime = ConvertHMSnToInt(aTime.tm_hour, aTime.tm_min, aTime.tm_sec, ns); + } return true; } return false; -#endif } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */