LibreOffice Module tools (master) 1
tdate.cxx
Go to the documentation of this file.
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#include <tools/date.hxx>
20#include <sal/log.hxx>
21#include <com/sun/star/util/DateTime.hpp>
22
23#include <systemdatetime.hxx>
24#include <comphelper/date.hxx>
25
26namespace
27{
28
29const sal_Int16 kYearMax = SAL_MAX_INT16;
30const sal_Int16 kYearMin = SAL_MIN_INT16;
31
32}
33
34void Date::setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
35{
36 // don't warn about 0/0/0, commonly used as a default-value/no-value
37 SAL_WARN_IF( nYear == 0 && !(nYear == 0 && nMonth == 0 && nDay == 0),
38 "tools.datetime", "Date::setDateFromDMY - sure about 0 year? It's not in the calendar.");
39 assert( nMonth < 100 && "nMonth % 100 not representable" );
40 assert( nDay < 100 && "nDay % 100 not representable" );
41 if (nYear < 0)
42 mnDate =
43 (static_cast<sal_Int32>( nYear ) * 10000) -
44 (static_cast<sal_Int32>( nMonth % 100 ) * 100) -
45 static_cast<sal_Int32>( nDay % 100 );
46 else
47 mnDate =
48 (static_cast<sal_Int32>( nYear ) * 10000) +
49 (static_cast<sal_Int32>( nMonth % 100 ) * 100) +
50 static_cast<sal_Int32>( nDay % 100 );
51}
52
53void Date::SetDate( sal_Int32 nNewDate )
54{
55 assert( ((nNewDate / 10000) != 0) && "you don't want to set a 0 year, do you?" );
56 mnDate = nNewDate;
57}
58
59// static
60sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
61{
62 SAL_WARN_IF( nMonth < 1 || 12 < nMonth, "tools.datetime", "Date::GetDaysInMonth - nMonth out of bounds " << nMonth);
63 if (nMonth < 1)
64 nMonth = 1;
65 else if (12 < nMonth)
66 nMonth = 12;
67 return comphelper::date::getDaysInMonth( nMonth, nYear);
68}
69
71{
72 // This is a very common datum we often calculate from.
73 if (mnDate == 18991230) // 1899-12-30
74 {
75#ifndef NDEBUG
76 static sal_Int32 nDays = DateToDays( GetDay(), GetMonth(), GetYear());
77 assert(nDays == 693594);
78#endif
79 return 693594;
80 }
81 // Not calling comphelper::date::convertDateToDaysNormalizing() here just
82 // avoids a second check on null-date handling like above.
83 sal_uInt16 nDay = GetDay();
84 sal_uInt16 nMonth = GetMonth();
85 sal_Int16 nYear = GetYear();
86 comphelper::date::normalize( nDay, nMonth, nYear);
87 return comphelper::date::convertDateToDays( nDay, nMonth, nYear);
88}
89
90sal_Int32 Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
91{
92 return comphelper::date::convertDateToDaysNormalizing( nDay, nMonth, nYear);
93}
94
95static Date lcl_DaysToDate( sal_Int32 nDays )
96{
97 sal_uInt16 nDay;
98 sal_uInt16 nMonth;
99 sal_Int16 nYear;
100 comphelper::date::convertDaysToDate( nDays, nDay, nMonth, nYear);
101 return Date( nDay, nMonth, nYear);
102}
103
105{
106 if ( !GetSystemDateTime( &mnDate, nullptr ) )
107 setDateFromDMY( 1, 1, 1900 );
108}
109
110Date::Date( const css::util::DateTime& rDateTime )
111{
112 setDateFromDMY( rDateTime.Day, rDateTime.Month, rDateTime.Year );
113}
114
115void Date::SetDay( sal_uInt16 nNewDay )
116{
117 setDateFromDMY( nNewDay, GetMonth(), GetYear() );
118}
119
120void Date::SetMonth( sal_uInt16 nNewMonth )
121{
122 setDateFromDMY( GetDay(), nNewMonth, GetYear() );
123}
124
125void Date::SetYear( sal_Int16 nNewYear )
126{
127 assert( nNewYear != 0 );
128 setDateFromDMY( GetDay(), GetMonth(), nNewYear );
129}
130
131void Date::AddYears( sal_Int16 nAddYears )
132{
133 sal_Int16 nYear = GetYear();
134 if (nYear < 0)
135 {
136 if (nAddYears < 0)
137 {
138 if (nYear < kYearMin - nAddYears)
139 nYear = kYearMin;
140 else
141 nYear += nAddYears;
142 }
143 else
144 {
145 nYear += nAddYears;
146 if (nYear == 0)
147 nYear = 1;
148 }
149 }
150 else
151 {
152 if (nAddYears > 0)
153 {
154 if (kYearMax - nAddYears < nYear)
155 nYear = kYearMax;
156 else
157 nYear += nAddYears;
158 }
159 else
160 {
161 nYear += nAddYears;
162 if (nYear == 0)
163 nYear = -1;
164 }
165 }
166
167 SetYear( nYear );
168 if (GetMonth() == 2 && GetDay() == 29 && !comphelper::date::isLeapYear( nYear))
169 SetDay(28);
170}
171
172void Date::AddMonths( sal_Int32 nAddMonths )
173{
174 sal_Int32 nMonths = GetMonth() + nAddMonths;
175 sal_Int32 nNewMonth = nMonths % 12;
176 sal_Int32 nYear = GetYear() + nMonths / 12;
177 if( nMonths <= 0 || nNewMonth == 0 )
178 --nYear;
179 if( nNewMonth <= 0 )
180 nNewMonth += 12;
181 if (nYear == 0)
182 nYear = (nAddMonths < 0 ? -1 : 1);
183 else if (nYear < kYearMin)
184 nYear = kYearMin;
185 else if (nYear > kYearMax)
186 nYear = kYearMax;
187 SetMonth( static_cast<sal_uInt16>(nNewMonth) );
188 SetYear( static_cast<sal_Int16>(nYear) );
189 Normalize();
190}
191
193{
194 return static_cast<DayOfWeek>((GetAsNormalizedDays()-1) % 7);
195}
196
197sal_uInt16 Date::GetDayOfYear() const
198{
199 sal_uInt16 nDay = GetDay();
200 sal_uInt16 nMonth = GetMonth();
201 sal_Int16 nYear = GetYear();
202 Normalize( nDay, nMonth, nYear);
203
204 for( sal_uInt16 i = 1; i < nMonth; i++ )
205 nDay += comphelper::date::getDaysInMonth( i, nYear );
206 return nDay;
207}
208
209sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
210 sal_Int16 nMinimumNumberOfDaysInWeek ) const
211{
212 short nWeek;
213 short n1WDay = static_cast<short>(Date( 1, 1, GetYear() ).GetDayOfWeek());
214 short nDayOfYear = static_cast<short>(GetDayOfYear());
215
216 // weekdays start at 0, thus decrement one
217 nDayOfYear--;
218 // account for StartDay
219 n1WDay = (n1WDay+(7-static_cast<short>(eStartDay))) % 7;
220
221 if (nMinimumNumberOfDaysInWeek < 1 || 7 < nMinimumNumberOfDaysInWeek)
222 {
223 SAL_WARN( "tools.datetime", "Date::GetWeekOfYear: invalid nMinimumNumberOfDaysInWeek" );
224 nMinimumNumberOfDaysInWeek = 4;
225 }
226
227 if ( nMinimumNumberOfDaysInWeek == 1 )
228 {
229 nWeek = ((n1WDay+nDayOfYear)/7) + 1;
230 // Set to 53rd week only if we're not in the
231 // first week of the new year
232 if ( nWeek == 54 )
233 nWeek = 1;
234 else if ( nWeek == 53 )
235 {
236 short nDaysInYear = static_cast<short>(GetDaysInYear());
237 short nDaysNextYear = static_cast<short>(Date( 1, 1, GetNextYear() ).GetDayOfWeek());
238 nDaysNextYear = (nDaysNextYear+(7-static_cast<short>(eStartDay))) % 7;
239 if ( nDayOfYear > (nDaysInYear-nDaysNextYear-1) )
240 nWeek = 1;
241 }
242 }
243 else if ( nMinimumNumberOfDaysInWeek == 7 )
244 {
245 nWeek = ((n1WDay+nDayOfYear)/7);
246 // First week of a year is equal to the last week of the previous year
247 if ( nWeek == 0 )
248 {
249 Date aLastDatePrevYear( 31, 12, GetPrevYear() );
250 nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
251 }
252 }
253 else // ( nMinimumNumberOfDaysInWeek == something_else, commentary examples for 4 )
254 {
255 // x_monday - thursday
256 if ( n1WDay < nMinimumNumberOfDaysInWeek )
257 nWeek = 1;
258 // Friday
259 else if ( n1WDay == nMinimumNumberOfDaysInWeek )
260 nWeek = 53;
261 // Saturday
262 else if ( n1WDay == nMinimumNumberOfDaysInWeek + 1 )
263 {
264 // Year after leap year
265 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
266 nWeek = 53;
267 else
268 nWeek = 52;
269 }
270 // Sunday
271 else
272 nWeek = 52;
273
274 if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
275 {
276 if ( nWeek == 1 )
277 nWeek += (nDayOfYear + n1WDay) / 7;
278 else
279 nWeek = (nDayOfYear + n1WDay) / 7;
280 if ( nWeek == 53 )
281 {
282 // next x_Sunday == first x_Sunday in the new year
283 // == still the same week!
284 sal_Int32 nTempDays = GetAsNormalizedDays();
285
286 nTempDays += 6 - (GetDayOfWeek()+(7-static_cast<short>(eStartDay))) % 7;
287 nWeek = lcl_DaysToDate( nTempDays ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
288 }
289 }
290 }
291
292 return static_cast<sal_uInt16>(nWeek);
293}
294
295sal_uInt16 Date::GetDaysInMonth() const
296{
297 sal_uInt16 nDay = GetDay();
298 sal_uInt16 nMonth = GetMonth();
299 sal_Int16 nYear = GetYear();
300 Normalize( nDay, nMonth, nYear);
301
302 return comphelper::date::getDaysInMonth( nMonth, nYear );
303}
304
306{
307 sal_Int16 nYear = GetYear();
308 return comphelper::date::isLeapYear( nYear );
309}
310
312{
313 sal_uInt16 nDay = GetDay();
314 sal_uInt16 nMonth = GetMonth();
315 sal_Int16 nYear = GetYear();
316
317 if ( !nMonth || (nMonth > 12) )
318 return false;
319 if ( !nDay || (nDay > comphelper::date::getDaysInMonth( nMonth, nYear )) )
320 return false;
321 else if ( nYear <= 1582 )
322 {
323 if ( nYear < 1582 )
324 return false;
325 else if ( nMonth < 10 )
326 return false;
327 else if ( (nMonth == 10) && (nDay < 15) )
328 return false;
329 }
330
331 return true;
332}
333
335{
337}
338
339//static
340bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
341{
342 return comphelper::date::isValidDate( nDay, nMonth, nYear);
343}
344
346{
347 return IsEndOfMonth(GetDay(), GetMonth(), GetYear());
348}
349
350//static
351bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
352{
353 return comphelper::date::isValidDate(nDay, nMonth, nYear)
354 && comphelper::date::getDaysInMonth(nMonth, nYear) == nDay;
355}
356
358{
359 sal_uInt16 nDay = GetDay();
360 sal_uInt16 nMonth = GetMonth();
361 sal_Int16 nYear = GetYear();
362
363 if (!Normalize( nDay, nMonth, nYear))
364 return;
365
366 setDateFromDMY( nDay, nMonth, nYear );
367}
368
369//static
370bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear )
371{
372 return comphelper::date::normalize( rDay, rMonth, rYear);
373}
374
375void Date::AddDays( sal_Int32 nDays )
376{
377 if (nDays != 0)
378 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
379}
380
382{
383 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
384 return *this;
385}
386
388{
389 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
390 return *this;
391}
392
393Date operator +( const Date& rDate, sal_Int32 nDays )
394{
395 Date aDate( rDate );
396 aDate.AddDays( nDays );
397 return aDate;
398}
399
400Date operator -( const Date& rDate, sal_Int32 nDays )
401{
402 Date aDate( rDate );
403 aDate.AddDays( -nDays );
404 return aDate;
405}
406
407sal_Int32 operator -( const Date& rDate1, const Date& rDate2 )
408{
409 return rDate1.GetAsNormalizedDays() - rDate2.GetAsNormalizedDays();
410}
411
412/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents a date in the proleptic Gregorian calendar.
Definition: date.hxx:55
bool IsValidDate() const
If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year)
Definition: tdate.cxx:334
sal_uInt16 GetDayOfYear() const
Obtain the day of the year for the date.
Definition: tdate.cxx:197
void SetDate(sal_Int32 nNewDate)
Definition: tdate.cxx:53
Date(DateInitEmpty)
Definition: date.hxx:71
sal_uInt16 GetDaysInYear() const
Definition: date.hxx:178
sal_Int32 GetAsNormalizedDays() const
An accelerated form of DateToDays on this date.
Definition: tdate.cxx:70
sal_uInt16 GetWeekOfYear(DayOfWeek eStartDay=MONDAY, sal_Int16 nMinimumNumberOfDaysInWeek=4) const
Obtain the week of the year for a date.
Definition: tdate.cxx:209
Date & operator++()
Definition: tdate.cxx:381
void setDateFromDMY(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Definition: tdate.cxx:34
sal_Int16 GetNextYear() const
Definition: date.hxx:116
void AddDays(sal_Int32 nAddDays)
Add days skipping year 0 and truncating at limits.
Definition: tdate.cxx:375
bool IsValidAndGregorian() const
If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year) AND is...
Definition: tdate.cxx:311
sal_Int16 GetYear() const
Definition: date.hxx:113
sal_uInt16 GetDay() const
Definition: date.hxx:101
sal_Int16 GetPrevYear() const
Definition: date.hxx:117
sal_uInt16 GetDaysInMonth() const
Obtain the number of days in the month of the year of the date.
Definition: tdate.cxx:295
bool IsLeapYear() const
Definition: tdate.cxx:305
bool IsEndOfMonth() const
Definition: tdate.cxx:345
void AddYears(sal_Int16 nAddYears)
Add years skipping year 0 and truncating at limits.
Definition: tdate.cxx:131
void SetMonth(sal_uInt16 nNewMonth)
nNewMonth must be <100
Definition: tdate.cxx:120
void SetYear(sal_Int16 nNewYear)
nNewYear must be != 0
Definition: tdate.cxx:125
void Normalize()
Normalize date, invalid day or month values are adapted such that they carry over to the next month o...
Definition: tdate.cxx:357
sal_Int32 mnDate
Definition: date.hxx:57
static sal_Int32 DateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
Internally normalizes values.
Definition: tdate.cxx:90
DayOfWeek GetDayOfWeek() const
Obtain the day of the week for the date.
Definition: tdate.cxx:192
void AddMonths(sal_Int32 nAddMonths)
Add months skipping year 0 and truncating at limits.
Definition: tdate.cxx:172
void SetDay(sal_uInt16 nNewDay)
nNewDay must be <100
Definition: tdate.cxx:115
Date & operator--()
Definition: tdate.cxx:387
sal_uInt16 GetMonth() const
Definition: date.hxx:107
DateInitSystem
Definition: date.hxx:62
DayOfWeek
Definition: date.hxx:30
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
constexpr sal_uInt16 getDaysInMonth(sal_uInt16 nMonth, sal_Int16 nYear)
constexpr bool isLeapYear(sal_Int16 nYear)
constexpr sal_Int16 kYearMax
void convertDaysToDate(sal_Int32 nDays, sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
bool isValidDate(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
constexpr sal_Int16 kYearMin
constexpr sal_Int32 convertDateToDays(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
bool normalize(sal_uInt16 &rDay, sal_uInt16 &rMonth, sal_Int16 &rYear)
sal_Int32 convertDateToDaysNormalizing(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
int i
bool GetSystemDateTime(sal_Int32 *pDate, sal_Int64 *pTime)
Get current local timestamp.
static Date lcl_DaysToDate(sal_Int32 nDays)
Definition: tdate.cxx:95
Date operator-(const Date &rDate, sal_Int32 nDays)
Definition: tdate.cxx:400
Date operator+(const Date &rDate, sal_Int32 nDays)
Definition: tdate.cxx:393
#define SAL_MIN_INT16
#define SAL_MAX_INT16