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
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;
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
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) )
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
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
)
259 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
)
262 else if ( n1WDay
== nMinimumNumberOfDaysInWeek
+ 1 )
264 // Year after leap year
265 if ( Date( 1, 1, GetPrevYear() ).IsLeapYear() )
274 if ( (nWeek
== 1) || (nDayOfYear
+ n1WDay
> 6) )
277 nWeek
+= (nDayOfYear
+ n1WDay
) / 7;
279 nWeek
= (nDayOfYear
+ n1WDay
) / 7;
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) )
319 if ( !nDay
|| (nDay
> comphelper::date::getDaysInMonth( nMonth
, nYear
)) )
321 else if ( nYear
<= 1582 )
325 else if ( nMonth
< 10 )
327 else if ( (nMonth
== 10) && (nDay
< 15) )
334 bool Date::IsValidDate() const
336 return comphelper::date::isValidDate( GetDay(), GetMonth(), GetYear());
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());
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
))
366 setDateFromDMY( nDay
, nMonth
, nYear
);
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
)
378 *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays
);
381 Date
& Date::operator ++()
383 *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
387 Date
& Date::operator --()
389 *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
393 Date
operator +( const Date
& rDate
, sal_Int32 nDays
)
396 aDate
.AddDays( nDays
);
400 Date
operator -( const Date
& rDate
, sal_Int32 nDays
)
403 aDate
.AddDays( -nDays
);
407 sal_Int32
operator -( const Date
& rDate1
, const Date
& rDate2
)
409 return rDate1
.GetAsNormalizedDays() - rDate2
.GetAsNormalizedDays();
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */