LCOV - code coverage report
Current view: top level - include/tools - date.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 29 37 78.4 %
Date: 2014-11-03 Functions: 17 21 81.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             : #ifndef INCLUDED_TOOLS_DATE_HXX
      20             : #define INCLUDED_TOOLS_DATE_HXX
      21             : 
      22             : #include <tools/toolsdllapi.h>
      23             : #include <com/sun/star/util/Date.hpp>
      24             : #include <sal/log.hxx>
      25             : 
      26             : class ResId;
      27             : 
      28             : enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
      29             :                  SATURDAY, SUNDAY };
      30             : 
      31             : // TODO FIXME: make it handle signed year?
      32             : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Date
      33             : {
      34             : private:
      35             :     sal_uInt32      nDate;
      36      126958 :     void            init( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
      37             :                         { nDate = (   sal_uInt32( nDay   % 100 ) ) +
      38      253916 :                                   ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
      39      253916 :                                   ( ( sal_uInt32( nYear  % 10000 ) ) * 10000); }
      40             : 
      41             : public:
      42             :     enum DateInitSystem
      43             :     {
      44             :         SYSTEM
      45             :     };
      46             : 
      47             :     // TODO temporary until all uses are inspected and resolved
      48             :     enum DateInitEmpty
      49             :     {
      50             :         EMPTY
      51             :     };
      52             : 
      53       84456 :                     Date( DateInitEmpty)
      54       84456 :                         { nDate = 0; }
      55             :                     Date( DateInitSystem );
      56             :                     Date( const ResId & rResId );
      57         632 :                     Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
      58       84850 :                     Date( const Date& rDate )
      59       84850 :                         { nDate = rDate.nDate; }
      60      126622 :                     Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
      61      126622 :                         { init(nDay, nMonth, nYear); }
      62         336 :                     Date( const ::com::sun::star::util::Date& _rDate )
      63             :                     {
      64             :                         SAL_WARN_IF(_rDate.Year < 0, "tools.datetime", "Negative year in css::util::Date to ::Date conversion");
      65         336 :                         init(_rDate.Day, _rDate.Month, _rDate.Year);
      66         336 :                     }
      67             : 
      68          16 :     void            SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
      69        2270 :     sal_uInt32      GetDate() const { return nDate; }
      70          44 :     ::com::sun::star::util::Date GetUNODate() const { return ::com::sun::star::util::Date(GetDay(), GetMonth(), GetYear()); }
      71             : 
      72             :     void            SetDay( sal_uInt16 nNewDay );
      73             :     void            SetMonth( sal_uInt16 nNewMonth );
      74             :     void            SetYear( sal_uInt16 nNewYear );
      75       40010 :     sal_uInt16      GetDay() const { return (sal_uInt16)(nDate % 100); }
      76       40064 :     sal_uInt16      GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
      77       43198 :     sal_uInt16      GetYear() const { return (sal_uInt16)(nDate / 10000); }
      78             : 
      79             :     /** Obtain the day of the week for the date.
      80             : 
      81             :         Internally normalizes a copy of values.
      82             :         The result may be unexpected for a non-normalized invalid date like
      83             :         Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
      84             :      */
      85             :     DayOfWeek       GetDayOfWeek() const;
      86             : 
      87             :     /** Obtain the day of the year for the date.
      88             : 
      89             :         Internally normalizes a copy of values.
      90             :         The result may be unexpected for a non-normalized invalid date like
      91             :         Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
      92             :      */
      93             :     sal_uInt16      GetDayOfYear() const;
      94             : 
      95             :     /** Obtain the week of the year for a date.
      96             : 
      97             :         @param nMinimumNumberOfDaysInWeek
      98             :                How many days of a week must reside in the first week of a year.
      99             : 
     100             :         Internally normalizes a copy of values.
     101             :         The result may be unexpected for a non-normalized invalid date like
     102             :         Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
     103             :      */
     104             :     sal_uInt16      GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
     105             :                                    sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
     106             : 
     107             :     /** Obtain the number of days in the month of the year of the date.
     108             : 
     109             :         Internally normalizes a copy of values.
     110             : 
     111             :         The result may be unexpected for a non-normalized invalid date like
     112             :         Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
     113             : 
     114             :         These would result in 31 as --11-31 rolls over to --12-01 and the
     115             :         number of days in December is returned.
     116             : 
     117             :         Instead, to obtain the value for the actual set use the static method
     118             :         Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.
     119             :      */
     120             :     sal_uInt16      GetDaysInMonth() const;
     121             : 
     122           0 :     sal_uInt16      GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
     123             :     bool            IsLeapYear() const;
     124             : 
     125             :     /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
     126             :         depending on month/year) AND is of the Gregorian calendar (1582-10-15
     127             :         <= date) (AND implicitly date <= 9999-12-31 due to internal
     128             :         representation) */
     129             :     bool            IsValidAndGregorian() const;
     130             : 
     131             :     /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
     132             :         depending on month/year) */
     133             :     bool            IsValidDate() const;
     134             : 
     135             :     /** Normalize date, invalid day or month values are adapted such that they
     136             :         carry over to the next month or/and year, for example 1999-02-32
     137             :         becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes
     138             :         2000-02-11. Truncates at 9999-12-31, 0000-00-x will yield the
     139             :         normalized value of 0000-01-max(1,(x-31))
     140             : 
     141             :         This may be necessary after Date ctors or if the SetDate(), SetDay(),
     142             :         SetMonth(), SetYear() methods set individual non-matching values.
     143             :         Adding/subtracting to/from dates never produces invalid dates.
     144             : 
     145             :         @returns TRUE if the date was normalized, i.e. not valid before.
     146             :      */
     147             :     bool            Normalize();
     148             : 
     149           0 :     bool            IsBetween( const Date& rFrom, const Date& rTo ) const
     150           0 :                         { return ((nDate >= rFrom.nDate) &&
     151           0 :                                  (nDate <= rTo.nDate)); }
     152             : 
     153        3671 :     bool            operator ==( const Date& rDate ) const
     154        3671 :                         { return (nDate == rDate.nDate); }
     155          82 :     bool            operator !=( const Date& rDate ) const
     156          82 :                         { return (nDate != rDate.nDate); }
     157         566 :     bool            operator  >( const Date& rDate ) const
     158         566 :                         { return (nDate > rDate.nDate); }
     159         442 :     bool            operator  <( const Date& rDate ) const
     160         442 :                         { return (nDate < rDate.nDate); }
     161           0 :     bool            operator >=( const Date& rDate ) const
     162           0 :                         { return (nDate >= rDate.nDate); }
     163           0 :     bool            operator <=( const Date& rDate ) const
     164           0 :                         { return (nDate <= rDate.nDate); }
     165             : 
     166       76432 :     Date&           operator =( const Date& rDate )
     167       76432 :                         { nDate = rDate.nDate; return *this; }
     168             :     Date&           operator +=( long nDays );
     169             :     Date&           operator -=( long nDays );
     170             :     Date&           operator ++();
     171             :     Date&           operator --();
     172             :     Date            operator ++( int );
     173             :     Date            operator --( int );
     174             : 
     175             :     TOOLS_DLLPUBLIC friend Date     operator +( const Date& rDate, long nDays );
     176             :     TOOLS_DLLPUBLIC friend Date     operator -( const Date& rDate, long nDays );
     177             :     TOOLS_DLLPUBLIC friend long     operator -( const Date& rDate1, const Date& rDate2 );
     178             : 
     179             :     /** Obtain number of days in a month of a year.
     180             : 
     181             :         Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not
     182             :         normalize values.
     183             :      */
     184             :     static sal_uInt16 GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
     185             : 
     186             :     /// Internally normalizes values.
     187             :     static long DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
     188             :     /// Semantically identical to IsValidDate() member method.
     189             :     static bool IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
     190             :     /// Semantically identical to Normalize() member method.
     191             :     static bool Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear );
     192             : 
     193             :  private:
     194             :     /// An accelerated form of DateToDays on this date
     195             :     long GetAsNormalizedDays() const;
     196             : };
     197             : 
     198             : #endif
     199             : 
     200             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10