1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_TOOLS_DATE_HXX
20 #define INCLUDED_TOOLS_DATE_HXX
22 #include <tools/toolsdllapi.h>
26 #include <com/sun/star/util/Date.hpp>
28 namespace com::sun::star::util
{ struct DateTime
; }
30 enum DayOfWeek
{ MONDAY
, TUESDAY
, WEDNESDAY
, THURSDAY
, FRIDAY
,
33 /** Represents a date in the proleptic Gregorian calendar.
35 Largest representable date is 32767-12-31 = 327671231
37 Smallest representable date is -32768-01-01 = -327680101
39 Due to possible conversions to css::util::Date, which has a short
40 Year member variable, these limits are fix.
42 Year value 0 is unused. The year before year 1 CE is year 1 BCE, which is
43 the traditional proleptic Gregorian calendar.
45 This is not how ISO 8601:2000 defines things (but ISO 8601:1998 Draft
46 Revision did), but it enables class Date to be used for writing XML files
47 as XML Schema Part 2 in D.3.2 No Year Zero says
48 "The year "0000" is an illegal year value.", see
49 https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#noYearZero
50 and furthermore the note for 3.2.7 dateTime
51 https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#dateTime
54 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Date
58 void setDateFromDMY( sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
);
71 explicit Date( DateInitEmpty
) : mnDate(0) {}
72 explicit Date( DateInitSystem
);
73 explicit Date( sal_Int32 nDate
) : mnDate(nDate
) {}
74 Date( const Date
& rDate
) : mnDate(rDate
.mnDate
) {}
76 /** nDay and nMonth both must be <100, nYear must be != 0 */
77 Date( sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
)
78 { setDateFromDMY(nDay
, nMonth
, nYear
); }
80 explicit Date( const css::util::Date
& rUDate
)
82 setDateFromDMY(rUDate
.Day
, rUDate
.Month
, rUDate
.Year
);
84 explicit Date( const css::util::DateTime
& _rDateTime
);
86 bool IsEmpty() const { return mnDate
== 0; }
88 void SetDate( sal_Int32 nNewDate
);
89 sal_Int32
GetDate() const { return mnDate
; }
90 /** Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. */
91 sal_uInt32
GetDateUnsigned() const { return static_cast<sal_uInt32
>(mnDate
< 0 ? -mnDate
: mnDate
); }
92 css::util::Date
GetUNODate() const { return css::util::Date(GetDay(), GetMonth(), GetYear()); }
94 /** nNewDay must be <100 */
95 void SetDay( sal_uInt16 nNewDay
);
96 /** nNewMonth must be <100 */
97 void SetMonth( sal_uInt16 nNewMonth
);
98 /** nNewYear must be != 0 */
99 void SetYear( sal_Int16 nNewYear
);
101 sal_uInt16
GetDay() const
104 static_cast<sal_uInt16
>(-mnDate
% 100) :
105 static_cast<sal_uInt16
>( mnDate
% 100);
107 sal_uInt16
GetMonth() const
110 static_cast<sal_uInt16
>((-mnDate
/ 100) % 100) :
111 static_cast<sal_uInt16
>(( mnDate
/ 100) % 100);
113 sal_Int16
GetYear() const { return static_cast<sal_Int16
>(mnDate
/ 10000); }
114 /** Type safe access for values that are guaranteed to be unsigned, like Date::SYSTEM. */
115 sal_uInt16
GetYearUnsigned() const { return static_cast<sal_uInt16
>((mnDate
< 0 ? -mnDate
: mnDate
) / 10000); }
116 sal_Int16
GetNextYear() const { sal_Int16 nY
= GetYear(); return nY
== -1 ? 1 : nY
+ 1; }
117 sal_Int16
GetPrevYear() const { sal_Int16 nY
= GetYear(); return nY
== 1 ? -1 : nY
- 1; }
119 /** Add years skipping year 0 and truncating at limits. If the original
120 date was on Feb-29 and the resulting date is not a leap year, the
121 result is adjusted to Feb-28.
123 void AddYears( sal_Int16 nAddYears
);
125 /** Add months skipping year 0 and truncating at limits. If the original
126 date was on Feb-29 or day 31 and the resulting date is not a leap year
127 or a month with fewer days, the result is adjusted to Feb-28 or day 30.
129 void AddMonths( sal_Int32 nAddMonths
);
131 /** Add days skipping year 0 and truncating at limits.
133 void AddDays( sal_Int32 nAddDays
);
135 /** Obtain the day of the week for the date.
137 Internally normalizes a copy of values.
138 The result may be unexpected for a non-normalized invalid date like
139 Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
141 DayOfWeek
GetDayOfWeek() const;
143 /** Obtain the day of the year for the date.
145 Internally normalizes a copy of values.
146 The result may be unexpected for a non-normalized invalid date like
147 Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
149 sal_uInt16
GetDayOfYear() const;
151 /** Obtain the week of the year for a date.
153 @param nMinimumNumberOfDaysInWeek
154 How many days of a week must reside in the first week of a year.
156 Internally normalizes a copy of values.
157 The result may be unexpected for a non-normalized invalid date like
158 Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
160 sal_uInt16
GetWeekOfYear( DayOfWeek eStartDay
= MONDAY
,
161 sal_Int16 nMinimumNumberOfDaysInWeek
= 4 ) const;
163 /** Obtain the number of days in the month of the year of the date.
165 Internally normalizes a copy of values.
167 The result may be unexpected for a non-normalized invalid date like
168 Date(31,11,2000) or a sequence of aDate.SetDay(31); aDate.SetMonth(11);
170 These would result in 31 as --11-31 rolls over to --12-01 and the
171 number of days in December is returned.
173 Instead, to obtain the value for the actual set use the static method
174 Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.
176 sal_uInt16
GetDaysInMonth() const;
178 sal_uInt16
GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
179 bool IsLeapYear() const;
181 /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
182 depending on month/year) AND is of the Gregorian calendar (1582-10-15
185 bool IsValidAndGregorian() const;
187 /** If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31)
188 depending on month/year) */
189 bool IsValidDate() const;
191 // Returns true, if the date is the end of the month, false otherwise.
192 bool IsEndOfMonth() const;
194 /** Normalize date, invalid day or month values are adapted such that they
195 carry over to the next month or/and year, for example 1999-02-32
196 becomes 1999-03-04, 1999-13-01 becomes 2000-01-01, 1999-13-42 becomes
197 2000-02-11. Truncates at -32768-01-01 or 32767-12-31, 0001-00-x will
198 yield the normalized value of -0001-12-x
200 This may be necessary after Date ctors or if the SetDate(), SetDay(),
201 SetMonth(), SetYear() methods set individual non-matching values.
202 Adding/subtracting to/from dates never produces invalid dates.
206 bool IsBetween( const Date
& rFrom
, const Date
& rTo
) const
207 { return ((mnDate
>= rFrom
.mnDate
) &&
208 (mnDate
<= rTo
.mnDate
)); }
210 bool operator ==( const Date
& rDate
) const
211 { return (mnDate
== rDate
.mnDate
); }
212 bool operator !=( const Date
& rDate
) const
213 { return (mnDate
!= rDate
.mnDate
); }
214 bool operator >( const Date
& rDate
) const
215 { return (mnDate
> rDate
.mnDate
); }
216 bool operator <( const Date
& rDate
) const
217 { return (mnDate
< rDate
.mnDate
); }
218 bool operator >=( const Date
& rDate
) const
219 { return (mnDate
>= rDate
.mnDate
); }
220 bool operator <=( const Date
& rDate
) const
221 { return (mnDate
<= rDate
.mnDate
); }
223 Date
& operator =( const Date
& rDate
)
224 { mnDate
= rDate
.mnDate
; return *this; }
225 Date
& operator =( const css::util::Date
& rUDate
)
226 { setDateFromDMY( rUDate
.Day
, rUDate
.Month
, rUDate
.Year
); return *this; }
230 TOOLS_DLLPUBLIC
friend Date
operator +( const Date
& rDate
, sal_Int32 nDays
);
231 TOOLS_DLLPUBLIC
friend Date
operator -( const Date
& rDate
, sal_Int32 nDays
);
232 TOOLS_DLLPUBLIC
friend sal_Int32
operator -( const Date
& rDate1
, const Date
& rDate2
);
234 /** Obtain number of days in a month of a year.
236 Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not
239 static sal_uInt16
GetDaysInMonth( sal_uInt16 nMonth
, sal_Int16 nYear
);
241 /// Internally normalizes values.
242 static sal_Int32
DateToDays( sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
);
243 /// Semantically identical to IsValidDate() member method.
244 static bool IsValidDate( sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
);
245 /// Semantically identical to IsEndOfMonth() member method.
246 static bool IsEndOfMonth(sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
);
247 /// Semantically identical to Normalize() member method.
248 static bool Normalize( sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
);
251 /// An accelerated form of DateToDays on this date
252 sal_Int32
GetAsNormalizedDays() const;
255 TOOLS_DLLPUBLIC
std::ostream
& operator<<(std::ostream
& os
, const Date
& rDate
);
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */