Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / source / datetime / tdate.cxx
blobe20add43035352f298f74ea64880a810d8a573e4
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>
24 #include <comphelper/date.hxx>
26 namespace
29 const sal_Int16 kYearMax = SAL_MAX_INT16;
30 const sal_Int16 kYearMin = SAL_MIN_INT16;
34 void Date::setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
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 );
53 void Date::SetDate( sal_Int32 nNewDate )
55 assert( ((nNewDate / 10000) != 0) && "you don't want to set a 0 year, do you?" );
56 mnDate = nNewDate;
59 // static
60 sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
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);
70 sal_Int32 Date::GetAsNormalizedDays() const
72 // This is a very common datum we often calculate from.
73 if (mnDate == 18991230) // 1899-12-30
75 #ifndef NDEBUG
76 static sal_Int32 nDays = DateToDays( GetDay(), GetMonth(), GetYear());
77 assert(nDays == 693594);
78 #endif
79 return 693594;
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);
90 sal_Int32 Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
92 return comphelper::date::convertDateToDaysNormalizing( nDay, nMonth, nYear);
95 static Date lcl_DaysToDate( sal_Int32 nDays )
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);
104 Date::Date( DateInitSystem )
106 if ( !GetSystemDateTime( &mnDate, nullptr ) )
107 setDateFromDMY( 1, 1, 1900 );
110 Date::Date( const css::util::DateTime& rDateTime )
112 setDateFromDMY( rDateTime.Day, rDateTime.Month, rDateTime.Year );
115 void Date::SetDay( sal_uInt16 nNewDay )
117 setDateFromDMY( nNewDay, GetMonth(), GetYear() );
120 void Date::SetMonth( sal_uInt16 nNewMonth )
122 setDateFromDMY( GetDay(), nNewMonth, GetYear() );
125 void Date::SetYear( sal_Int16 nNewYear )
127 assert( nNewYear != 0 );
128 setDateFromDMY( GetDay(), GetMonth(), nNewYear );
131 void Date::AddYears( sal_Int16 nAddYears )
133 sal_Int16 nYear = GetYear();
134 if (nYear < 0)
136 if (nAddYears < 0)
138 if (nYear < kYearMin - nAddYears)
139 nYear = kYearMin;
140 else
141 nYear += nAddYears;
143 else
145 nYear += nAddYears;
146 if (nYear == 0)
147 nYear = 1;
150 else
152 if (nAddYears > 0)
154 if (kYearMax - nAddYears < nYear)
155 nYear = kYearMax;
156 else
157 nYear += nAddYears;
159 else
161 nYear += nAddYears;
162 if (nYear == 0)
163 nYear = -1;
167 SetYear( nYear );
168 if (GetMonth() == 2 && GetDay() == 29 && !comphelper::date::isLeapYear( nYear))
169 SetDay(28);
172 void Date::AddMonths( sal_Int32 nAddMonths )
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();
192 DayOfWeek Date::GetDayOfWeek() const
194 return static_cast<DayOfWeek>((GetAsNormalizedDays()-1) % 7);
197 sal_uInt16 Date::GetDayOfYear() const
199 sal_uInt16 nDay = GetDay();
200 sal_uInt16 nMonth = GetMonth();
201 sal_Int16 nYear = GetYear();
202 Normalize( nDay, nMonth, nYear);
204 for( sal_uInt16 i = 1; i < nMonth; i++ )
205 nDay += comphelper::date::getDaysInMonth( i, nYear );
206 return nDay;
209 sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
210 sal_Int16 nMinimumNumberOfDaysInWeek ) const
212 short nWeek;
213 short n1WDay = static_cast<short>(Date( 1, 1, GetYear() ).GetDayOfWeek());
214 short nDayOfYear = static_cast<short>(GetDayOfYear());
216 // weekdays start at 0, thus decrement one
217 nDayOfYear--;
218 // account for StartDay
219 n1WDay = (n1WDay+(7-static_cast<short>(eStartDay))) % 7;
221 if (nMinimumNumberOfDaysInWeek < 1 || 7 < nMinimumNumberOfDaysInWeek)
223 SAL_WARN( "tools.datetime", "Date::GetWeekOfYear: invalid nMinimumNumberOfDaysInWeek" );
224 nMinimumNumberOfDaysInWeek = 4;
227 if ( nMinimumNumberOfDaysInWeek == 1 )
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 )
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;
243 else if ( nMinimumNumberOfDaysInWeek == 7 )
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 )
249 Date aLastDatePrevYear( 31, 12, GetPrevYear() );
250 nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
253 else // ( nMinimumNumberOfDaysInWeek == something_else, commentary examples for 4 )
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 )
264 // Year after leap year
265 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
266 nWeek = 53;
267 else
268 nWeek = 52;
270 // Sunday
271 else
272 nWeek = 52;
274 if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
276 if ( nWeek == 1 )
277 nWeek += (nDayOfYear + n1WDay) / 7;
278 else
279 nWeek = (nDayOfYear + n1WDay) / 7;
280 if ( nWeek == 53 )
282 // next x_Sunday == first x_Sunday in the new year
283 // == still the same week!
284 sal_Int32 nTempDays = GetAsNormalizedDays();
286 nTempDays += 6 - (GetDayOfWeek()+(7-static_cast<short>(eStartDay))) % 7;
287 nWeek = lcl_DaysToDate( nTempDays ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
292 return static_cast<sal_uInt16>(nWeek);
295 sal_uInt16 Date::GetDaysInMonth() const
297 sal_uInt16 nDay = GetDay();
298 sal_uInt16 nMonth = GetMonth();
299 sal_Int16 nYear = GetYear();
300 Normalize( nDay, nMonth, nYear);
302 return comphelper::date::getDaysInMonth( nMonth, nYear );
305 bool Date::IsLeapYear() const
307 sal_Int16 nYear = GetYear();
308 return comphelper::date::isLeapYear( nYear );
311 bool Date::IsValidAndGregorian() const
313 sal_uInt16 nDay = GetDay();
314 sal_uInt16 nMonth = GetMonth();
315 sal_Int16 nYear = GetYear();
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 )
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;
331 return true;
334 bool Date::IsValidDate() const
336 return comphelper::date::isValidDate( GetDay(), GetMonth(), GetYear());
339 //static
340 bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
342 return comphelper::date::isValidDate( nDay, nMonth, nYear);
345 bool Date::IsEndOfMonth() const
347 return IsEndOfMonth(GetDay(), GetMonth(), GetYear());
350 //static
351 bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
353 return comphelper::date::isValidDate(nDay, nMonth, nYear)
354 && comphelper::date::getDaysInMonth(nMonth, nYear) == nDay;
357 void Date::Normalize()
359 sal_uInt16 nDay = GetDay();
360 sal_uInt16 nMonth = GetMonth();
361 sal_Int16 nYear = GetYear();
363 if (!Normalize( nDay, nMonth, nYear))
364 return;
366 setDateFromDMY( nDay, nMonth, nYear );
369 //static
370 bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear )
372 return comphelper::date::normalize( rDay, rMonth, rYear);
375 void Date::AddDays( sal_Int32 nDays )
377 if (nDays != 0)
378 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
381 Date& Date::operator ++()
383 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
384 return *this;
387 Date& Date::operator --()
389 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
390 return *this;
393 Date operator +( const Date& rDate, sal_Int32 nDays )
395 Date aDate( rDate );
396 aDate.AddDays( nDays );
397 return aDate;
400 Date operator -( const Date& rDate, sal_Int32 nDays )
402 Date aDate( rDate );
403 aDate.AddDays( -nDays );
404 return aDate;
407 sal_Int32 operator -( const Date& rDate1, const Date& rDate2 )
409 return rDate1.GetAsNormalizedDays() - rDate2.GetAsNormalizedDays();
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */