tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / tools / source / datetime / tdate.cxx
blob3548f08f06f8711be7828cc86b23ef8fd62ae9d3
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 // coverity[ tainted_data_return : FALSE ] version 2023.12.2
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 short nWeek;
228 if ( nMinimumNumberOfDaysInWeek == 1 )
230 nWeek = ((n1WDay+nDayOfYear)/7) + 1;
231 // Set to 53rd week only if we're not in the
232 // first week of the new year
233 if ( nWeek == 54 )
234 nWeek = 1;
235 else if ( nWeek == 53 )
237 short nDaysInYear = static_cast<short>(GetDaysInYear());
238 short nDaysNextYear = static_cast<short>(Date( 1, 1, GetNextYear() ).GetDayOfWeek());
239 nDaysNextYear = (nDaysNextYear+(7-static_cast<short>(eStartDay))) % 7;
240 if ( nDayOfYear > (nDaysInYear-nDaysNextYear-1) )
241 nWeek = 1;
244 else if ( nMinimumNumberOfDaysInWeek == 7 )
246 nWeek = ((n1WDay+nDayOfYear)/7);
247 // First week of a year is equal to the last week of the previous year
248 if ( nWeek == 0 )
250 Date aLastDatePrevYear( 31, 12, GetPrevYear() );
251 nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
254 else // ( nMinimumNumberOfDaysInWeek == something_else, commentary examples for 4 )
256 // x_monday - thursday
257 if ( n1WDay < nMinimumNumberOfDaysInWeek )
258 nWeek = 1;
259 // Friday
260 else if ( n1WDay == nMinimumNumberOfDaysInWeek )
261 nWeek = 53;
262 // Saturday
263 else if ( n1WDay == nMinimumNumberOfDaysInWeek + 1 )
265 // Year after leap year
266 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
267 nWeek = 53;
268 else
269 nWeek = 52;
271 // Sunday
272 else
273 nWeek = 52;
275 if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
277 if ( nWeek == 1 )
278 nWeek += (nDayOfYear + n1WDay) / 7;
279 else
280 nWeek = (nDayOfYear + n1WDay) / 7;
281 if ( nWeek == 53 )
283 // next x_Sunday == first x_Sunday in the new year
284 // == still the same week!
285 sal_Int32 nTempDays = GetAsNormalizedDays();
287 nTempDays += 6 - (GetDayOfWeek()+(7-static_cast<short>(eStartDay))) % 7;
288 nWeek = lcl_DaysToDate( nTempDays ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
293 return static_cast<sal_uInt16>(nWeek);
296 sal_uInt16 Date::GetDaysInMonth() const
298 sal_uInt16 nDay = GetDay();
299 sal_uInt16 nMonth = GetMonth();
300 sal_Int16 nYear = GetYear();
301 Normalize( nDay, nMonth, nYear);
303 return comphelper::date::getDaysInMonth( nMonth, nYear );
306 bool Date::IsLeapYear() const
308 sal_Int16 nYear = GetYear();
309 return comphelper::date::isLeapYear( nYear );
312 bool Date::IsValidAndGregorian() const
314 sal_uInt16 nDay = GetDay();
315 sal_uInt16 nMonth = GetMonth();
316 sal_Int16 nYear = GetYear();
318 if ( !nMonth || (nMonth > 12) )
319 return false;
320 if ( !nDay || (nDay > comphelper::date::getDaysInMonth( nMonth, nYear )) )
321 return false;
322 else if ( nYear <= 1582 )
324 if ( nYear < 1582 )
325 return false;
326 else if ( nMonth < 10 )
327 return false;
328 else if ( (nMonth == 10) && (nDay < 15) )
329 return false;
332 return true;
335 bool Date::IsValidDate() const
337 return comphelper::date::isValidDate( GetDay(), GetMonth(), GetYear());
340 //static
341 bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
343 return comphelper::date::isValidDate( nDay, nMonth, nYear);
346 bool Date::IsEndOfMonth() const
348 return IsEndOfMonth(GetDay(), GetMonth(), GetYear());
351 //static
352 bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
354 return comphelper::date::isValidDate(nDay, nMonth, nYear)
355 && comphelper::date::getDaysInMonth(nMonth, nYear) == nDay;
358 void Date::Normalize()
360 sal_uInt16 nDay = GetDay();
361 sal_uInt16 nMonth = GetMonth();
362 sal_Int16 nYear = GetYear();
364 if (!Normalize( nDay, nMonth, nYear))
365 return;
367 setDateFromDMY( nDay, nMonth, nYear );
370 //static
371 bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear )
373 return comphelper::date::normalize( rDay, rMonth, rYear);
376 void Date::AddDays( sal_Int32 nDays )
378 if (nDays != 0)
379 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
382 Date& Date::operator ++()
384 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
385 return *this;
388 Date& Date::operator --()
390 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
391 return *this;
394 Date operator +( const Date& rDate, sal_Int32 nDays )
396 Date aDate( rDate );
397 aDate.AddDays( nDays );
398 return aDate;
401 Date operator -( const Date& rDate, sal_Int32 nDays )
403 Date aDate( rDate );
404 aDate.AddDays( -nDays );
405 return aDate;
408 sal_Int32 operator -( const Date& rDate1, const Date& rDate2 )
410 return rDate1.GetAsNormalizedDays() - rDate2.GetAsNormalizedDays();
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */