xref: /AOO41X/main/tools/inc/tools/date.hxx (revision 8b851043d896eaadc6634f0a22437412985b1d4a)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _DATE_HXX
24 #define _DATE_HXX
25 
26 #include "tools/toolsdllapi.h"
27 #include <tools/solar.h>
28 
29 class ResId;
30 
31 // --------------
32 // - Date-Types -
33 // --------------
34 
35 enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
36                  SATURDAY, SUNDAY };
37 
38 // --------
39 // - Date -
40 // --------
41 
42 class TOOLS_DLLPUBLIC Date
43 {
44 private:
45     sal_uInt32      nDate;
46 
47 public:
48                     Date();
49                     Date( const ResId & rResId );
Date(sal_uInt32 _nDate)50                     Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
Date(const Date & rDate)51                     Date( const Date& rDate )
52                         { nDate = rDate.nDate; }
Date(sal_uInt16 nDay,sal_uInt16 nMonth,sal_uInt16 nYear)53                     Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
54                         { nDate = (   sal_uInt32( nDay   % 100 ) ) +
55                                   ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
56                                   ( ( sal_uInt32( nYear  % 10000 ) ) * 10000); }
57 
SetDate(sal_uInt32 nNewDate)58     void            SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
GetDate() const59     sal_uInt32      GetDate() const { return nDate; }
60 
61     void            SetDay( sal_uInt16 nNewDay );
62     void            SetMonth( sal_uInt16 nNewMonth );
63     void            SetYear( sal_uInt16 nNewYear );
GetDay() const64     sal_uInt16          GetDay() const { return (sal_uInt16)(nDate % 100); }
GetMonth() const65     sal_uInt16          GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
GetYear() const66     sal_uInt16          GetYear() const { return (sal_uInt16)(nDate / 10000); }
67 
68     DayOfWeek       GetDayOfWeek() const;
69     sal_uInt16          GetDayOfYear() const;
70     /** nMinimumNumberOfDaysInWeek: how many days of a week must reside in the
71         first week of a year. */
72     sal_uInt16          GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
73                                    sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
74 
75     sal_uInt16          GetDaysInMonth() const;
GetDaysInYear() const76     sal_uInt16          GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
77     sal_Bool            IsLeapYear() const;
78     sal_Bool            IsValid() const;
79 
IsBetween(const Date & rFrom,const Date & rTo) const80     sal_Bool            IsBetween( const Date& rFrom, const Date& rTo ) const
81                         { return ((nDate >= rFrom.nDate) &&
82                                  (nDate <= rTo.nDate)); }
83 
operator ==(const Date & rDate) const84     sal_Bool            operator ==( const Date& rDate ) const
85                         { return (nDate == rDate.nDate); }
operator !=(const Date & rDate) const86     sal_Bool            operator !=( const Date& rDate ) const
87                         { return (nDate != rDate.nDate); }
operator >(const Date & rDate) const88     sal_Bool            operator  >( const Date& rDate ) const
89                         { return (nDate > rDate.nDate); }
operator <(const Date & rDate) const90     sal_Bool            operator  <( const Date& rDate ) const
91                         { return (nDate < rDate.nDate); }
operator >=(const Date & rDate) const92     sal_Bool            operator >=( const Date& rDate ) const
93                         { return (nDate >= rDate.nDate); }
operator <=(const Date & rDate) const94     sal_Bool            operator <=( const Date& rDate ) const
95                         { return (nDate <= rDate.nDate); }
96 
operator =(const Date & rDate)97     Date&           operator =( const Date& rDate )
98                         { nDate = rDate.nDate; return *this; }
99     Date&           operator +=( long nDays );
100     Date&           operator -=( long nDays );
101     Date&           operator ++();
102     Date&           operator --();
103 #ifndef MPW33
104     Date            operator ++( int );
105     Date            operator --( int );
106 #endif
107 
108     TOOLS_DLLPUBLIC friend Date     operator +( const Date& rDate, long nDays );
109     TOOLS_DLLPUBLIC friend Date     operator -( const Date& rDate, long nDays );
110     TOOLS_DLLPUBLIC friend long     operator -( const Date& rDate1, const Date& rDate2 );
111 
112     static long DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
113 
114 };
115 
116 #endif // _DATE_HXX
117