LCOV - code coverage report
Current view: top level - libreoffice/tools/source/datetime - ttime.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 99 132 75.0 %
Date: 2012-12-27 Functions: 14 20 70.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             : #if defined( WNT )
      21             : #include <windows.h>
      22             : #elif defined UNX
      23             : #include <unistd.h>
      24             : #include <limits.h>
      25             : #include <math.h>
      26             : #include <sys/time.h>
      27             : #endif
      28             : 
      29             : #include <time.h>
      30             : #include <tools/time.hxx>
      31             : 
      32             : #if defined(SOLARIS) && defined(__GNUC__)
      33             : extern long altzone;
      34             : #endif
      35             : 
      36         202 : static sal_Int32 TimeToSec100( const Time& rTime )
      37             : {
      38         202 :     short  nSign   = (rTime.GetTime() >= 0) ? +1 : -1;
      39         202 :     sal_Int32   nHour   = rTime.GetHour();
      40         202 :     sal_Int32   nMin    = rTime.GetMin();
      41         202 :     sal_Int32   nSec    = rTime.GetSec();
      42         202 :     sal_Int32   n100Sec = rTime.Get100Sec();
      43             : 
      44             : //  Due to interal compiler error in MSC a little bit more complicated:
      45             : //  return (n100Sec + (nSec*100) + (nMin*60*100) + (nHour*60*60*100) * nSign);
      46             : 
      47         202 :     sal_Int32 nRet = n100Sec;
      48         202 :     nRet     += nSec*100;
      49         202 :     nRet     += nMin*60*100;
      50         202 :     nRet     += nHour*60*60*100;
      51             : 
      52         202 :     return (nRet * nSign);
      53             : }
      54             : 
      55         101 : static Time Sec100ToTime( sal_Int32 nSec100 )
      56             : {
      57             :     short nSign;
      58         101 :     if ( nSec100 < 0 )
      59             :     {
      60           0 :         nSec100 *= -1;
      61           0 :         nSign = -1;
      62             :     }
      63             :     else
      64         101 :         nSign = 1;
      65             : 
      66         101 :     Time aTime( 0, 0, 0, nSec100 );
      67         101 :     aTime.SetTime( aTime.GetTime() * nSign );
      68         101 :     return aTime;
      69             : }
      70             : 
      71        2219 : Time::Time( TimeInitSystem )
      72             : {
      73             : #if defined( WNT )
      74             :     SYSTEMTIME aDateTime;
      75             :     GetLocalTime( &aDateTime );
      76             : 
      77             :     // construct time
      78             :     nTime = (((sal_Int32)aDateTime.wHour)*1000000) +
      79             :             (((sal_Int32)aDateTime.wMinute)*10000) +
      80             :             (((sal_Int32)aDateTime.wSecond)*100) +
      81             :             ((sal_Int32)aDateTime.wMilliseconds/10);
      82             : #else
      83             :     time_t     nTmpTime;
      84             :     struct tm aTime;
      85             : 
      86             :     // determine time
      87        2219 :     nTmpTime = time( 0 );
      88             : 
      89             :     // construct time
      90        2219 :     if ( localtime_r( &nTmpTime, &aTime ) )
      91             :     {
      92             :         nTime = (((sal_Int32)aTime.tm_hour)*1000000) +
      93             :                 (((sal_Int32)aTime.tm_min)*10000) +
      94        2219 :                 (((sal_Int32)aTime.tm_sec)*100);
      95             :     }
      96             :     else
      97           0 :         nTime = 0;
      98             : #endif
      99        2219 : }
     100             : 
     101        1793 : Time::Time( const Time& rTime )
     102             : {
     103        1793 :     nTime = rTime.nTime;
     104        1793 : }
     105             : 
     106       14345 : Time::Time( sal_uIntPtr nHour, sal_uIntPtr nMin, sal_uIntPtr nSec, sal_uIntPtr n100Sec )
     107             : {
     108             :     // normalize time
     109       14345 :     nSec    += n100Sec / 100;
     110       14345 :     n100Sec  = n100Sec % 100;
     111       14345 :     nMin    += nSec / 60;
     112       14345 :     nSec     = nSec % 60;
     113       14345 :     nHour   += nMin / 60;
     114       14345 :     nMin     = nMin % 60;
     115             : 
     116             :     // construct time
     117       14345 :     nTime = (sal_Int32)(n100Sec + (nSec*100) + (nMin*10000) + (nHour*1000000));
     118       14345 : }
     119             : 
     120         109 : void Time::SetHour( sal_uInt16 nNewHour )
     121             : {
     122         109 :     short  nSign      = (nTime >= 0) ? +1 : -1;
     123         109 :     sal_Int32   nMin      = GetMin();
     124         109 :     sal_Int32   nSec      = GetSec();
     125         109 :     sal_Int32   n100Sec   = Get100Sec();
     126             : 
     127             :     nTime = (n100Sec + (nSec*100) + (nMin*10000) +
     128         109 :             (((sal_Int32)nNewHour)*1000000)) * nSign;
     129         109 : }
     130             : 
     131           8 : void Time::SetMin( sal_uInt16 nNewMin )
     132             : {
     133           8 :     short  nSign      = (nTime >= 0) ? +1 : -1;
     134           8 :     sal_Int32   nHour     = GetHour();
     135           8 :     sal_Int32   nSec      = GetSec();
     136           8 :     sal_Int32   n100Sec   = Get100Sec();
     137             : 
     138             :     // no overflow
     139           8 :     nNewMin = nNewMin % 60;
     140             : 
     141             :     nTime = (n100Sec + (nSec*100) + (((sal_Int32)nNewMin)*10000) +
     142           8 :             (nHour*1000000)) * nSign;
     143           8 : }
     144             : 
     145         827 : void Time::SetSec( sal_uInt16 nNewSec )
     146             : {
     147         827 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     148         827 :     sal_Int32   nHour     = GetHour();
     149         827 :     sal_Int32   nMin      = GetMin();
     150         827 :     sal_Int32   n100Sec   = Get100Sec();
     151             : 
     152             :     // no overflow
     153         827 :     nNewSec = nNewSec % 60;
     154             : 
     155             :     nTime = (n100Sec + (((sal_Int32)nNewSec)*100) + (nMin*10000) +
     156         827 :             (nHour*1000000)) * nSign;
     157         827 : }
     158             : 
     159         819 : void Time::Set100Sec( sal_uInt16 nNew100Sec )
     160             : {
     161         819 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     162         819 :     sal_Int32   nHour     = GetHour();
     163         819 :     sal_Int32   nMin      = GetMin();
     164         819 :     sal_Int32   nSec      = GetSec();
     165             : 
     166             :     // no overflow
     167         819 :     nNew100Sec = nNew100Sec % 100;
     168             : 
     169             :     nTime = (((sal_Int32)nNew100Sec) + (nSec*100) + (nMin*10000) +
     170         819 :             (nHour*1000000)) * nSign;
     171         819 : }
     172             : 
     173         716 : sal_Int32 Time::GetMSFromTime() const
     174             : {
     175         716 :     short       nSign     = (nTime >= 0) ? +1 : -1;
     176         716 :     sal_Int32   nHour     = GetHour();
     177         716 :     sal_Int32   nMin      = GetMin();
     178         716 :     sal_Int32   nSec      = GetSec();
     179         716 :     sal_Int32   n100Sec   = Get100Sec();
     180             : 
     181         716 :     return (((nHour*3600000)+(nMin*60000)+(nSec*1000)+(n100Sec*10))*nSign);
     182             : }
     183             : 
     184           0 : void Time::MakeTimeFromMS( sal_Int32 nMS )
     185             : {
     186             :     short nSign;
     187           0 :     if ( nMS < 0 )
     188             :     {
     189           0 :         nMS *= -1;
     190           0 :         nSign = -1;
     191             :     }
     192             :     else
     193           0 :         nSign = 1;
     194             : 
     195           0 :     Time aTime( 0, 0, 0, nMS/10 );
     196           0 :     SetTime( aTime.GetTime() * nSign );
     197           0 : }
     198             : 
     199           0 : double Time::GetTimeInDays() const
     200             : {
     201           0 :     short  nSign      = (nTime >= 0) ? +1 : -1;
     202           0 :     double nHour      = GetHour();
     203           0 :     double nMin       = GetMin();
     204           0 :     double nSec       = GetSec();
     205           0 :     double n100Sec    = Get100Sec();
     206             : 
     207           0 :     return (nHour+(nMin/60)+(nSec/(60*60))+(n100Sec/(60*60*100))) / 24 * nSign;
     208             : }
     209             : 
     210       54238 : Time& Time::operator =( const Time& rTime )
     211             : {
     212       54238 :     nTime = rTime.nTime;
     213       54238 :     return *this;
     214             : }
     215             : 
     216         101 : Time& Time::operator +=( const Time& rTime )
     217             : {
     218         101 :     nTime = Sec100ToTime( TimeToSec100( *this ) +
     219         101 :                           TimeToSec100( rTime ) ).GetTime();
     220         101 :     return *this;
     221             : }
     222             : 
     223           0 : Time& Time::operator -=( const Time& rTime )
     224             : {
     225           0 :     nTime = Sec100ToTime( TimeToSec100( *this ) -
     226           0 :                           TimeToSec100( rTime ) ).GetTime();
     227           0 :     return *this;
     228             : }
     229             : 
     230           0 : Time operator +( const Time& rTime1, const Time& rTime2 )
     231             : {
     232           0 :     return Sec100ToTime( TimeToSec100( rTime1 ) +
     233           0 :                          TimeToSec100( rTime2 ) );
     234             : }
     235             : 
     236           0 : Time operator -( const Time& rTime1, const Time& rTime2 )
     237             : {
     238           0 :     return Sec100ToTime( TimeToSec100( rTime1 ) -
     239           0 :                          TimeToSec100( rTime2 ) );
     240             : }
     241             : 
     242           0 : sal_Bool Time::IsEqualIgnore100Sec( const Time& rTime ) const
     243             : {
     244           0 :     sal_Int32 n1 = (nTime < 0 ? -Get100Sec() : Get100Sec() );
     245           0 :     sal_Int32 n2 = (rTime.nTime < 0 ? -rTime.Get100Sec() : rTime.Get100Sec() );
     246           0 :     return (nTime - n1) == (rTime.nTime - n2);
     247             : }
     248             : 
     249         101 : Time Time::GetUTCOffset()
     250             : {
     251             : #if defined( WNT )
     252             :     TIME_ZONE_INFORMATION   aTimeZone;
     253             :     aTimeZone.Bias = 0;
     254             :     DWORD nTimeZoneRet = GetTimeZoneInformation( &aTimeZone );
     255             :     sal_Int32 nTempTime = aTimeZone.Bias;
     256             :     if ( nTimeZoneRet == TIME_ZONE_ID_STANDARD )
     257             :         nTempTime += aTimeZone.StandardBias;
     258             :     else if ( nTimeZoneRet == TIME_ZONE_ID_DAYLIGHT )
     259             :         nTempTime += aTimeZone.DaylightBias;
     260             :     Time aTime( 0, (sal_uInt16)Abs( nTempTime ) );
     261             :     if ( nTempTime > 0 )
     262             :         aTime = -aTime;
     263             :     return aTime;
     264             : #else
     265             :     static sal_uIntPtr  nCacheTicks = 0;
     266             :     static sal_Int32    nCacheSecOffset = -1;
     267         101 :     sal_uIntPtr         nTicks = Time::GetSystemTicks();
     268             :     time_t          nTime;
     269             :     tm              aTM;
     270             :     sal_Int32           nLocalTime;
     271             :     sal_Int32           nUTC;
     272             :     short           nTempTime;
     273             : 
     274             :     // determine value again if needed
     275         101 :     if ( (nCacheSecOffset == -1)            ||
     276             :          ((nTicks - nCacheTicks) > 360000)  ||
     277             :          ( nTicks < nCacheTicks ) // handle overflow
     278             :          )
     279             :     {
     280           6 :         nTime = time( 0 );
     281           6 :         localtime_r( &nTime, &aTM );
     282           6 :         nLocalTime = mktime( &aTM );
     283             : #if defined( SOLARIS )
     284             :         // Solaris gmtime_r() seems not to handle daylight saving time
     285             :         // flags correctly
     286             :         nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone );
     287             : #elif defined( LINUX )
     288             :         // Linux mktime() seems not to handle tm_isdst correctly
     289           6 :         nUTC = nLocalTime - aTM.tm_gmtoff;
     290             : #else
     291             :          gmtime_r( &nTime, &aTM );
     292             :          nUTC = mktime( &aTM );
     293             : #endif
     294           6 :          nCacheTicks = nTicks;
     295           6 :          nCacheSecOffset = (nLocalTime-nUTC) / 60;
     296             :     }
     297             : 
     298         101 :     nTempTime = (short)Abs( nCacheSecOffset );
     299         101 :     Time aTime( 0, (sal_uInt16)nTempTime );
     300         101 :     if ( nCacheSecOffset < 0 )
     301           0 :         aTime = -aTime;
     302         101 :     return aTime;
     303             : #endif
     304             : }
     305             : 
     306       25680 : sal_uIntPtr Time::GetSystemTicks()
     307             : {
     308             : #if defined WNT
     309             :     return (sal_uIntPtr)GetTickCount();
     310             : #else
     311             :     timeval tv;
     312       25680 :     gettimeofday (&tv, 0);
     313             : 
     314       25680 :     double fTicks = tv.tv_sec;
     315       25680 :     fTicks *= 1000;
     316       25680 :     fTicks += ((tv.tv_usec + 500) / 1000);
     317             : 
     318       25680 :     fTicks = fmod (fTicks, double(ULONG_MAX));
     319       25680 :     return sal_uIntPtr(fTicks);
     320             : #endif
     321             : }
     322             : 
     323             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10