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

Generated by: LCOV version 1.10