LCOV - code coverage report
Current view: top level - libreoffice/tools/source/datetime - datetime.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 34 137 24.8 %
Date: 2012-12-27 Functions: 3 17 17.6 %
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 : sal_Bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
      23             : {
      24           0 :     if ( (*this >= rFrom) && (*this <= rTo) )
      25           0 :         return sal_True;
      26             :     else
      27           0 :         return sal_False;
      28             : }
      29             : 
      30           0 : sal_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 sal_True;
      35             :     else
      36           0 :         return sal_False;
      37             : }
      38             : 
      39           0 : sal_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 sal_True;
      44             :     else
      45           0 :         return sal_False;
      46             : }
      47             : 
      48           0 : sal_Bool DateTime::operator >=( const DateTime& rDateTime ) const
      49             : {
      50           0 :     if ( (Date::operator>( rDateTime )) ||
      51           0 :          (Date::operator==( rDateTime ) && Time::operator>=( rDateTime )) )
      52           0 :         return sal_True;
      53             :     else
      54           0 :         return sal_False;
      55             : }
      56             : 
      57           0 : sal_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 sal_True;
      62             :     else
      63           0 :         return sal_False;
      64             : }
      65             : 
      66           0 : long DateTime::GetSecFromDateTime( const Date& rDate ) const
      67             : {
      68           0 :     if ( Date::operator<( rDate ) )
      69           0 :         return 0;
      70             :     else
      71             :     {
      72           0 :         long nSec = Date( *this ) - rDate;
      73           0 :         nSec *= 24UL*60*60;
      74           0 :         long nHour = GetHour();
      75           0 :         long nMin  = GetMin();
      76           0 :         nSec += (nHour*3600)+(nMin*60)+GetSec();
      77           0 :         return nSec;
      78             :     }
      79             : }
      80             : 
      81         101 : DateTime& DateTime::operator +=( const Time& rTime )
      82             : {
      83         101 :     Time aTime = *this;
      84         101 :     aTime += rTime;
      85         101 :     sal_uInt16 nHours = aTime.GetHour();
      86         101 :     if ( aTime.GetTime() > 0 )
      87             :     {
      88         202 :         while ( nHours >= 24 )
      89             :         {
      90           0 :             Date::operator++();
      91           0 :             nHours -= 24;
      92             :         }
      93         101 :         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         101 :     Time::operator=( aTime );
     106             : 
     107         101 :     return *this;
     108             : }
     109             : 
     110           0 : DateTime& DateTime::operator -=( const Time& rTime )
     111             : {
     112           0 :     Time aTime = *this;
     113           0 :     aTime -= rTime;
     114           0 :     sal_uInt16 nHours = aTime.GetHour();
     115           0 :     if ( aTime.GetTime() > 0 )
     116             :     {
     117           0 :         while ( nHours >= 24 )
     118             :         {
     119           0 :             Date::operator++();
     120           0 :             nHours -= 24;
     121             :         }
     122           0 :         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           0 :     Time::operator=( aTime );
     135             : 
     136           0 :     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 *= 24UL * 60 * 60 * 1000;     // time expressed in milliseconds
     185           0 :         aTime.MakeTimeFromMS( long(fFrac) );    // method handles negative ms
     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         358 : double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
     199             : {
     200         358 :     long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
     201         358 :     long nTime = rDateTime1.GetMSFromTime() - rDateTime2.GetMSFromTime();
     202         358 :     if ( nTime )
     203             :     {
     204           6 :         double fTime = double(nTime);
     205           6 :         fTime /= 24UL * 60 * 60 * 1000; // convert from milliseconds to fraction
     206           6 :         if ( nDays < 0 && fTime > 0.0 )
     207           0 :             fTime = 1.0 - fTime;
     208           6 :         return double(nDays) + fTime;
     209             :     }
     210         352 :     return double(nDays);
     211             : }
     212             : 
     213           0 : void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
     214             : {
     215           0 :     const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
     216           0 :     const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
     217             : 
     218           0 :     sal_Int64 nYears = GetYear() - 1601;
     219             :     sal_Int64 nDays =
     220             :         nYears * 365 +
     221             :         nYears / 4 - nYears / 100 + nYears / 400 +
     222           0 :         GetDayOfYear() - 1;
     223             : 
     224             :     sal_Int64 aTime =
     225             :         a100nPerDay * nDays +
     226             :         a100nPerSecond * (
     227           0 :                 sal_Int64( GetSec() ) +
     228           0 :                 60 * sal_Int64( GetMin() ) +
     229           0 :                 60 * 60 * sal_Int64( GetHour() ) );
     230             : 
     231           0 :     rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
     232           0 :     rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
     233           0 : }
     234             : 
     235         149 : DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
     236             : {
     237         149 :     const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
     238         149 :     const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
     239             : 
     240             :     sal_Int64 aTime = sal_Int64(
     241             :             sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
     242         149 :             sal_uInt64( rLower ) );
     243             : 
     244         149 :     sal_Int64 nDays = aTime / a100nPerDay;
     245             :     sal_Int64 nYears =
     246             :         ( nDays -
     247             :           ( nDays / ( 4 * 365 ) ) +
     248             :           ( nDays / ( 100 * 365 ) ) -
     249         149 :           ( nDays / ( 400 * 365 ) ) ) / 365;
     250         149 :     nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
     251             : 
     252         149 :     sal_uInt16 nMonths = 0;
     253        1059 :     for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
     254             :     {
     255         761 :         nDays = nDaysCount;
     256         761 :         nMonths ++;
     257             :         nDaysCount -= Date(
     258         761 :             1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
     259         761 :             GetDaysInMonth();
     260             :     }
     261             : 
     262             :     Date _aDate(
     263             :         (sal_uInt16)( nDays + 1 ), nMonths,
     264         149 :         sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
     265             :     Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
     266             :             sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
     267         149 :             sal_uIntPtr( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ) );
     268             : 
     269         149 :     return DateTime( _aDate, _aTime );
     270             : }
     271             : 
     272             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10