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 #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
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
)
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, ...
75 // https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Usage
76 // https://en.wikipedia.org/wiki/0_(year)#History_of_astronomical_usage
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
)
88 return aDaysInMonth
[nMonth
-1];
91 if (ImpIsLeapYear(nYear
))
92 return aDaysInMonth
[nMonth
-1] + 1;
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" );
109 (static_cast<sal_Int32
>( nYear
) * 10000) -
110 (static_cast<sal_Int32
>( nMonth
% 100 ) * 100) -
111 static_cast<sal_Int32
>( nDay
% 100 );
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?" );
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
);
131 else if (12 < nMonth
)
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);
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
);
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);
174 nYear
= static_cast<sal_Int16
>((nDays
/ 365) - (i
* nSign
));
177 nTempDays
= nDays
- ImpYearToDays(nYear
);
186 if ( nTempDays
> 365 )
188 if ( (nTempDays
!= 366) || !ImpIsLeapYear( nYear
) )
198 sal_uInt16 nMonth
= 1;
199 while ( nTempDays
> ImplDaysInMonth( nMonth
, nYear
) )
201 nTempDays
-= ImplDaysInMonth( nMonth
, nYear
);
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();
242 if (nYear
< kYearMin
- nAddYears
)
258 if (kYearMax
- nAddYears
< nYear
)
272 if (GetMonth() == 2 && GetDay() == 29 && !ImpIsLeapYear( nYear
))
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 )
286 nYear
= (nAddMonths
< 0 ? -1 : 1);
287 else if (nYear
< kYearMin
)
289 else if (nYear
> kYearMax
)
291 SetMonth( static_cast<sal_uInt16
>(nNewMonth
) );
292 SetYear( static_cast<sal_Int16
>(nYear
) );
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
);
313 sal_uInt16
Date::GetWeekOfYear( DayOfWeek eStartDay
,
314 sal_Int16 nMinimumNumberOfDaysInWeek
) const
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
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
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) )
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
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
)
363 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
)
366 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
+ 1 )
368 // Year after leap year
369 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
378 if ( (nWeek
== 1) || (nDayOfYear
+ n1WDay
> 6) )
381 nWeek
+= (nDayOfYear
+ n1WDay
) / 7;
383 nWeek
= (nDayOfYear
+ n1WDay
) / 7;
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) )
423 if ( !nDay
|| (nDay
> ImplDaysInMonth( nMonth
, nYear
)) )
425 else if ( nYear
<= 1582 )
429 else if ( nMonth
< 10 )
431 else if ( (nMonth
== 10) && (nDay
< 15) )
438 bool Date::IsValidDate() const
440 return IsValidDate( GetDay(), GetMonth(), GetYear());
444 bool Date::IsValidDate( sal_uInt16 nDay
, sal_uInt16 nMonth
, sal_Int16 nYear
)
448 if ( !nMonth
|| (nMonth
> 12) )
450 if ( !nDay
|| (nDay
> ImplDaysInMonth( nMonth
, nYear
)) )
455 bool Date::IsEndOfMonth() const
457 return IsEndOfMonth(GetDay(), GetMonth(), GetYear());
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
))
475 setDateFromDMY( nDay
, nMonth
, nYear
);
479 bool Date::Normalize( sal_uInt16
& rDay
, sal_uInt16
& rMonth
, sal_Int16
& rYear
)
481 if (IsValidDate( rDay
, rMonth
, rYear
))
484 if (rDay
== 0 && rMonth
== 0 && rYear
== 0)
485 return false; // empty date
490 ; // nothing, handled below
493 // Last day of month is determined at the end.
498 rYear
+= rMonth
/ 12;
499 rMonth
= rMonth
% 12;
514 while (rDay
> (nDays
= ImplDaysInMonth( rMonth
, rYear
)))
521 if (rYear
== kYearMin
)
535 while (rDay
> (nDays
= ImplDaysInMonth( rMonth
, rYear
)))
542 if (rYear
== kYearMax
)
555 rDay
= ImplDaysInMonth( rMonth
, rYear
);
560 void Date::AddDays( sal_Int32 nDays
)
563 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays
);
566 Date
& Date::operator ++()
568 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
572 Date
& Date::operator --()
574 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
578 Date
operator +( const Date
& rDate
, sal_Int32 nDays
)
581 aDate
.AddDays( nDays
);
585 Date
operator -( const Date
& rDate
, sal_Int32 nDays
)
588 aDate
.AddDays( -nDays
);
592 sal_Int32
operator -( const Date
& rDate1
, const Date
& rDate2
)
594 return rDate1
.GetAsNormalizedDays() - rDate2
.GetAsNormalizedDays();
597 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */