LCOV - code coverage report
Current view: top level - tools/source/datetime - datetime.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 107 148 72.3 %
Date: 2015-06-13 12:38:46 Functions: 12 18 66.7 %
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             : #include <tools/datetime.hxx>
      20             : #include <rtl/math.hxx>
      21             : 
      22         256 : DateTime::DateTime( const css::util::DateTime& rDateTime )
      23             :   : Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ),
      24         256 :     Time( rDateTime.Hours, rDateTime.Minutes, rDateTime.Seconds, rDateTime.NanoSeconds )
      25             : {
      26         256 : }
      27             : 
      28           0 : bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
      29             : {
      30           0 :     if ( (*this >= rFrom) && (*this <= rTo) )
      31           0 :         return true;
      32             :     else
      33           0 :         return false;
      34             : }
      35             : 
      36           0 : bool DateTime::operator >( const DateTime& rDateTime ) const
      37             : {
      38           0 :     if ( (Date::operator>( rDateTime )) ||
      39           0 :          (Date::operator==( rDateTime ) && tools::Time::operator>( rDateTime )) )
      40           0 :         return true;
      41             :     else
      42           0 :         return false;
      43             : }
      44             : 
      45           3 : bool DateTime::operator <( const DateTime& rDateTime ) const
      46             : {
      47           6 :     if ( (Date::operator<( rDateTime )) ||
      48           4 :          (Date::operator==( rDateTime ) && tools::Time::operator<( rDateTime )) )
      49           1 :         return true;
      50             :     else
      51           2 :         return false;
      52             : }
      53             : 
      54          30 : bool DateTime::operator >=( const DateTime& rDateTime ) const
      55             : {
      56          60 :     if ( (Date::operator>( rDateTime )) ||
      57          60 :          (Date::operator==( rDateTime ) && tools::Time::operator>=( rDateTime )) )
      58           0 :         return true;
      59             :     else
      60          30 :         return false;
      61             : }
      62             : 
      63           2 : bool DateTime::operator <=( const DateTime& rDateTime ) const
      64             : {
      65           4 :     if ( (Date::operator<( rDateTime )) ||
      66           2 :          (Date::operator==( rDateTime ) && tools::Time::operator<=( rDateTime )) )
      67           0 :         return true;
      68             :     else
      69           2 :         return false;
      70             : }
      71             : 
      72          42 : long DateTime::GetSecFromDateTime( const Date& rDate ) const
      73             : {
      74          42 :     if ( Date::operator<( rDate ) )
      75           0 :         return 0;
      76             :     else
      77             :     {
      78          42 :         long nSec = Date( *this ) - rDate;
      79          42 :         nSec *= 24UL*60*60;
      80          42 :         long nHour = GetHour();
      81          42 :         long nMin  = GetMin();
      82          42 :         nSec += (nHour*3600)+(nMin*60)+GetSec();
      83          42 :         return nSec;
      84             :     }
      85             : }
      86             : 
      87         451 : DateTime& DateTime::operator +=( const tools::Time& rTime )
      88             : {
      89         451 :     tools::Time aTime = *this;
      90         451 :     aTime += rTime;
      91         451 :     sal_uInt16 nHours = aTime.GetHour();
      92         451 :     if ( aTime.GetTime() > 0 )
      93             :     {
      94         937 :         while ( nHours >= 24 )
      95             :         {
      96          35 :             Date::operator++();
      97          35 :             nHours -= 24;
      98             :         }
      99         451 :         aTime.SetHour( nHours );
     100             :     }
     101           0 :     else if ( aTime.GetTime() != 0 )
     102             :     {
     103           0 :         while ( nHours >= 24 )
     104             :         {
     105           0 :             Date::operator--();
     106           0 :             nHours -= 24;
     107             :         }
     108           0 :         Date::operator--();
     109           0 :         aTime = Time( 24, 0, 0 )+aTime;
     110             :     }
     111         451 :     tools::Time::operator=( aTime );
     112             : 
     113         451 :     return *this;
     114             : }
     115             : 
     116         166 : DateTime& DateTime::operator -=( const tools::Time& rTime )
     117             : {
     118         166 :     tools::Time aTime = *this;
     119         166 :     aTime -= rTime;
     120         166 :     sal_uInt16 nHours = aTime.GetHour();
     121         166 :     if ( aTime.GetTime() > 0 )
     122             :     {
     123         308 :         while ( nHours >= 24 )
     124             :         {
     125           0 :             Date::operator++();
     126           0 :             nHours -= 24;
     127             :         }
     128         154 :         aTime.SetHour( nHours );
     129             :     }
     130          12 :     else if ( aTime.GetTime() != 0 )
     131             :     {
     132          24 :         while ( nHours >= 24 )
     133             :         {
     134           0 :             Date::operator--();
     135           0 :             nHours -= 24;
     136             :         }
     137          12 :         Date::operator--();
     138          12 :         aTime = Time( 24, 0, 0 )+aTime;
     139             :     }
     140         166 :     tools::Time::operator=( aTime );
     141             : 
     142         166 :     return *this;
     143             : }
     144             : 
     145           0 : DateTime operator +( const DateTime& rDateTime, long nDays )
     146             : {
     147           0 :     DateTime aDateTime( rDateTime );
     148           0 :     aDateTime += nDays;
     149           0 :     return aDateTime;
     150             : }
     151             : 
     152           0 : DateTime operator -( const DateTime& rDateTime, long nDays )
     153             : {
     154           0 :     DateTime aDateTime( rDateTime );
     155           0 :     aDateTime -= nDays;
     156           0 :     return aDateTime;
     157             : }
     158             : 
     159           0 : DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
     160             : {
     161           0 :     DateTime aDateTime( rDateTime );
     162           0 :     aDateTime += rTime;
     163           0 :     return aDateTime;
     164             : }
     165             : 
     166           0 : DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
     167             : {
     168           0 :     DateTime aDateTime( rDateTime );
     169           0 :     aDateTime -= rTime;
     170           0 :     return aDateTime;
     171             : }
     172             : 
     173           6 : DateTime& DateTime::operator +=( double fTimeInDays )
     174             : {
     175             :     double fInt, fFrac;
     176           6 :     if ( fTimeInDays < 0.0 )
     177             :     {
     178           0 :         fInt = ::rtl::math::approxCeil( fTimeInDays );
     179           0 :         fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
     180             :     }
     181             :     else
     182             :     {
     183           6 :         fInt = ::rtl::math::approxFloor( fTimeInDays );
     184           6 :         fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
     185             :     }
     186           6 :     Date::operator+=( long(fInt) );     // full days
     187           6 :     if ( fFrac )
     188             :     {
     189           3 :         tools::Time aTime(0);  // default ctor calls system time, we don't need that
     190           3 :         fFrac *= ::tools::Time::nanoSecPerDay;   // time expressed in nanoseconds
     191           3 :         aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) );    // method handles negative ns
     192           3 :         operator+=( aTime );
     193             :     }
     194           6 :     return *this;
     195             : }
     196             : 
     197           1 : DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
     198             : {
     199           1 :     DateTime aDateTime( rDateTime );
     200           1 :     aDateTime += fTimeInDays;
     201           1 :     return aDateTime;
     202             : }
     203             : 
     204        3416 : double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
     205             : {
     206        3416 :     long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
     207        3416 :     sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
     208        3416 :     if ( nTime )
     209             :     {
     210         212 :         double fTime = double(nTime);
     211         212 :         fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
     212         212 :         if ( nDays < 0 && fTime > 0.0 )
     213           0 :             fTime = 1.0 - fTime;
     214         212 :         return double(nDays) + fTime;
     215             :     }
     216        3204 :     return double(nDays);
     217             : }
     218             : 
     219         208 : void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
     220             : {
     221         208 :     const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
     222         208 :     const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
     223             : 
     224         208 :     sal_Int64 nYears = GetYear() - 1601;
     225             :     sal_Int64 nDays =
     226         416 :         nYears * 365 +
     227         624 :         nYears / 4 - nYears / 100 + nYears / 400 +
     228         416 :         GetDayOfYear() - 1;
     229             : 
     230             :     sal_Int64 aTime =
     231         208 :         a100nPerDay * nDays +
     232         208 :         GetNSFromTime()/100;
     233             : 
     234         208 :     rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
     235         208 :     rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
     236         208 : }
     237             : 
     238         670 : DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
     239             : {
     240         670 :     const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
     241         670 :     const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
     242             : 
     243             :     sal_Int64 aTime = sal_Int64(
     244         670 :             sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
     245         670 :             sal_uInt64( rLower ) );
     246             : 
     247         670 :     sal_Int64 nDays = aTime / a100nPerDay;
     248             :     sal_Int64 nYears =
     249         670 :         ( nDays -
     250        1340 :           ( nDays / ( 4 * 365 ) ) +
     251        1340 :           ( nDays / ( 100 * 365 ) ) -
     252        1340 :           ( nDays / ( 400 * 365 ) ) ) / 365;
     253         670 :     nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
     254             : 
     255         670 :     sal_uInt16 nMonths = 0;
     256        4456 :     for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
     257             :     {
     258        3116 :         nDays = nDaysCount;
     259        3116 :         nMonths ++;
     260             :         nDaysCount -= Date(
     261        3116 :             1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
     262        3116 :             GetDaysInMonth();
     263             :     }
     264             : 
     265             :     Date _aDate(
     266             :         (sal_uInt16)( nDays + 1 ), nMonths,
     267         670 :         sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
     268         670 :     tools::Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
     269         670 :                  sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) )      % sal_Int64( 60 ) ),
     270         670 :                  sal_uIntPtr( ( aTime / ( a100nPerSecond ) )           % sal_Int64( 60 ) ),
     271        2680 :                  (aTime % a100nPerSecond) * 100 );
     272             : 
     273         670 :     return DateTime( _aDate, _aTime );
     274             : }
     275             : 
     276             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11