LCOV - code coverage report
Current view: top level - tools/source/datetime - ttime.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 158 190 83.2 %
Date: 2015-06-13 12:38:46 Functions: 22 25 88.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <sal/config.h>
      21             : 
      22             : #include <cerrno>
      23             : 
      24             : #if defined( WNT )
      25             : #include <windows.h>
      26             : #elif defined UNX
      27             : #include <unistd.h>
      28             : #include <limits.h>
      29             : #include <math.h>
      30             : #include <sys/time.h>
      31             : #endif
      32             : 
      33             : #include <time.h>
      34             : #ifdef __MACH__
      35             : #include <mach/clock.h>
      36             : #include <mach/mach.h>
      37             : #endif
      38             : 
      39             : #include <sal/log.hxx>
      40             : #include <tools/time.hxx>
      41             : #include <osl/diagnose.h>
      42             : 
      43             : #if defined(SOLARIS) && defined(__GNUC__)
      44             : extern long altzone;
      45             : #endif
      46             : 
      47             : namespace {
      48             : 
      49             :     const sal_Int64 secMask  = SAL_CONST_INT64(1000000000);
      50             :     const sal_Int64 minMask  = SAL_CONST_INT64(100000000000);
      51             :     const sal_Int64 hourMask = SAL_CONST_INT64(10000000000000);
      52             : 
      53             :     const sal_Int64 nanoSecInSec = 1000000000;
      54             :     const sal_Int16 secInMin     = 60;
      55             :     const sal_Int16 minInHour    = 60;
      56             : 
      57        1392 :     sal_Int64 TimeToNanoSec( const tools::Time& rTime )
      58             :     {
      59        1392 :         short  nSign   = (rTime.GetTime() >= 0) ? +1 : -1;
      60        1392 :         sal_Int32   nHour    = rTime.GetHour();
      61        1392 :         sal_Int32   nMin     = rTime.GetMin();
      62        1392 :         sal_Int32   nSec     = rTime.GetSec();
      63        1392 :         sal_Int32   nNanoSec = rTime.GetNanoSec();
      64             : 
      65        1392 :         sal_Int64 nRet = nNanoSec;
      66        1392 :         nRet     +=            nSec * nanoSecInSec;
      67        1392 :         nRet     +=       nMin * secInMin * nanoSecInSec;
      68        1392 :         nRet     += nHour * minInHour * secInMin * nanoSecInSec;
      69             : 
      70        1392 :         return (nRet * nSign);
      71             :     }
      72             : 
      73         696 :     tools::Time NanoSecToTime( sal_Int64 nNanoSec )
      74             :     {
      75             :         short nSign;
      76         696 :         if ( nNanoSec < 0 )
      77             :         {
      78          12 :             nNanoSec *= -1;
      79          12 :             nSign = -1;
      80             :         }
      81             :         else
      82         684 :             nSign = 1;
      83             : 
      84         696 :         tools::Time aTime( 0, 0, 0, nNanoSec );
      85         696 :         aTime.SetTime( aTime.GetTime() * nSign );
      86         696 :         return aTime;
      87             :     }
      88             : 
      89             : } // anonymous namespace
      90             : 
      91             : namespace tools {
      92             : 
      93       21546 : Time::Time( TimeInitSystem )
      94             : {
      95             : #if defined( WNT )
      96             :     SYSTEMTIME aDateTime;
      97             :     GetLocalTime( &aDateTime );
      98             : 
      99             :     // construct time
     100             :     nTime = aDateTime.wHour         * hourMask +
     101             :             aDateTime.wMinute       * minMask +
     102             :             aDateTime.wSecond       * secMask +
     103             :             aDateTime.wMilliseconds * 1000000;
     104             : #else
     105             :     // determine time
     106             :     struct timespec tsTime;
     107             : #if defined( __MACH__ )
     108             :     // OS X does not have clock_gettime, use clock_get_time
     109             :     clock_serv_t cclock;
     110             :     mach_timespec_t mts;
     111             :     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
     112             :     clock_get_time(cclock, &mts);
     113             :     mach_port_deallocate(mach_task_self(), cclock);
     114             :     tsTime.tv_sec  = mts.tv_sec;
     115             :     tsTime.tv_nsec = mts.tv_nsec;
     116             : #else
     117             :     // CLOCK_REALTIME should be supported
     118             :     // on any modern Unix, but be extra cautious
     119       21546 :     if (clock_gettime(CLOCK_REALTIME, &tsTime) != 0)
     120             :     {
     121             :         struct timeval tvTime;
     122           0 :         OSL_VERIFY( gettimeofday(&tvTime, NULL) != 0 );
     123           0 :         tsTime.tv_sec  = tvTime.tv_sec;
     124           0 :         tsTime.tv_nsec = tvTime.tv_usec * 1000;
     125             :     }
     126             : #endif // __MACH__
     127             : 
     128             :     // construct time
     129             :     struct tm aTime;
     130       21546 :     time_t nTmpTime = tsTime.tv_sec;
     131       21546 :     if ( localtime_r( &nTmpTime, &aTime ) )
     132             :     {
     133       43092 :         nTime = aTime.tm_hour * hourMask +
     134       43092 :                 aTime.tm_min  * minMask +
     135       43092 :                 aTime.tm_sec  * secMask +
     136       21546 :                 tsTime.tv_nsec;
     137             :     }
     138             :     else
     139           0 :         nTime = 0;
     140             : #endif // WNT
     141       21546 : }
     142             : 
     143       19228 : Time::Time( const tools::Time& rTime )
     144             : {
     145       19228 :     nTime = rTime.nTime;
     146       19228 : }
     147             : 
     148        6088 : Time::Time( sal_uInt32 nHour, sal_uInt32 nMin, sal_uInt32 nSec, sal_uInt64 nNanoSec )
     149             : {
     150        6088 :     init(nHour, nMin, nSec, nNanoSec);
     151        6088 : }
     152         110 : Time::Time( const ::com::sun::star::util::Time &_rTime )
     153             : {
     154         110 :     init(_rTime.Hours, _rTime.Minutes, _rTime.Seconds, _rTime.NanoSeconds);
     155         110 : }
     156           8 : Time::Time( const ::com::sun::star::util::DateTime &_rDateTime )
     157             : {
     158           8 :     init(_rDateTime.Hours, _rDateTime.Minutes, _rDateTime.Seconds, _rDateTime.NanoSeconds);
     159           8 : }
     160             : 
     161        6206 : void tools::Time::init( sal_uInt32 nHour, sal_uInt32 nMin, sal_uInt32 nSec, sal_uInt64 nNanoSec )
     162             : {
     163             :     // normalize time
     164        6206 :     nSec     += nNanoSec / nanoSecInSec;
     165        6206 :     nNanoSec %= nanoSecInSec;
     166        6206 :     nMin     += nSec / secInMin;
     167        6206 :     nSec     %= secInMin;
     168        6206 :     nHour    += nMin / minInHour;
     169        6206 :     nMin     %= minInHour;
     170             : 
     171             :     // construct time
     172             :     nTime = nNanoSec +
     173       12412 :             nSec  * secMask +
     174       12412 :             nMin  * minMask +
     175       12412 :             nHour * hourMask;
     176        6206 : }
     177             : 
     178         732 : void tools::Time::SetHour( sal_uInt16 nNewHour )
     179             : {
     180         732 :     short  nSign          = (nTime >= 0) ? +1 : -1;
     181         732 :     sal_Int32   nMin      = GetMin();
     182         732 :     sal_Int32   nSec      = GetSec();
     183         732 :     sal_Int32   nNanoSec  = GetNanoSec();
     184             : 
     185        1464 :     nTime = nSign *
     186        1464 :             ( nNanoSec +
     187        1464 :               nSec  * secMask +
     188        1464 :               nMin  * minMask +
     189        1464 :               nNewHour * hourMask );
     190         732 : }
     191             : 
     192         127 : void tools::Time::SetMin( sal_uInt16 nNewMin )
     193             : {
     194         127 :     short  nSign          = (nTime >= 0) ? +1 : -1;
     195         127 :     sal_Int32   nHour     = GetHour();
     196         127 :     sal_Int32   nSec      = GetSec();
     197         127 :     sal_Int32   nNanoSec  = GetNanoSec();
     198             : 
     199             :     // no overflow
     200         127 :     nNewMin = nNewMin % minInHour;
     201             : 
     202         254 :     nTime = nSign *
     203         254 :             ( nNanoSec +
     204         254 :               nSec  * secMask +
     205         254 :               nNewMin  * minMask +
     206         254 :               nHour * hourMask );
     207         127 : }
     208             : 
     209        2718 : void tools::Time::SetSec( sal_uInt16 nNewSec )
     210             : {
     211        2718 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     212        2718 :     sal_Int32   nHour     = GetHour();
     213        2718 :     sal_Int32   nMin      = GetMin();
     214        2718 :     sal_Int32   nNanoSec   = GetNanoSec();
     215             : 
     216             :     // no overflow
     217        2718 :     nNewSec = nNewSec % secInMin;
     218             : 
     219        5436 :     nTime = nSign *
     220        5436 :             ( nNanoSec +
     221        5436 :               nNewSec  * secMask +
     222        5436 :               nMin  * minMask +
     223        5436 :               nHour * hourMask );
     224        2718 : }
     225             : 
     226        2645 : void tools::Time::SetNanoSec( sal_uInt32 nNewNanoSec )
     227             : {
     228        2645 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     229        2645 :     sal_Int32   nHour     = GetHour();
     230        2645 :     sal_Int32   nMin      = GetMin();
     231        2645 :     sal_Int32   nSec      = GetSec();
     232             : 
     233             :     // no overflow
     234        2645 :     nNewNanoSec = nNewNanoSec % nanoSecInSec;
     235             : 
     236        5290 :     nTime = nSign *
     237        5290 :             ( nNewNanoSec +
     238        5290 :               nSec  * secMask +
     239        5290 :               nMin  * minMask +
     240        5290 :               nHour * hourMask );
     241        2645 : }
     242             : 
     243        7040 : sal_Int64 tools::Time::GetNSFromTime() const
     244             : {
     245        7040 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     246        7040 :     sal_Int32   nHour     = GetHour();
     247        7040 :     sal_Int32   nMin      = GetMin();
     248        7040 :     sal_Int32   nSec      = GetSec();
     249        7040 :     sal_Int32   nNanoSec  = GetNanoSec();
     250             : 
     251        7040 :     return nSign *
     252       14080 :            ( nNanoSec +
     253       14080 :              nSec  * nanoSecInSec +
     254       14080 :              nMin  * (secInMin * nanoSecInSec) +
     255       14080 :              nHour * (minInHour * secInMin * nanoSecInSec) );
     256             : }
     257             : 
     258           3 : void tools::Time::MakeTimeFromNS( sal_Int64 nNS )
     259             : {
     260             :     short nSign;
     261           3 :     if ( nNS < 0 )
     262             :     {
     263           0 :         nNS *= -1;
     264           0 :         nSign = -1;
     265             :     }
     266             :     else
     267           3 :         nSign = 1;
     268             : 
     269             :     // avoid overflow when sal_uIntPtr is 32 bits
     270           3 :     tools::Time aTime( 0, 0, nNS/nanoSecInSec, nNS % nanoSecInSec );
     271           3 :     SetTime( aTime.GetTime() * nSign );
     272           3 : }
     273             : 
     274           0 : sal_Int32 tools::Time::GetMSFromTime() const
     275             : {
     276           0 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     277           0 :     sal_Int32   nHour     = GetHour();
     278           0 :     sal_Int32   nMin      = GetMin();
     279           0 :     sal_Int32   nSec      = GetSec();
     280           0 :     sal_Int32   nNanoSec  = GetNanoSec();
     281             : 
     282           0 :     return nSign *
     283           0 :            ( nNanoSec/1000000 +
     284           0 :              nSec  * 1000 +
     285           0 :              nMin  * 60000 +
     286           0 :              nHour * 360000 );
     287             : }
     288             : 
     289           2 : void tools::Time::MakeTimeFromMS( sal_Int32 nMS )
     290             : {
     291             :     short nSign;
     292           2 :     if ( nMS < 0 )
     293             :     {
     294           0 :         nMS *= -1;
     295           0 :         nSign = -1;
     296             :     }
     297             :     else
     298           2 :         nSign = 1;
     299             : 
     300             :     // avoid overflow when sal_uIntPtr is 32 bits
     301           2 :     tools::Time aTime( 0, 0, nMS/1000, (nMS % 1000) * 1000000 );
     302           2 :     SetTime( aTime.GetTime() * nSign );
     303           2 : }
     304             : 
     305           0 : double tools::Time::GetTimeInDays() const
     306             : {
     307           0 :     short  nSign      = (nTime >= 0) ? +1 : -1;
     308           0 :     double nHour      = GetHour();
     309           0 :     double nMin       = GetMin();
     310           0 :     double nSec       = GetSec();
     311           0 :     double nNanoSec   = GetNanoSec();
     312             : 
     313           0 :     return (nHour + (nMin / 60) + (nSec / (minInHour * secInMin)) + (nNanoSec / (minInHour * secInMin * nanoSecInSec))) / 24 * nSign;
     314             : }
     315             : 
     316        7543 : Time& tools::Time::operator =( const tools::Time& rTime )
     317             : {
     318        7543 :     nTime = rTime.nTime;
     319        7543 :     return *this;
     320             : }
     321             : 
     322         457 : Time& tools::Time::operator +=( const tools::Time& rTime )
     323             : {
     324         457 :     nTime = NanoSecToTime( TimeToNanoSec( *this ) +
     325         457 :                            TimeToNanoSec( rTime ) ).GetTime();
     326         457 :     return *this;
     327             : }
     328             : 
     329         170 : Time& tools::Time::operator -=( const tools::Time& rTime )
     330             : {
     331         170 :     nTime = NanoSecToTime( TimeToNanoSec( *this ) -
     332         170 :                            TimeToNanoSec( rTime ) ).GetTime();
     333         170 :     return *this;
     334             : }
     335             : 
     336          12 : Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 )
     337             : {
     338          12 :     return NanoSecToTime( TimeToNanoSec( rTime1 ) +
     339          12 :                           TimeToNanoSec( rTime2 ) );
     340             : }
     341             : 
     342          57 : Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 )
     343             : {
     344          57 :     return NanoSecToTime( TimeToNanoSec( rTime1 ) -
     345          57 :                           TimeToNanoSec( rTime2 ) );
     346             : }
     347             : 
     348           0 : bool tools::Time::IsEqualIgnoreNanoSec( const tools::Time& rTime ) const
     349             : {
     350           0 :     sal_Int32 n1 = (nTime       < 0 ? -static_cast<sal_Int32>(GetNanoSec())       : GetNanoSec() );
     351           0 :     sal_Int32 n2 = (rTime.nTime < 0 ? -static_cast<sal_Int32>(rTime.GetNanoSec()) : rTime.GetNanoSec() );
     352           0 :     return (nTime - n1) == (rTime.nTime - n2);
     353             : }
     354             : 
     355         610 : Time tools::Time::GetUTCOffset()
     356             : {
     357             : #if defined( WNT )
     358             :     TIME_ZONE_INFORMATION   aTimeZone;
     359             :     aTimeZone.Bias = 0;
     360             :     DWORD nTimeZoneRet = GetTimeZoneInformation( &aTimeZone );
     361             :     sal_Int32 nTempTime = aTimeZone.Bias;
     362             :     if ( nTimeZoneRet == TIME_ZONE_ID_STANDARD )
     363             :         nTempTime += aTimeZone.StandardBias;
     364             :     else if ( nTimeZoneRet == TIME_ZONE_ID_DAYLIGHT )
     365             :         nTempTime += aTimeZone.DaylightBias;
     366             :     tools::Time aTime( 0, (sal_uInt16)abs( nTempTime ) );
     367             :     if ( nTempTime > 0 )
     368             :         aTime = -aTime;
     369             :     return aTime;
     370             : #else
     371             :     static sal_uInt64   nCacheTicks = 0;
     372             :     static sal_Int32    nCacheSecOffset = -1;
     373         610 :     sal_uInt64          nTicks = tools::Time::GetSystemTicks();
     374             :     time_t          nTime;
     375             :     tm              aTM;
     376             :     sal_Int32           nLocalTime;
     377             :     sal_Int32           nUTC;
     378             :     short           nTempTime;
     379             : 
     380             :     // determine value again if needed
     381        1204 :     if ( (nCacheSecOffset == -1)            ||
     382        1188 :          ((nTicks - nCacheTicks) > 360000)  ||
     383         594 :          ( nTicks < nCacheTicks ) // handle overflow
     384             :          )
     385             :     {
     386          16 :         nTime = time( 0 );
     387          16 :         localtime_r( &nTime, &aTM );
     388          16 :         nLocalTime = mktime( &aTM );
     389             : #if defined( SOLARIS )
     390             :         // Solaris gmtime_r() seems not to handle daylight saving time
     391             :         // flags correctly
     392             :         nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone );
     393             : #elif defined( LINUX )
     394             :         // Linux mktime() seems not to handle tm_isdst correctly
     395          16 :         nUTC = nLocalTime - aTM.tm_gmtoff;
     396             : #else
     397             :          gmtime_r( &nTime, &aTM );
     398             :          nUTC = mktime( &aTM );
     399             : #endif
     400          16 :          nCacheTicks = nTicks;
     401          16 :          nCacheSecOffset = (nLocalTime-nUTC) / 60;
     402             :     }
     403             : 
     404         610 :     nTempTime = abs( nCacheSecOffset );
     405         610 :     tools::Time aTime( 0, (sal_uInt16)nTempTime );
     406         610 :     if ( nCacheSecOffset < 0 )
     407           0 :         aTime = -aTime;
     408         610 :     return aTime;
     409             : #endif
     410             : }
     411             : 
     412   162810122 : sal_uInt64 tools::Time::GetSystemTicks()
     413             : {
     414             : #if defined WNT
     415             :     static LARGE_INTEGER nTicksPerSecond;
     416             :     static bool bTicksPerSecondInitialized = false;
     417             :     if (!bTicksPerSecondInitialized)
     418             :     {
     419             :         QueryPerformanceFrequency(&nTicksPerSecond);
     420             :         bTicksPerSecondInitialized = true;
     421             :     }
     422             : 
     423             :     LARGE_INTEGER nPerformanceCount;
     424             :     QueryPerformanceCounter(&nPerformanceCount);
     425             : 
     426             :     return static_cast<sal_uInt64>(
     427             :         (nPerformanceCount.QuadPart*1000)/nTicksPerSecond.QuadPart);
     428             : #else
     429             :     timeval tv;
     430   162810122 :     int n = gettimeofday (&tv, 0);
     431   162810122 :     if (n == -1) {
     432           0 :         int e = errno;
     433             :         SAL_WARN("tools.datetime", "gettimeofday failed: " << e);
     434             :     }
     435   162810122 :     return static_cast<sal_uInt64>(tv.tv_sec) * 1000
     436   162810122 :         + (static_cast<sal_uInt64>(tv.tv_usec) + 500) / 1000;
     437             : #endif
     438             : }
     439             : 
     440             : } /* namespace tools */
     441             : 
     442             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11