bump product version to 4.1.6.2
[LibreOffice.git] / include / tools / date.hxx
blob31cf706f3a5c105b60603b822efefb2f29fc4066
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 _DATE_HXX
20 #define _DATE_HXX
22 #include "tools/toolsdllapi.h"
23 #include <tools/solar.h>
24 #include <com/sun/star/util/Date.hpp>
25 #include <sal/log.hxx>
27 class ResId;
29 enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
30 SATURDAY, SUNDAY };
32 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Date
34 private:
35 sal_uInt32 nDate;
36 void init( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
37 { nDate = ( sal_uInt32( nDay % 100 ) ) +
38 ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
39 ( ( sal_uInt32( nYear % 10000 ) ) * 10000); }
41 public:
42 enum DateInitSystem
44 SYSTEM
47 // TODO temporary until all uses are inspected and resolved
48 enum DateInitEmpty
50 EMPTY
53 Date( DateInitEmpty)
54 { nDate = 0; }
55 Date( DateInitSystem );
56 Date( const ResId & rResId );
57 Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
58 Date( const Date& rDate )
59 { nDate = rDate.nDate; }
60 Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
61 { init(nDay, nMonth, nYear); }
62 Date( const ::com::sun::star::util::Date& _rDate )
64 SAL_WARN_IF(_rDate.Year < 0, "tools.datetime", "Negative year in css::util::Date to ::Date conversion");
65 init(_rDate.Day, _rDate.Month, _rDate.Year);
68 void SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
69 sal_uInt32 GetDate() const { return nDate; }
70 ::com::sun::star::util::Date GetUNODate() const { return ::com::sun::star::util::Date(GetDay(), GetMonth(), GetYear()); }
72 void SetDay( sal_uInt16 nNewDay );
73 void SetMonth( sal_uInt16 nNewMonth );
74 void SetYear( sal_uInt16 nNewYear );
75 sal_uInt16 GetDay() const { return (sal_uInt16)(nDate % 100); }
76 sal_uInt16 GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
77 sal_uInt16 GetYear() const { return (sal_uInt16)(nDate / 10000); }
79 /** Obtain the day of the week for the date.
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);
85 DayOfWeek GetDayOfWeek() const;
87 /** Obtain the day of the year for the date.
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);
93 sal_uInt16 GetDayOfYear() const;
95 /** Obtain the week of the year for a date.
97 @param nMinimumNumberOfDaysInWeek
98 How many days of a week must reside in the first week of a year.
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);
104 sal_uInt16 GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
105 sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
107 /** Obtain the number of days in the month of the year of the date.
109 Internally normalizes a copy of values.
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);
114 These would result in 31 as --11-31 rolls over to --12-01 and the
115 number of days in December is returned.
117 Instead, to obtain the value for the actual set use the static method
118 Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.
120 sal_uInt16 GetDaysInMonth() const;
122 sal_uInt16 GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
123 sal_Bool IsLeapYear() const;
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 sal_Bool IsValidAndGregorian() const;
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;
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))
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.
145 @returns TRUE if the date was normalized, i.e. not valid before.
147 bool Normalize();
149 sal_Bool IsBetween( const Date& rFrom, const Date& rTo ) const
150 { return ((nDate >= rFrom.nDate) &&
151 (nDate <= rTo.nDate)); }
153 sal_Bool operator ==( const Date& rDate ) const
154 { return (nDate == rDate.nDate); }
155 sal_Bool operator !=( const Date& rDate ) const
156 { return (nDate != rDate.nDate); }
157 sal_Bool operator >( const Date& rDate ) const
158 { return (nDate > rDate.nDate); }
159 sal_Bool operator <( const Date& rDate ) const
160 { return (nDate < rDate.nDate); }
161 sal_Bool operator >=( const Date& rDate ) const
162 { return (nDate >= rDate.nDate); }
163 sal_Bool operator <=( const Date& rDate ) const
164 { return (nDate <= rDate.nDate); }
166 Date& operator =( const Date& rDate )
167 { 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 );
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 );
179 /** Obtain number of days in a month of a year.
181 Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not
182 normalize values.
184 static sal_uInt16 GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
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 );
195 #endif
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */