diff options
author | Takeshi Abe <tabe@fixedpoint.jp> | 2018-04-29 05:20:47 +0900 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2018-05-04 21:30:43 +0200 |
commit | 5103c6a405e93bb05018ab7c89e7e9446c740aaa (patch) | |
tree | 98b3b1705cfcd0eeb867cff94e8766f1978c287f /tools/source/datetime/ttime.cxx | |
parent | 7b0ff01d1bf718c5b439b5cddfcdfe051271b28c (diff) |
tools: Avoid looking up system clock twice to get DateTime
DateTime::DateTime(DateTimeInitSystem) had initialized Date and
Time separately, which causes a slight possibility that it could
get a wrong datetime with almost 24 hours delay when it went beyond
midnight. E.g., the date part was of the previous day while the
time part was 00:00:00.xxx of the next day.
This also reduces duplicate code by sharing GetSystemDateTime().
Change-Id: I352d90f468f5cbc70e7936a337bed97365baa06c
Reviewed-on: https://gerrit.libreoffice.org/53612
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Eike Rathke <erack@redhat.com>
Diffstat (limited to 'tools/source/datetime/ttime.cxx')
-rw-r--r-- | tools/source/datetime/ttime.cxx | 85 |
1 files changed, 18 insertions, 67 deletions
diff --git a/tools/source/datetime/ttime.cxx b/tools/source/datetime/ttime.cxx index 9d12bb9208e7..231a096e09dc 100644 --- a/tools/source/datetime/ttime.cxx +++ b/tools/source/datetime/ttime.cxx @@ -19,8 +19,6 @@ #include <sal/config.h> -#include <cerrno> - #if defined(_WIN32) #if !defined WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN @@ -29,7 +27,6 @@ #include <mmsystem.h> #elif defined UNX #include <unistd.h> -#include <limits.h> #include <math.h> #include <sys/time.h> #endif @@ -45,16 +42,14 @@ #include <tools/time.hxx> #include <osl/diagnose.h> +#include <systemdatetime.hxx> + #if defined(__sun) && defined(__GNUC__) extern long altzone; #endif namespace { - const sal_Int64 secMask = SAL_CONST_INT64(1000000000); - const sal_Int64 minMask = SAL_CONST_INT64(100000000000); - const sal_Int64 hourMask = SAL_CONST_INT64(10000000000000); - const sal_Int64 nanoSecInSec = 1000000000; const sal_Int16 secInMin = 60; const sal_Int16 minInHour = 60; @@ -97,52 +92,8 @@ namespace tools { Time::Time( TimeInitSystem ) { -#if defined(_WIN32) - SYSTEMTIME aDateTime; - GetLocalTime( &aDateTime ); - - // construct time - nTime = aDateTime.wHour * hourMask + - aDateTime.wMinute * minMask + - aDateTime.wSecond * secMask + - aDateTime.wMilliseconds * 1000000; -#else - // determine time - struct timespec tsTime; -#if defined( __MACH__ ) - // OS X 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; -#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; - } -#endif // __MACH__ - - // construct time - struct tm aTime; - time_t nTmpTime = tsTime.tv_sec; - if ( localtime_r( &nTmpTime, &aTime ) ) - { - nTime = aTime.tm_hour * hourMask + - aTime.tm_min * minMask + - aTime.tm_sec * secMask + - tsTime.tv_nsec; - } - else + if ( !GetSystemDateTime( nullptr, &nTime ) ) nTime = 0; -#endif // WNT } Time::Time( const tools::Time& rTime ) @@ -175,9 +126,9 @@ void tools::Time::init( sal_uInt32 nHour, sal_uInt32 nMin, sal_uInt32 nSec, sal_ // construct time nTime = nNanoSec + - nSec * secMask + - nMin * minMask + - nHour * hourMask; + nSec * SEC_MASK + + nMin * MIN_MASK + + nHour * HOUR_MASK; } void tools::Time::SetHour( sal_uInt16 nNewHour ) @@ -189,9 +140,9 @@ void tools::Time::SetHour( sal_uInt16 nNewHour ) nTime = nSign * ( nNanoSec + - nSec * secMask + - nMin * minMask + - nNewHour * hourMask ); + nSec * SEC_MASK + + nMin * MIN_MASK + + nNewHour * HOUR_MASK ); } void tools::Time::SetMin( sal_uInt16 nNewMin ) @@ -206,9 +157,9 @@ void tools::Time::SetMin( sal_uInt16 nNewMin ) nTime = nSign * ( nNanoSec + - nSec * secMask + - nNewMin * minMask + - nHour * hourMask ); + nSec * SEC_MASK + + nNewMin * MIN_MASK + + nHour * HOUR_MASK ); } void tools::Time::SetSec( sal_uInt16 nNewSec ) @@ -223,9 +174,9 @@ void tools::Time::SetSec( sal_uInt16 nNewSec ) nTime = nSign * ( nNanoSec + - nNewSec * secMask + - nMin * minMask + - nHour * hourMask ); + nNewSec * SEC_MASK + + nMin * MIN_MASK + + nHour * HOUR_MASK ); } void tools::Time::SetNanoSec( sal_uInt32 nNewNanoSec ) @@ -240,9 +191,9 @@ void tools::Time::SetNanoSec( sal_uInt32 nNewNanoSec ) nTime = nSign * ( nNewNanoSec + - nSec * secMask + - nMin * minMask + - nHour * hourMask ); + nSec * SEC_MASK + + nMin * MIN_MASK + + nHour * HOUR_MASK ); } sal_Int64 tools::Time::GetNSFromTime() const |