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 _DATE_HXX
20 : #define _DATE_HXX
21 :
22 : #include "tools/toolsdllapi.h"
23 : #include <tools/solar.h>
24 :
25 : class ResId;
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 :
36 : public:
37 : enum DateInitSystem
38 : {
39 : SYSTEM
40 : };
41 :
42 : // TODO temporary until all uses are inspected and resolved
43 : enum DateInitEmpty
44 : {
45 : EMPTY
46 : };
47 :
48 22552 : Date( DateInitEmpty)
49 22552 : { nDate = 0; }
50 : Date( DateInitSystem );
51 : Date( const ResId & rResId );
52 294 : Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
53 49808 : Date( const Date& rDate )
54 49808 : { nDate = rDate.nDate; }
55 29643 : Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
56 : { nDate = ( sal_uInt32( nDay % 100 ) ) +
57 59286 : ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
58 59286 : ( ( sal_uInt32( nYear % 10000 ) ) * 10000); }
59 :
60 0 : void SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
61 700 : sal_uInt32 GetDate() const { return nDate; }
62 :
63 : void SetDay( sal_uInt16 nNewDay );
64 : void SetMonth( sal_uInt16 nNewMonth );
65 : void SetYear( sal_uInt16 nNewYear );
66 26999 : sal_uInt16 GetDay() const { return (sal_uInt16)(nDate % 100); }
67 27037 : sal_uInt16 GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
68 27963 : sal_uInt16 GetYear() const { return (sal_uInt16)(nDate / 10000); }
69 :
70 : /** Obtain the day of the week for the date.
71 :
72 : Internally normalizes a copy of values.
73 : The result may be unexpected for a non-normalized invalid date like
74 : Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
75 : */
76 : DayOfWeek GetDayOfWeek() const;
77 :
78 : /** Obtain the day of the year 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 : sal_uInt16 GetDayOfYear() const;
85 :
86 : /** Obtain the week of the year for a date.
87 :
88 : @param nMinimumNumberOfDaysInWeek
89 : How many days of a week must reside in the first week of a year.
90 :
91 : Internally normalizes a copy of values.
92 : The result may be unexpected for a non-normalized invalid date like
93 : Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
94 : */
95 : sal_uInt16 GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
96 : sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
97 :
98 : /** Obtain the number of days in the month of the year of the date.
99 :
100 : Internally normalizes a copy of values.
101 :
102 : The result may be unexpected for a non-normalized invalid date like
103 : Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
104 :
105 : These would result in 31 as --11-31 rolls over to --12-01 and the
106 : number of days in December is returned.
107 :
108 : Instead, to obtain the value for the actual set use the static method
109 : Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.
110 : */
111 : sal_uInt16 GetDaysInMonth() const;
112 :
113 0 : sal_uInt16 GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
114 : bool IsLeapYear() const;
115 :
116 : /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
117 : depending on month/year) AND is of the Gregorian calendar (1582-10-15
118 : <= date) (AND implicitly date <= 9999-12-31 due to internal
119 : representation) */
120 : bool IsValidAndGregorian() const;
121 :
122 : /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
123 : depending on month/year) */
124 : bool IsValidDate() const;
125 :
126 : /** Normalize date, invalid day or month values are adapted such that they
127 : carry over to the next month or/and year, for example 1999-02-32
128 : becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes
129 : 2000-02-11. Truncates at 9999-12-31, 0000-00-x will yield the
130 : normalized value of 0000-01-max(1,(x-31))
131 :
132 : This may be necessary after Date ctors or if the SetDate(), SetDay(),
133 : SetMonth(), SetYear() methods set individual non-matching values.
134 : Adding/subtracting to/from dates never produces invalid dates.
135 :
136 : @returns TRUE if the date was normalized, i.e. not valid before.
137 : */
138 : bool Normalize();
139 :
140 0 : bool IsBetween( const Date& rFrom, const Date& rTo ) const
141 0 : { return ((nDate >= rFrom.nDate) &&
142 0 : (nDate <= rTo.nDate)); }
143 :
144 209 : bool operator ==( const Date& rDate ) const
145 209 : { return (nDate == rDate.nDate); }
146 17 : bool operator !=( const Date& rDate ) const
147 17 : { return (nDate != rDate.nDate); }
148 275 : bool operator >( const Date& rDate ) const
149 275 : { return (nDate > rDate.nDate); }
150 229 : bool operator <( const Date& rDate ) const
151 229 : { return (nDate < rDate.nDate); }
152 0 : bool operator >=( const Date& rDate ) const
153 0 : { return (nDate >= rDate.nDate); }
154 0 : bool operator <=( const Date& rDate ) const
155 0 : { return (nDate <= rDate.nDate); }
156 :
157 43694 : Date& operator =( const Date& rDate )
158 43694 : { nDate = rDate.nDate; return *this; }
159 : Date& operator +=( long nDays );
160 : Date& operator -=( long nDays );
161 : Date& operator ++();
162 : Date& operator --();
163 : Date operator ++( int );
164 : Date operator --( int );
165 :
166 : TOOLS_DLLPUBLIC friend Date operator +( const Date& rDate, long nDays );
167 : TOOLS_DLLPUBLIC friend Date operator -( const Date& rDate, long nDays );
168 : TOOLS_DLLPUBLIC friend long operator -( const Date& rDate1, const Date& rDate2 );
169 :
170 : /** Obtain number of days in a month of a year.
171 :
172 : Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not
173 : normalize values.
174 : */
175 : static sal_uInt16 GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
176 :
177 : /// Internally normalizes values.
178 : static long DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
179 : /// Semantically identical to IsValidDate() member method.
180 : static bool IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
181 : /// Semantically identical to Normalize() member method.
182 : static bool Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear );
183 :
184 : };
185 :
186 : #endif
187 :
188 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|