Bump version to 21.06.18.1
[LibreOffice.git] / tools / source / datetime / tdate.cxx
blob979611333813c1e64e50f4c6168f2b01afff6d44
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 #include <tools/date.hxx>
20 #include <sal/log.hxx>
21 #include <com/sun/star/util/DateTime.hpp>
23 #include <systemdatetime.hxx>
25 const sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
26 31, 31, 30, 31, 30, 31 };
28 // Once upon a time the number of days we internally handled was limited to
29 // MAX_DAYS 3636532. That changed with a full 16-bit year.
30 // Assuming the first valid positive date in a proleptic Gregorian calendar is
31 // 0001-01-01, this resulted in an end date of 9957-06-26.
32 // Hence we documented that years up to and including 9956 are handled.
33 /* XXX: it is unclear history why this value was chosen, the representable
34 * 9999-12-31 would be 3652060 days from 0001-01-01. Even 9998-12-31 to
35 * distinguish from a maximum possible date would be 3651695.
36 * There is connectivity/source/commontools/dbconversion.cxx that still has the
37 * same value to calculate with css::util::Date */
38 /* XXX can that dbconversion cope with years > 9999 or negative years at all?
39 * Database fields may be limited to positive 4 digits. */
41 const sal_Int32 MIN_DAYS = -11968265; // -32768-01-01
42 const sal_Int32 MAX_DAYS = 11967900; // 32767-12-31
44 namespace
47 const sal_Int16 kYearMax = SAL_MAX_INT16;
48 const sal_Int16 kYearMin = SAL_MIN_INT16;
50 // Days until start of year from zero, so month and day of month can be added.
51 // year 1 => 0 days, year 2 => 365 days, ...
52 // year -1 => -366 days, year -2 => -731 days, ...
53 sal_Int32 ImpYearToDays( sal_Int16 nYear )
55 assert( nYear != 0 );
56 sal_Int32 nOffset;
57 sal_Int32 nYr;
58 if (nYear < 0)
60 nOffset = -366;
61 nYr = nYear + 1;
63 else
65 nOffset = 0;
66 nYr = nYear - 1;
68 return nOffset + nYr*365 + nYr/4 - nYr/100 + nYr/400;
71 bool ImpIsLeapYear( sal_Int16 nYear )
73 // Leap years BCE are -1, -5, -9, ...
74 // See
75 // https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Usage
76 // https://en.wikipedia.org/wiki/0_(year)#History_of_astronomical_usage
77 assert( nYear != 0 );
78 if (nYear < 0)
79 nYear = -nYear - 1;
80 return ( ( ((nYear % 4) == 0) && ((nYear % 100) != 0) ) ||
81 ( (nYear % 400) == 0 ) );
84 // All callers must have sanitized or normalized month and year values!
85 sal_uInt16 ImplDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
87 if ( nMonth != 2 )
88 return aDaysInMonth[nMonth-1];
89 else
91 if (ImpIsLeapYear(nYear))
92 return aDaysInMonth[nMonth-1] + 1;
93 else
94 return aDaysInMonth[nMonth-1];
100 void Date::setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
102 // don't warn about 0/0/0, commonly used as a default-value/no-value
103 SAL_WARN_IF( nYear == 0 && !(nYear == 0 && nMonth == 0 && nDay == 0),
104 "tools.datetime", "Date::setDateFromDMY - sure about 0 year? It's not in the calendar.");
105 assert( nMonth < 100 && "nMonth % 100 not representable" );
106 assert( nDay < 100 && "nDay % 100 not representable" );
107 if (nYear < 0)
108 mnDate =
109 (static_cast<sal_Int32>( nYear ) * 10000) -
110 (static_cast<sal_Int32>( nMonth % 100 ) * 100) -
111 static_cast<sal_Int32>( nDay % 100 );
112 else
113 mnDate =
114 (static_cast<sal_Int32>( nYear ) * 10000) +
115 (static_cast<sal_Int32>( nMonth % 100 ) * 100) +
116 static_cast<sal_Int32>( nDay % 100 );
119 void Date::SetDate( sal_Int32 nNewDate )
121 assert( ((nNewDate / 10000) != 0) && "you don't want to set a 0 year, do you?" );
122 mnDate = nNewDate;
125 // static
126 sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
128 SAL_WARN_IF( nMonth < 1 || 12 < nMonth, "tools.datetime", "Date::GetDaysInMonth - nMonth out of bounds " << nMonth);
129 if (nMonth < 1)
130 nMonth = 1;
131 else if (12 < nMonth)
132 nMonth = 12;
133 return ImplDaysInMonth( nMonth, nYear);
136 sal_Int32 Date::GetAsNormalizedDays() const
138 // This is a very common datum we often calculate from.
139 if (mnDate == 18991230) // 1899-12-30
141 assert(DateToDays( GetDay(), GetMonth(), GetYear() ) == 693594);
142 return 693594;
144 return DateToDays( GetDay(), GetMonth(), GetYear() );
147 sal_Int32 Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
149 Normalize( nDay, nMonth, nYear);
151 sal_Int32 nDays = ImpYearToDays(nYear);
152 for( sal_uInt16 i = 1; i < nMonth; i++ )
153 nDays += ImplDaysInMonth(i,nYear);
154 nDays += nDay;
155 return nDays;
158 static Date lcl_DaysToDate( sal_Int32 nDays )
160 if ( nDays <= MIN_DAYS )
161 return Date( 1, 1, kYearMin );
162 if ( nDays >= MAX_DAYS )
163 return Date( 31, 12, kYearMax );
165 // Day 0 is -0001-12-31, day 1 is 0001-01-01
166 const sal_Int16 nSign = (nDays <= 0 ? -1 : 1);
167 sal_Int32 nTempDays;
168 sal_Int32 i = 0;
169 bool bCalc;
171 sal_Int16 nYear;
174 nYear = static_cast<sal_Int16>((nDays / 365) - (i * nSign));
175 if (nYear == 0)
176 nYear = nSign;
177 nTempDays = nDays - ImpYearToDays(nYear);
178 bCalc = false;
179 if ( nTempDays < 1 )
181 i += nSign;
182 bCalc = true;
184 else
186 if ( nTempDays > 365 )
188 if ( (nTempDays != 366) || !ImpIsLeapYear( nYear ) )
190 i -= nSign;
191 bCalc = true;
196 while ( bCalc );
198 sal_uInt16 nMonth = 1;
199 while ( nTempDays > ImplDaysInMonth( nMonth, nYear ) )
201 nTempDays -= ImplDaysInMonth( nMonth, nYear );
202 ++nMonth;
205 return Date( static_cast<sal_uInt16>(nTempDays), nMonth, nYear );
208 Date::Date( DateInitSystem )
210 if ( !GetSystemDateTime( &mnDate, nullptr ) )
211 setDateFromDMY( 1, 1, 1900 );
214 Date::Date( const css::util::DateTime& rDateTime )
216 setDateFromDMY( rDateTime.Day, rDateTime.Month, rDateTime.Year );
219 void Date::SetDay( sal_uInt16 nNewDay )
221 setDateFromDMY( nNewDay, GetMonth(), GetYear() );
224 void Date::SetMonth( sal_uInt16 nNewMonth )
226 setDateFromDMY( GetDay(), nNewMonth, GetYear() );
229 void Date::SetYear( sal_Int16 nNewYear )
231 assert( nNewYear != 0 );
232 setDateFromDMY( GetDay(), GetMonth(), nNewYear );
235 void Date::AddYears( sal_Int16 nAddYears )
237 sal_Int16 nYear = GetYear();
238 if (nYear < 0)
240 if (nAddYears < 0)
242 if (nYear < kYearMin - nAddYears)
243 nYear = kYearMin;
244 else
245 nYear += nAddYears;
247 else
249 nYear += nAddYears;
250 if (nYear == 0)
251 nYear = 1;
254 else
256 if (nAddYears > 0)
258 if (kYearMax - nAddYears < nYear)
259 nYear = kYearMax;
260 else
261 nYear += nAddYears;
263 else
265 nYear += nAddYears;
266 if (nYear == 0)
267 nYear = -1;
271 SetYear( nYear );
272 if (GetMonth() == 2 && GetDay() == 29 && !ImpIsLeapYear( nYear))
273 SetDay(28);
276 void Date::AddMonths( sal_Int32 nAddMonths )
278 sal_Int32 nMonths = GetMonth() + nAddMonths;
279 sal_Int32 nNewMonth = nMonths % 12;
280 sal_Int32 nYear = GetYear() + nMonths / 12;
281 if( nMonths <= 0 || nNewMonth == 0 )
282 --nYear;
283 if( nNewMonth <= 0 )
284 nNewMonth += 12;
285 if (nYear == 0)
286 nYear = (nAddMonths < 0 ? -1 : 1);
287 else if (nYear < kYearMin)
288 nYear = kYearMin;
289 else if (nYear > kYearMax)
290 nYear = kYearMax;
291 SetMonth( static_cast<sal_uInt16>(nNewMonth) );
292 SetYear( static_cast<sal_Int16>(nYear) );
293 Normalize();
296 DayOfWeek Date::GetDayOfWeek() const
298 return static_cast<DayOfWeek>((GetAsNormalizedDays()-1) % 7);
301 sal_uInt16 Date::GetDayOfYear() const
303 sal_uInt16 nDay = GetDay();
304 sal_uInt16 nMonth = GetMonth();
305 sal_Int16 nYear = GetYear();
306 Normalize( nDay, nMonth, nYear);
308 for( sal_uInt16 i = 1; i < nMonth; i++ )
309 nDay += ::ImplDaysInMonth( i, nYear );
310 return nDay;
313 sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
314 sal_Int16 nMinimumNumberOfDaysInWeek ) const
316 short nWeek;
317 short n1WDay = static_cast<short>(Date( 1, 1, GetYear() ).GetDayOfWeek());
318 short nDayOfYear = static_cast<short>(GetDayOfYear());
320 // weekdays start at 0, thus decrement one
321 nDayOfYear--;
322 // account for StartDay
323 n1WDay = (n1WDay+(7-static_cast<short>(eStartDay))) % 7;
325 if (nMinimumNumberOfDaysInWeek < 1 || 7 < nMinimumNumberOfDaysInWeek)
327 SAL_WARN( "tools.datetime", "Date::GetWeekOfYear: invalid nMinimumNumberOfDaysInWeek" );
328 nMinimumNumberOfDaysInWeek = 4;
331 if ( nMinimumNumberOfDaysInWeek == 1 )
333 nWeek = ((n1WDay+nDayOfYear)/7) + 1;
334 // Set to 53rd week only if we're not in the
335 // first week of the new year
336 if ( nWeek == 54 )
337 nWeek = 1;
338 else if ( nWeek == 53 )
340 short nDaysInYear = static_cast<short>(GetDaysInYear());
341 short nDaysNextYear = static_cast<short>(Date( 1, 1, GetNextYear() ).GetDayOfWeek());
342 nDaysNextYear = (nDaysNextYear+(7-static_cast<short>(eStartDay))) % 7;
343 if ( nDayOfYear > (nDaysInYear-nDaysNextYear-1) )
344 nWeek = 1;
347 else if ( nMinimumNumberOfDaysInWeek == 7 )
349 nWeek = ((n1WDay+nDayOfYear)/7);
350 // First week of a year is equal to the last week of the previous year
351 if ( nWeek == 0 )
353 Date aLastDatePrevYear( 31, 12, GetPrevYear() );
354 nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
357 else // ( nMinimumNumberOfDaysInWeek == something_else, commentary examples for 4 )
359 // x_monday - thursday
360 if ( n1WDay < nMinimumNumberOfDaysInWeek )
361 nWeek = 1;
362 // Friday
363 else if ( n1WDay == nMinimumNumberOfDaysInWeek )
364 nWeek = 53;
365 // Saturday
366 else if ( n1WDay == nMinimumNumberOfDaysInWeek + 1 )
368 // Year after leap year
369 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
370 nWeek = 53;
371 else
372 nWeek = 52;
374 // Sunday
375 else
376 nWeek = 52;
378 if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
380 if ( nWeek == 1 )
381 nWeek += (nDayOfYear + n1WDay) / 7;
382 else
383 nWeek = (nDayOfYear + n1WDay) / 7;
384 if ( nWeek == 53 )
386 // next x_Sunday == first x_Sunday in the new year
387 // == still the same week!
388 sal_Int32 nTempDays = GetAsNormalizedDays();
390 nTempDays += 6 - (GetDayOfWeek()+(7-static_cast<short>(eStartDay))) % 7;
391 nWeek = lcl_DaysToDate( nTempDays ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
396 return static_cast<sal_uInt16>(nWeek);
399 sal_uInt16 Date::GetDaysInMonth() const
401 sal_uInt16 nDay = GetDay();
402 sal_uInt16 nMonth = GetMonth();
403 sal_Int16 nYear = GetYear();
404 Normalize( nDay, nMonth, nYear);
406 return ImplDaysInMonth( nMonth, nYear );
409 bool Date::IsLeapYear() const
411 sal_Int16 nYear = GetYear();
412 return ImpIsLeapYear( nYear );
415 bool Date::IsValidAndGregorian() const
417 sal_uInt16 nDay = GetDay();
418 sal_uInt16 nMonth = GetMonth();
419 sal_Int16 nYear = GetYear();
421 if ( !nMonth || (nMonth > 12) )
422 return false;
423 if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
424 return false;
425 else if ( nYear <= 1582 )
427 if ( nYear < 1582 )
428 return false;
429 else if ( nMonth < 10 )
430 return false;
431 else if ( (nMonth == 10) && (nDay < 15) )
432 return false;
435 return true;
438 bool Date::IsValidDate() const
440 return IsValidDate( GetDay(), GetMonth(), GetYear());
443 //static
444 bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
446 if (nYear == 0)
447 return false;
448 if ( !nMonth || (nMonth > 12) )
449 return false;
450 if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
451 return false;
452 return true;
455 bool Date::IsEndOfMonth() const
457 return IsEndOfMonth(GetDay(), GetMonth(), GetYear());
460 //static
461 bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
463 return IsValidDate(nDay, nMonth, nYear) && ImplDaysInMonth(nMonth, nYear) == nDay;
466 void Date::Normalize()
468 sal_uInt16 nDay = GetDay();
469 sal_uInt16 nMonth = GetMonth();
470 sal_Int16 nYear = GetYear();
472 if (!Normalize( nDay, nMonth, nYear))
473 return;
475 setDateFromDMY( nDay, nMonth, nYear );
478 //static
479 bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear )
481 if (IsValidDate( rDay, rMonth, rYear))
482 return false;
484 if (rDay == 0 && rMonth == 0 && rYear == 0)
485 return false; // empty date
487 if (rDay == 0)
489 if (rMonth == 0)
490 ; // nothing, handled below
491 else
492 --rMonth;
493 // Last day of month is determined at the end.
496 if (rMonth > 12)
498 rYear += rMonth / 12;
499 rMonth = rMonth % 12;
500 if (rYear == 0)
501 rYear = 1;
503 if (rMonth == 0)
505 --rYear;
506 if (rYear == 0)
507 rYear = -1;
508 rMonth = 12;
511 if (rYear < 0)
513 sal_uInt16 nDays;
514 while (rDay > (nDays = ImplDaysInMonth( rMonth, rYear)))
516 rDay -= nDays;
517 if (rMonth > 1)
518 --rMonth;
519 else
521 if (rYear == kYearMin)
523 rDay = 1;
524 rMonth = 1;
525 return true;
527 --rYear;
528 rMonth = 12;
532 else
534 sal_uInt16 nDays;
535 while (rDay > (nDays = ImplDaysInMonth( rMonth, rYear)))
537 rDay -= nDays;
538 if (rMonth < 12)
539 ++rMonth;
540 else
542 if (rYear == kYearMax)
544 rDay = 31;
545 rMonth = 12;
546 return true;
548 ++rYear;
549 rMonth = 1;
554 if (rDay == 0)
555 rDay = ImplDaysInMonth( rMonth, rYear);
557 return true;
560 void Date::AddDays( sal_Int32 nDays )
562 if (nDays != 0)
563 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
566 Date& Date::operator ++()
568 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
569 return *this;
572 Date& Date::operator --()
574 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
575 return *this;
578 Date operator +( const Date& rDate, sal_Int32 nDays )
580 Date aDate( rDate );
581 aDate.AddDays( nDays );
582 return aDate;
585 Date operator -( const Date& rDate, sal_Int32 nDays )
587 Date aDate( rDate );
588 aDate.AddDays( -nDays );
589 return aDate;
592 sal_Int32 operator -( const Date& rDate1, const Date& rDate2 )
594 return rDate1.GetAsNormalizedDays() - rDate2.GetAsNormalizedDays();
597 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */