Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / tools / source / datetime / tdate.cxx
blob719219a58afe7526f1cff8d85369d5a209598776
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 .
20 #if defined( WNT )
21 #include <windows.h>
22 #else
23 #include <time.h>
24 #endif
26 #include <tools/date.hxx>
27 #include <sal/log.hxx>
29 #ifdef MACOSX
30 extern "C" {
31 struct tm *localtime_r(const time_t *timep, struct tm *buffer);
33 #endif
35 static const sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
36 31, 31, 30, 31, 30, 31 };
38 #define MAX_DAYS 3636532
40 inline bool ImpIsLeapYear( sal_uInt16 nYear )
42 return ( ( ((nYear % 4) == 0) && ((nYear % 100) != 0) ) ||
43 ( (nYear % 400) == 0 ) );
46 // All callers must have sanitized or normalized month and year values!
47 inline sal_uInt16 ImplDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear )
49 if ( nMonth != 2 )
50 return aDaysInMonth[nMonth-1];
51 else
53 if (ImpIsLeapYear(nYear))
54 return aDaysInMonth[nMonth-1] + 1;
55 else
56 return aDaysInMonth[nMonth-1];
60 // static
61 sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear )
63 SAL_WARN_IF( nMonth < 1 || 12 < nMonth, "tools", "Date::GetDaysInMonth - nMonth out of bounds " << nMonth);
64 if (nMonth < 1)
65 nMonth = 1;
66 else if (12 < nMonth)
67 nMonth = 12;
68 return ImplDaysInMonth( nMonth, nYear);
71 long Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
73 long nDays;
75 Normalize( nDay, nMonth, nYear);
77 nDays = ((sal_uIntPtr)nYear-1) * 365;
78 nDays += ((nYear-1) / 4) - ((nYear-1) / 100) + ((nYear-1) / 400);
79 for( sal_uInt16 i = 1; i < nMonth; i++ )
80 nDays += ImplDaysInMonth(i,nYear);
81 nDays += nDay;
82 return nDays;
85 static void DaysToDate( long nDays,
86 sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear )
88 long nTempDays;
89 long i = 0;
90 bool bCalc;
94 nTempDays = (long)nDays;
95 rYear = (sal_uInt16)((nTempDays / 365) - i);
96 nTempDays -= ((sal_uIntPtr)rYear-1) * 365;
97 nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
98 bCalc = false;
99 if ( nTempDays < 1 )
101 i++;
102 bCalc = true;
104 else
106 if ( nTempDays > 365 )
108 if ( (nTempDays != 366) || !ImpIsLeapYear( rYear ) )
110 i--;
111 bCalc = true;
116 while ( bCalc );
118 rMonth = 1;
119 while ( (sal_uIntPtr)nTempDays > ImplDaysInMonth( rMonth, rYear ) )
121 nTempDays -= ImplDaysInMonth( rMonth, rYear );
122 rMonth++;
124 rDay = (sal_uInt16)nTempDays;
127 Date::Date( DateInitSystem )
129 #if defined WNT
130 SYSTEMTIME aDateTime;
131 GetLocalTime( &aDateTime );
133 // Combine to date
134 nDate = ((sal_uIntPtr)aDateTime.wDay) +
135 (((sal_uIntPtr)aDateTime.wMonth)*100) +
136 (((sal_uIntPtr)aDateTime.wYear)*10000);
137 #else
138 time_t nTmpTime;
139 struct tm aTime;
141 // get current time
142 nTmpTime = time( 0 );
144 // compute date
145 if ( localtime_r( &nTmpTime, &aTime ) )
147 nDate = ((sal_uIntPtr)aTime.tm_mday) +
148 (((sal_uIntPtr)(aTime.tm_mon+1))*100) +
149 (((sal_uIntPtr)(aTime.tm_year+1900))*10000);
151 else
152 nDate = 1 + 100 + (((sal_uIntPtr)1900)*10000);
153 #endif
156 void Date::SetDay( sal_uInt16 nNewDay )
158 sal_uIntPtr nMonth = GetMonth();
159 sal_uIntPtr nYear = GetYear();
161 nDate = ((sal_uIntPtr)(nNewDay%100)) + (nMonth*100) + (nYear*10000);
164 void Date::SetMonth( sal_uInt16 nNewMonth )
166 sal_uIntPtr nDay = GetDay();
167 sal_uIntPtr nYear = GetYear();
169 nDate = nDay + (((sal_uIntPtr)(nNewMonth%100))*100) + (nYear*10000);
172 void Date::SetYear( sal_uInt16 nNewYear )
174 sal_uIntPtr nDay = GetDay();
175 sal_uIntPtr nMonth = GetMonth();
177 nDate = nDay + (nMonth*100) + (((sal_uIntPtr)(nNewYear%10000))*10000);
180 DayOfWeek Date::GetDayOfWeek() const
182 return (DayOfWeek)((sal_uIntPtr)(DateToDays( GetDay(), GetMonth(), GetYear() )-1) % 7);
185 sal_uInt16 Date::GetDayOfYear() const
187 sal_uInt16 nDay = GetDay();
188 sal_uInt16 nMonth = GetMonth();
189 sal_uInt16 nYear = GetYear();
190 Normalize( nDay, nMonth, nYear);
192 for( sal_uInt16 i = 1; i < nMonth; i++ )
193 nDay = nDay + ::ImplDaysInMonth( i, nYear ); // += yields a warning on MSVC, so don't use it
194 return nDay;
197 sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
198 sal_Int16 nMinimumNumberOfDaysInWeek ) const
200 short nWeek;
201 short n1WDay = (short)Date( 1, 1, GetYear() ).GetDayOfWeek();
202 short nDayOfYear = (short)GetDayOfYear();
204 // weekdays start at 0, thus decrement one
205 nDayOfYear--;
206 // account for StartDay
207 n1WDay = (n1WDay+(7-(short)eStartDay)) % 7;
209 if (nMinimumNumberOfDaysInWeek < 1 || 7 < nMinimumNumberOfDaysInWeek)
211 SAL_WARN( "tools.datetime", "Date::GetWeekOfYear: invalid nMinimumNumberOfDaysInWeek" );
212 nMinimumNumberOfDaysInWeek = 4;
215 if ( nMinimumNumberOfDaysInWeek == 1 )
217 nWeek = ((n1WDay+nDayOfYear)/7) + 1;
218 // Set to 53rd week only if we're not in the
219 // first week of the new year
220 if ( nWeek == 54 )
221 nWeek = 1;
222 else if ( nWeek == 53 )
224 short nDaysInYear = (short)GetDaysInYear();
225 short nDaysNextYear = (short)Date( 1, 1, GetYear()+1 ).GetDayOfWeek();
226 nDaysNextYear = (nDaysNextYear+(7-(short)eStartDay)) % 7;
227 if ( nDayOfYear > (nDaysInYear-nDaysNextYear-1) )
228 nWeek = 1;
231 else if ( nMinimumNumberOfDaysInWeek == 7 )
233 nWeek = ((n1WDay+nDayOfYear)/7);
234 // First week of a year is equal to the last week of the previous year
235 if ( nWeek == 0 )
237 Date aLastDatePrevYear( 31, 12, GetYear()-1 );
238 nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
241 else // ( nMinimumNumberOfDaysInWeek == somehing_else, commentary examples for 4 )
243 // x_monday - thursday
244 if ( n1WDay < nMinimumNumberOfDaysInWeek )
245 nWeek = 1;
246 // Friday
247 else if ( n1WDay == nMinimumNumberOfDaysInWeek )
248 nWeek = 53;
249 // Saturday
250 else if ( n1WDay == nMinimumNumberOfDaysInWeek + 1 )
252 // Year after leapyear
253 if ( Date( 1, 1, GetYear()-1 ).IsLeapYear() )
254 nWeek = 53;
255 else
256 nWeek = 52;
258 // Sunday
259 else
260 nWeek = 52;
262 if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
264 if ( nWeek == 1 )
265 nWeek += (nDayOfYear + n1WDay) / 7;
266 else
267 nWeek = (nDayOfYear + n1WDay) / 7;
268 if ( nWeek == 53 )
270 // next x_Sunday == first x_Sunday in the new year
271 // == still the same week!
272 long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
273 nTempDays += 6 - (GetDayOfWeek()+(7-(short)eStartDay)) % 7;
274 sal_uInt16 nDay;
275 sal_uInt16 nMonth;
276 sal_uInt16 nYear;
277 DaysToDate( nTempDays, nDay, nMonth, nYear );
278 nWeek = Date( nDay, nMonth, nYear ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
283 return (sal_uInt16)nWeek;
286 sal_uInt16 Date::GetDaysInMonth() const
288 sal_uInt16 nDay = GetDay();
289 sal_uInt16 nMonth = GetMonth();
290 sal_uInt16 nYear = GetYear();
291 Normalize( nDay, nMonth, nYear);
293 return ImplDaysInMonth( nMonth, nYear );
296 bool Date::IsLeapYear() const
298 sal_uInt16 nYear = GetYear();
299 return ImpIsLeapYear( nYear );
302 bool Date::IsValidAndGregorian() const
304 sal_uInt16 nDay = GetDay();
305 sal_uInt16 nMonth = GetMonth();
306 sal_uInt16 nYear = GetYear();
308 if ( !nMonth || (nMonth > 12) )
309 return false;
310 if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
311 return false;
312 else if ( nYear <= 1582 )
314 if ( nYear < 1582 )
315 return false;
316 else if ( nMonth < 10 )
317 return false;
318 else if ( (nMonth == 10) && (nDay < 15) )
319 return false;
322 return true;
325 bool Date::IsValidDate() const
327 return IsValidDate( GetDay(), GetMonth(), GetYear());
330 //static
331 bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
333 if ( !nMonth || (nMonth > 12) )
334 return false;
335 if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
336 return false;
337 return true;
340 bool Date::Normalize()
342 sal_uInt16 nDay = GetDay();
343 sal_uInt16 nMonth = GetMonth();
344 sal_uInt16 nYear = GetYear();
346 if (!Normalize( nDay, nMonth, nYear))
347 return false;
349 SetDay( nDay);
350 SetMonth( nMonth);
351 SetYear( nYear);
353 return true;
356 //static
357 bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear )
359 if (IsValidDate( rDay, rMonth, rYear))
360 return false;
362 if (rMonth > 12)
364 rYear += rMonth / 12;
365 rMonth = rMonth % 12;
367 if (!rMonth)
369 if (!rYear)
371 rYear = 0;
372 rMonth = 1;
373 if (rDay > 31)
374 rDay -= 31;
375 else
376 rDay = 1;
378 else
380 --rYear;
381 rMonth = 12;
384 sal_uInt16 nDays;
385 while (rDay > (nDays = ImplDaysInMonth( rMonth, rYear)))
387 rDay -= nDays;
388 if (rMonth < 12)
389 ++rMonth;
390 else
392 ++rYear;
393 rMonth = 1;
396 if (rYear > 9999)
398 rDay = 31;
399 rMonth = 12;
400 rYear = 9999;
402 return true;
405 Date& Date::operator +=( long nDays )
407 sal_uInt16 nDay;
408 sal_uInt16 nMonth;
409 sal_uInt16 nYear;
410 long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
412 nTempDays += nDays;
413 if ( nTempDays > MAX_DAYS )
414 nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
415 else if ( nTempDays <= 0 )
416 nDate = 1 + 100;
417 else
419 DaysToDate( nTempDays, nDay, nMonth, nYear );
420 nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
423 return *this;
426 Date& Date::operator -=( long nDays )
428 sal_uInt16 nDay;
429 sal_uInt16 nMonth;
430 sal_uInt16 nYear;
431 long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
433 nTempDays -= nDays;
434 if ( nTempDays > MAX_DAYS )
435 nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
436 else if ( nTempDays <= 0 )
437 nDate = 1 + 100;
438 else
440 DaysToDate( nTempDays, nDay, nMonth, nYear );
441 nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
444 return *this;
447 Date& Date::operator ++()
449 sal_uInt16 nDay;
450 sal_uInt16 nMonth;
451 sal_uInt16 nYear;
452 long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
454 if ( nTempDays < MAX_DAYS )
456 nTempDays++;
457 DaysToDate( nTempDays, nDay, nMonth, nYear );
458 nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
461 return *this;
464 Date& Date::operator --()
466 sal_uInt16 nDay;
467 sal_uInt16 nMonth;
468 sal_uInt16 nYear;
469 long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
471 if ( nTempDays > 1 )
473 nTempDays--;
474 DaysToDate( nTempDays, nDay, nMonth, nYear );
475 nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
477 return *this;
480 Date Date::operator ++( int )
482 Date aOldDate = *this;
483 Date::operator++();
484 return aOldDate;
487 Date Date::operator --( int )
489 Date aOldDate = *this;
490 Date::operator--();
491 return aOldDate;
494 Date operator +( const Date& rDate, long nDays )
496 Date aDate( rDate );
497 aDate += nDays;
498 return aDate;
501 Date operator -( const Date& rDate, long nDays )
503 Date aDate( rDate );
504 aDate -= nDays;
505 return aDate;
508 long operator -( const Date& rDate1, const Date& rDate2 )
510 sal_uIntPtr nTempDays1 = Date::DateToDays( rDate1.GetDay(), rDate1.GetMonth(),
511 rDate1.GetYear() );
512 sal_uIntPtr nTempDays2 = Date::DateToDays( rDate2.GetDay(), rDate2.GetMonth(),
513 rDate2.GetYear() );
514 return nTempDays1 - nTempDays2;
517 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */