tools/source/datetime/systemdatetime.cxx | 44 ++++++++++--------------- tools/source/datetime/ttime.cxx | 53 ++++++++----------------------- 2 files changed, 32 insertions(+), 65 deletions(-)
New commits: commit bea046c3e486edb87ba9ce348d77d55d9a966d4b Author: Michael Meeks <[email protected]> AuthorDate: Fri Feb 20 01:45:12 2026 +0000 Commit: Noel Grandin <[email protected]> CommitDate: Sat Feb 21 11:38:46 2026 +0100 tools: replace localtime_r/localtime_s with sal osl_time API Kill un-needed uses of localtime by replacing posix timezone calls (localtime_r, mktime, gmtime_r, timezone, altzone, tm_gmtoff) and Windows GetTimeZoneInformation in systemdatetime.cxx and ttime.cxx with our own internal sal abstraction. Change-Id: Ia4dbf7aff40b392ada196066be53ffcb6c74f2a3 Signed-off-by: Michael Meeks <[email protected]> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/199827 Reviewed-by: Noel Grandin <[email protected]> Tested-by: Jenkins CollaboraOffice <[email protected]> diff --git a/tools/source/datetime/systemdatetime.cxx b/tools/source/datetime/systemdatetime.cxx index 5bc785b42cfb..60d8f3707ba9 100644 --- a/tools/source/datetime/systemdatetime.cxx +++ b/tools/source/datetime/systemdatetime.cxx @@ -19,8 +19,7 @@ #include <sal/config.h> -#include <chrono> -#include <time.h> +#include <osl/time.h> #include <systemdatetime.hxx> @@ -40,31 +39,24 @@ 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) - bool ok = localtime_s(&aTime, &nTmpTime) == 0; -#else - bool ok = localtime_r(&nTmpTime, &aTime) != nullptr; -#endif - 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) - { - 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; - } + TimeValue utcTv; + if (!osl_getSystemTime(&utcTv)) + return false; - return false; + TimeValue localTv; + if (!osl_getLocalTimeFromSystemTime(&utcTv, &localTv)) + return false; + + oslDateTime dt; + if (!osl_getDateTimeFromTimeValue(&localTv, &dt)) + return false; + + if (pDate) + *pDate = ConvertYMDToInt(dt.Year, dt.Month, dt.Day); + if (pTime) + *pTime = ConvertHMSnToInt(dt.Hours, dt.Minutes, dt.Seconds, dt.NanoSeconds); + + return true; } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/tools/source/datetime/ttime.cxx b/tools/source/datetime/ttime.cxx index 4d0d27fea91b..f9c3d095eb4d 100644 --- a/tools/source/datetime/ttime.cxx +++ b/tools/source/datetime/ttime.cxx @@ -39,16 +39,13 @@ #include <mach/mach_time.h> #endif +#include <osl/time.h> #include <rtl/math.hxx> #include <tools/time.hxx> #include <com/sun/star/util/DateTime.hpp> #include <systemdatetime.hxx> -#if defined(__sun) && defined(__GNUC__) -extern long altzone; -#endif - namespace tools { Time::Time( TimeInitSystem ) @@ -271,26 +268,9 @@ bool tools::Time::IsEqualIgnoreNanoSec( const tools::Time& rTime ) const Time tools::Time::GetUTCOffset() { -#if defined(_WIN32) - TIME_ZONE_INFORMATION aTimeZone; - aTimeZone.Bias = 0; - DWORD nTimeZoneRet = GetTimeZoneInformation( &aTimeZone ); - sal_Int32 nTempTime = aTimeZone.Bias; - if ( nTimeZoneRet == TIME_ZONE_ID_STANDARD ) - nTempTime += aTimeZone.StandardBias; - else if ( nTimeZoneRet == TIME_ZONE_ID_DAYLIGHT ) - nTempTime += aTimeZone.DaylightBias; - tools::Time aTime( 0, static_cast<sal_uInt16>(abs( nTempTime )) ); - if ( nTempTime > 0 ) - aTime = -aTime; - return aTime; -#else static sal_uInt64 nCacheTicks = 0; static sal_Int32 nCacheSecOffset = -1; sal_uInt64 nTicks = tools::Time::GetSystemTicks(); - time_t nTime; - tm aTM; - short nTempTime; // determine value again if needed if ( (nCacheSecOffset == -1) || @@ -298,30 +278,25 @@ Time tools::Time::GetUTCOffset() ( nTicks < nCacheTicks ) // handle overflow ) { - nTime = time( nullptr ); - localtime_r( &nTime, &aTM ); - auto nLocalTime = mktime( &aTM ); -#if defined(__sun) - // Solaris gmtime_r() seems not to handle daylight saving time - // flags correctly - auto nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone ); -#elif defined( LINUX ) - // Linux mktime() seems not to handle tm_isdst correctly - auto nUTC = nLocalTime - aTM.tm_gmtoff; -#else - gmtime_r( &nTime, &aTM ); - auto nUTC = mktime( &aTM ); -#endif - nCacheTicks = nTicks; - nCacheSecOffset = (nLocalTime-nUTC) / 60; + TimeValue utcTv; + TimeValue localTv; + if (osl_getSystemTime(&utcTv) && osl_getLocalTimeFromSystemTime(&utcTv, &localTv)) + { + nCacheTicks = nTicks; + nCacheSecOffset = (static_cast<sal_Int64>(localTv.Seconds) + - static_cast<sal_Int64>(utcTv.Seconds)) / 60; + } + else + { + nCacheSecOffset = 0; + } } - nTempTime = abs( nCacheSecOffset ); + sal_Int32 nTempTime = abs( nCacheSecOffset ); tools::Time aTime( 0, static_cast<sal_uInt16>(nTempTime) ); if ( nCacheSecOffset < 0 ) aTime = -aTime; return aTime; -#endif } sal_uInt64 tools::Time::GetSystemTicks()
