Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / date.hxx
blob0fc4109c42f7045ca022b32157e10c4bd2af93e4
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 INCLUDED_TOOLS_DATE_HXX
20 #define INCLUDED_TOOLS_DATE_HXX
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>
27 enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
28 SATURDAY, SUNDAY };
30 // TODO FIXME: make it handle signed year?
31 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Date
33 private:
34 sal_uInt32 nDate;
35 void init( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
36 { nDate = ( sal_uInt32( nDay % 100 ) ) +
37 ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
38 ( ( sal_uInt32( nYear % 10000 ) ) * 10000); }
40 public:
41 enum DateInitSystem
43 SYSTEM
46 // TODO temporary until all uses are inspected and resolved
47 enum DateInitEmpty
49 EMPTY
52 Date( DateInitEmpty)
53 { nDate = 0; }
54 Date( DateInitSystem );
55 Date( sal_uInt32 _nDate ) { Date::nDate = _nDate; }
56 Date( const Date& rDate )
57 { nDate = rDate.nDate; }
58 Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
59 { init(nDay, nMonth, nYear); }
60 Date( const ::com::sun::star::util::Date& _rDate )
62 SAL_WARN_IF(_rDate.Year < 0, "tools.datetime", "Negative year in css::util::Date to ::Date conversion");
63 init(_rDate.Day, _rDate.Month, _rDate.Year);
65 Date( const ::com::sun::star::util::DateTime& _rDateTime );
67 void SetDate( sal_uInt32 nNewDate ) { nDate = nNewDate; }
68 sal_uInt32 GetDate() const { return nDate; }
69 ::com::sun::star::util::Date GetUNODate() const { return ::com::sun::star::util::Date(GetDay(), GetMonth(), GetYear()); }
71 void SetDay( sal_uInt16 nNewDay );
72 void SetMonth( sal_uInt16 nNewMonth );
73 void SetYear( sal_uInt16 nNewYear );
74 sal_uInt16 GetDay() const { return (sal_uInt16)(nDate % 100); }
75 sal_uInt16 GetMonth() const { return (sal_uInt16)((nDate / 100) % 100); }
76 sal_uInt16 GetYear() const { return (sal_uInt16)(nDate / 10000); }
78 /** Obtain the day of the week for the date.
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);
84 DayOfWeek GetDayOfWeek() const;
86 /** Obtain the day of the year for the date.
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);
92 sal_uInt16 GetDayOfYear() const;
94 /** Obtain the week of the year for a date.
96 @param nMinimumNumberOfDaysInWeek
97 How many days of a week must reside in the first week of a year.
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);
103 sal_uInt16 GetWeekOfYear( DayOfWeek eStartDay = MONDAY,
104 sal_Int16 nMinimumNumberOfDaysInWeek = 4 ) const;
106 /** Obtain the number of days in the month of the year of the date.
108 Internally normalizes a copy of values.
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);
113 These would result in 31 as --11-31 rolls over to --12-01 and the
114 number of days in December is returned.
116 Instead, to obtain the value for the actual set use the static method
117 Date::GetDaysInMonth( aDate.GetMonth(), aDate.GetYear()) in such cases.
119 sal_uInt16 GetDaysInMonth() const;
121 sal_uInt16 GetDaysInYear() const { return (IsLeapYear()) ? 366 : 365; }
122 bool IsLeapYear() const;
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;
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;
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))
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.
144 @returns TRUE if the date was normalized, i.e. not valid before.
146 bool Normalize();
148 bool IsBetween( const Date& rFrom, const Date& rTo ) const
149 { return ((nDate >= rFrom.nDate) &&
150 (nDate <= rTo.nDate)); }
152 bool operator ==( const Date& rDate ) const
153 { return (nDate == rDate.nDate); }
154 bool operator !=( const Date& rDate ) const
155 { return (nDate != rDate.nDate); }
156 bool operator >( const Date& rDate ) const
157 { return (nDate > rDate.nDate); }
158 bool operator <( const Date& rDate ) const
159 { return (nDate < rDate.nDate); }
160 bool operator >=( const Date& rDate ) const
161 { return (nDate >= rDate.nDate); }
162 bool operator <=( const Date& rDate ) const
163 { return (nDate <= rDate.nDate); }
165 Date& operator =( const Date& rDate )
166 { 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 );
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 );
178 /** Obtain number of days in a month of a year.
180 Internally sanitizes nMonth to values 1 <= nMonth <= 12, does not
181 normalize values.
183 static sal_uInt16 GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
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 );
192 private:
193 /// An accelerated form of DateToDays on this date
194 long GetAsNormalizedDays() const;
197 #endif
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */