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>
24 #include <comphelper/date.hxx>
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" );
43 (static_cast<sal_Int32
>( nYear
) * 10000) -
44 (static_cast<sal_Int32
>( nMonth
% 100 ) * 100) -
45 static_cast<sal_Int32
>( nDay
% 100 );
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?" );
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
);
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
76 static sal_Int32 nDays
= DateToDays( GetDay(), GetMonth(), GetYear());
77 assert(nDays
== 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
)
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();
138 if (nYear
< kYearMin
- nAddYears
)
154 if (kYearMax
- nAddYears
< nYear
)
168 if (GetMonth() == 2 && GetDay() == 29 && !comphelper::date::isLeapYear( nYear
))
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 )
182 nYear
= (nAddMonths
< 0 ? -1 : 1);
183 else if (nYear
< kYearMin
)
185 else if (nYear
> kYearMax
)
187 SetMonth( static_cast<sal_uInt16
>(nNewMonth
) );
188 SetYear( static_cast<sal_Int16
>(nYear
) );
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
);
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
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;
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
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) )
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
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
)
260 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
)
263 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
+ 1 )
265 // Year after leap year
266 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
275 if ( (nWeek
== 1) || (nDayOfYear
+ n1WDay
> 6) )
278 nWeek
+= (nDayOfYear
+ n1WDay
) / 7;
280 nWeek
= (nDayOfYear
+ n1WDay
) / 7;
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) )
320 if ( !nDay
|| (nDay
> comphelper::date::getDaysInMonth( nMonth
, nYear
)) )
322 else if ( nYear
<= 1582 )
326 else if ( nMonth
< 10 )
328 else if ( (nMonth
== 10) && (nDay
< 15) )
335 bool Date::IsValidDate() const
337 return comphelper::date::isValidDate( GetDay(), GetMonth(), GetYear());
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());
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
))
367 setDateFromDMY( nDay
, nMonth
, nYear
);
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
)
379 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays
);
382 Date
& Date::operator ++()
384 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
388 Date
& Date::operator --()
390 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
394 Date
operator +( const Date
& rDate
, sal_Int32 nDays
)
397 aDate
.AddDays( nDays
);
401 Date
operator -( const Date
& rDate
, sal_Int32 nDays
)
404 aDate
.AddDays( -nDays
);
408 sal_Int32
operator -( const Date
& rDate1
, const Date
& rDate2
)
410 return rDate1
.GetAsNormalizedDays() - rDate2
.GetAsNormalizedDays();
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */