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

Generated by: LCOV version 1.11