189b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
389b56da7SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
489b56da7SAndrew Rist * or more contributor license agreements. See the NOTICE file
589b56da7SAndrew Rist * distributed with this work for additional information
689b56da7SAndrew Rist * regarding copyright ownership. The ASF licenses this file
789b56da7SAndrew Rist * to you under the Apache License, Version 2.0 (the
889b56da7SAndrew Rist * "License"); you may not use this file except in compliance
989b56da7SAndrew Rist * with the License. You may obtain a copy of the License at
1089b56da7SAndrew Rist *
1189b56da7SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
1289b56da7SAndrew Rist *
1389b56da7SAndrew Rist * Unless required by applicable law or agreed to in writing,
1489b56da7SAndrew Rist * software distributed under the License is distributed on an
1589b56da7SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1689b56da7SAndrew Rist * KIND, either express or implied. See the License for the
1789b56da7SAndrew Rist * specific language governing permissions and limitations
1889b56da7SAndrew Rist * under the License.
1989b56da7SAndrew Rist *
2089b56da7SAndrew Rist *************************************************************/
2189b56da7SAndrew Rist
2289b56da7SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #if defined( OS2 )
28cdf0e10cSrcweir #define INCL_DOSDATETIME
29cdf0e10cSrcweir #include <svpm.h>
30cdf0e10cSrcweir #elif defined( WNT )
31cdf0e10cSrcweir #ifdef _MSC_VER
32cdf0e10cSrcweir #pragma warning (push,1)
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #include <tools/svwin.h>
35cdf0e10cSrcweir #ifdef _MSC_VER
36cdf0e10cSrcweir #pragma warning (pop)
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #else
39cdf0e10cSrcweir #include <time.h>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir
42cdf0e10cSrcweir #include <tools/debug.hxx>
43cdf0e10cSrcweir #include <tools/date.hxx>
44cdf0e10cSrcweir #ifdef MACOSX
45cdf0e10cSrcweir extern "C" {
46cdf0e10cSrcweir struct tm *localtime_r(const time_t *timep, struct tm *buffer);
47cdf0e10cSrcweir }
48cdf0e10cSrcweir #endif
49cdf0e10cSrcweir
50cdf0e10cSrcweir // =======================================================================
51cdf0e10cSrcweir
52cdf0e10cSrcweir static sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
53cdf0e10cSrcweir 31, 31, 30, 31, 30, 31 };
54cdf0e10cSrcweir
55cdf0e10cSrcweir #define MAX_DAYS 3636532
56cdf0e10cSrcweir
57cdf0e10cSrcweir // =======================================================================
58cdf0e10cSrcweir
ImpIsLeapYear(sal_uInt16 nYear)59cdf0e10cSrcweir inline sal_Bool ImpIsLeapYear( sal_uInt16 nYear )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir return (
62cdf0e10cSrcweir ( ((nYear % 4) == 0) && ((nYear % 100) != 0) ) ||
63cdf0e10cSrcweir ( (nYear % 400) == 0 )
64cdf0e10cSrcweir );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir
67cdf0e10cSrcweir // -----------------------------------------------------------------------
68cdf0e10cSrcweir
DaysInMonth(sal_uInt16 nMonth,sal_uInt16 nYear)69cdf0e10cSrcweir inline sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir if ( nMonth != 2 )
72cdf0e10cSrcweir return aDaysInMonth[nMonth-1];
73cdf0e10cSrcweir else
74cdf0e10cSrcweir {
75cdf0e10cSrcweir if (ImpIsLeapYear(nYear))
76cdf0e10cSrcweir return aDaysInMonth[nMonth-1] + 1;
77cdf0e10cSrcweir else
78cdf0e10cSrcweir return aDaysInMonth[nMonth-1];
79cdf0e10cSrcweir }
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir // -----------------------------------------------------------------------
83cdf0e10cSrcweir
DateToDays(sal_uInt16 nDay,sal_uInt16 nMonth,sal_uInt16 nYear)84cdf0e10cSrcweir long Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir long nDays;
87cdf0e10cSrcweir
88cdf0e10cSrcweir nDays = ((sal_uIntPtr)nYear-1) * 365;
89cdf0e10cSrcweir nDays += ((nYear-1) / 4) - ((nYear-1) / 100) + ((nYear-1) / 400);
90cdf0e10cSrcweir for( sal_uInt16 i = 1; i < nMonth; i++ )
91cdf0e10cSrcweir nDays += DaysInMonth(i,nYear);
92cdf0e10cSrcweir nDays += nDay;
93cdf0e10cSrcweir return nDays;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
96cdf0e10cSrcweir // -----------------------------------------------------------------------
97cdf0e10cSrcweir
DaysToDate(long nDays,sal_uInt16 & rDay,sal_uInt16 & rMonth,sal_uInt16 & rYear)98cdf0e10cSrcweir static void DaysToDate( long nDays,
99cdf0e10cSrcweir sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir long nTempDays;
102cdf0e10cSrcweir long i = 0;
103cdf0e10cSrcweir sal_Bool bCalc;
104cdf0e10cSrcweir
105cdf0e10cSrcweir do
106cdf0e10cSrcweir {
107cdf0e10cSrcweir nTempDays = (long)nDays;
108cdf0e10cSrcweir rYear = (sal_uInt16)((nTempDays / 365) - i);
109cdf0e10cSrcweir nTempDays -= ((sal_uIntPtr)rYear-1) * 365;
110cdf0e10cSrcweir nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400);
111cdf0e10cSrcweir bCalc = sal_False;
112cdf0e10cSrcweir if ( nTempDays < 1 )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir i++;
115cdf0e10cSrcweir bCalc = sal_True;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir else
118cdf0e10cSrcweir {
119cdf0e10cSrcweir if ( nTempDays > 365 )
120cdf0e10cSrcweir {
121cdf0e10cSrcweir if ( (nTempDays != 366) || !ImpIsLeapYear( rYear ) )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir i--;
124cdf0e10cSrcweir bCalc = sal_True;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir }
129cdf0e10cSrcweir while ( bCalc );
130cdf0e10cSrcweir
131cdf0e10cSrcweir rMonth = 1;
132cdf0e10cSrcweir while ( (sal_uIntPtr)nTempDays > DaysInMonth( rMonth, rYear ) )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir nTempDays -= DaysInMonth( rMonth, rYear );
135cdf0e10cSrcweir rMonth++;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir rDay = (sal_uInt16)nTempDays;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir
140cdf0e10cSrcweir // =======================================================================
141cdf0e10cSrcweir
Date()142cdf0e10cSrcweir Date::Date()
143cdf0e10cSrcweir {
144cdf0e10cSrcweir #if defined( OS2 )
145*27ead02aSPedro Giffuni PM_DATETIME aDateTime;
146cdf0e10cSrcweir DosGetDateTime( &aDateTime );
147cdf0e10cSrcweir
148cdf0e10cSrcweir // Datum zusammenbauen
149cdf0e10cSrcweir nDate = ((sal_uIntPtr)aDateTime.day) +
150cdf0e10cSrcweir (((sal_uIntPtr)aDateTime.month)*100) +
151cdf0e10cSrcweir (((sal_uIntPtr)aDateTime.year)*10000);
152cdf0e10cSrcweir #elif defined WNT
153cdf0e10cSrcweir SYSTEMTIME aDateTime;
154cdf0e10cSrcweir GetLocalTime( &aDateTime );
155cdf0e10cSrcweir
156cdf0e10cSrcweir // Datum zusammenbauen
157cdf0e10cSrcweir nDate = ((sal_uIntPtr)aDateTime.wDay) +
158cdf0e10cSrcweir (((sal_uIntPtr)aDateTime.wMonth)*100) +
159cdf0e10cSrcweir (((sal_uIntPtr)aDateTime.wYear)*10000);
160cdf0e10cSrcweir #else
161cdf0e10cSrcweir time_t nTmpTime;
162cdf0e10cSrcweir struct tm aTime;
163cdf0e10cSrcweir
164cdf0e10cSrcweir // Zeit ermitteln
165cdf0e10cSrcweir nTmpTime = time( 0 );
166cdf0e10cSrcweir
167cdf0e10cSrcweir // Datum zusammenbauen
168cdf0e10cSrcweir if ( localtime_r( &nTmpTime, &aTime ) )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir nDate = ((sal_uIntPtr)aTime.tm_mday) +
171cdf0e10cSrcweir (((sal_uIntPtr)(aTime.tm_mon+1))*100) +
172cdf0e10cSrcweir (((sal_uIntPtr)(aTime.tm_year+1900))*10000);
173cdf0e10cSrcweir }
174cdf0e10cSrcweir else
175cdf0e10cSrcweir nDate = 1 + 100 + (((sal_uIntPtr)1900)*10000);
176cdf0e10cSrcweir #endif
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir // -----------------------------------------------------------------------
180cdf0e10cSrcweir
SetDay(sal_uInt16 nNewDay)181cdf0e10cSrcweir void Date::SetDay( sal_uInt16 nNewDay )
182cdf0e10cSrcweir {
183cdf0e10cSrcweir sal_uIntPtr nMonth = GetMonth();
184cdf0e10cSrcweir sal_uIntPtr nYear = GetYear();
185cdf0e10cSrcweir
186cdf0e10cSrcweir nDate = ((sal_uIntPtr)(nNewDay%100)) + (nMonth*100) + (nYear*10000);
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
189cdf0e10cSrcweir // -----------------------------------------------------------------------
190cdf0e10cSrcweir
SetMonth(sal_uInt16 nNewMonth)191cdf0e10cSrcweir void Date::SetMonth( sal_uInt16 nNewMonth )
192cdf0e10cSrcweir {
193cdf0e10cSrcweir sal_uIntPtr nDay = GetDay();
194cdf0e10cSrcweir sal_uIntPtr nYear = GetYear();
195cdf0e10cSrcweir
196cdf0e10cSrcweir nDate = nDay + (((sal_uIntPtr)(nNewMonth%100))*100) + (nYear*10000);
197cdf0e10cSrcweir }
198cdf0e10cSrcweir
199cdf0e10cSrcweir // -----------------------------------------------------------------------
200cdf0e10cSrcweir
SetYear(sal_uInt16 nNewYear)201cdf0e10cSrcweir void Date::SetYear( sal_uInt16 nNewYear )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir sal_uIntPtr nDay = GetDay();
204cdf0e10cSrcweir sal_uIntPtr nMonth = GetMonth();
205cdf0e10cSrcweir
206cdf0e10cSrcweir nDate = nDay + (nMonth*100) + (((sal_uIntPtr)(nNewYear%10000))*10000);
207cdf0e10cSrcweir }
208cdf0e10cSrcweir
209cdf0e10cSrcweir // -----------------------------------------------------------------------
210cdf0e10cSrcweir
GetDayOfWeek() const211cdf0e10cSrcweir DayOfWeek Date::GetDayOfWeek() const
212cdf0e10cSrcweir {
213cdf0e10cSrcweir return (DayOfWeek)((sal_uIntPtr)(DateToDays( GetDay(), GetMonth(), GetYear() )-1) % 7);
214cdf0e10cSrcweir }
215cdf0e10cSrcweir
216cdf0e10cSrcweir // -----------------------------------------------------------------------
217cdf0e10cSrcweir
GetDayOfYear() const218cdf0e10cSrcweir sal_uInt16 Date::GetDayOfYear() const
219cdf0e10cSrcweir {
220cdf0e10cSrcweir sal_uInt16 nDay = GetDay();
221cdf0e10cSrcweir for( sal_uInt16 i = 1; i < GetMonth(); i++ )
222cdf0e10cSrcweir nDay = nDay + ::DaysInMonth( i, GetYear() ); // += yields a warning on MSVC, so don't use it
223cdf0e10cSrcweir return nDay;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir
226cdf0e10cSrcweir // -----------------------------------------------------------------------
227cdf0e10cSrcweir
GetWeekOfYear(DayOfWeek eStartDay,sal_Int16 nMinimumNumberOfDaysInWeek) const228cdf0e10cSrcweir sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
229cdf0e10cSrcweir sal_Int16 nMinimumNumberOfDaysInWeek ) const
230cdf0e10cSrcweir {
231cdf0e10cSrcweir short nWeek;
232cdf0e10cSrcweir short n1WDay = (short)Date( 1, 1, GetYear() ).GetDayOfWeek();
233cdf0e10cSrcweir short nDayOfYear = (short)GetDayOfYear();
234cdf0e10cSrcweir
235cdf0e10cSrcweir // Wochentage beginnen bei 0, deshalb einen abziehen
236cdf0e10cSrcweir nDayOfYear--;
237cdf0e10cSrcweir // StartDay beruecksichtigen
238cdf0e10cSrcweir n1WDay = (n1WDay+(7-(short)eStartDay)) % 7;
239cdf0e10cSrcweir
240cdf0e10cSrcweir if (nMinimumNumberOfDaysInWeek < 1 || 7 < nMinimumNumberOfDaysInWeek)
241cdf0e10cSrcweir {
242cdf0e10cSrcweir DBG_ERRORFILE("Date::GetWeekOfYear: invalid nMinimumNumberOfDaysInWeek");
243cdf0e10cSrcweir nMinimumNumberOfDaysInWeek = 4;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir
246cdf0e10cSrcweir if ( nMinimumNumberOfDaysInWeek == 1 )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir nWeek = ((n1WDay+nDayOfYear)/7) + 1;
249cdf0e10cSrcweir // 53te-Woche nur dann, wenn wir nicht schon in der ersten
250cdf0e10cSrcweir // Woche des neuen Jahres liegen
251cdf0e10cSrcweir if ( nWeek == 54 )
252cdf0e10cSrcweir nWeek = 1;
253cdf0e10cSrcweir else if ( nWeek == 53 )
254cdf0e10cSrcweir {
255cdf0e10cSrcweir short nDaysInYear = (short)GetDaysInYear();
256cdf0e10cSrcweir short nDaysNextYear = (short)Date( 1, 1, GetYear()+1 ).GetDayOfWeek();
257cdf0e10cSrcweir nDaysNextYear = (nDaysNextYear+(7-(short)eStartDay)) % 7;
258cdf0e10cSrcweir if ( nDayOfYear > (nDaysInYear-nDaysNextYear-1) )
259cdf0e10cSrcweir nWeek = 1;
260cdf0e10cSrcweir }
261cdf0e10cSrcweir }
262cdf0e10cSrcweir else if ( nMinimumNumberOfDaysInWeek == 7 )
263cdf0e10cSrcweir {
264cdf0e10cSrcweir nWeek = ((n1WDay+nDayOfYear)/7);
265cdf0e10cSrcweir // Erste Woche eines Jahres entspricht der letzen Woche des
266cdf0e10cSrcweir // vorherigen Jahres
267cdf0e10cSrcweir if ( nWeek == 0 )
268cdf0e10cSrcweir {
269cdf0e10cSrcweir Date aLastDatePrevYear( 31, 12, GetYear()-1 );
270cdf0e10cSrcweir nWeek = aLastDatePrevYear.GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
271cdf0e10cSrcweir }
272cdf0e10cSrcweir }
273cdf0e10cSrcweir else // ( nMinimumNumberOfDaysInWeek == somehing_else, commentary examples for 4 )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir // x_monday - thursday
276cdf0e10cSrcweir if ( n1WDay < nMinimumNumberOfDaysInWeek )
277cdf0e10cSrcweir nWeek = 1;
278cdf0e10cSrcweir // friday
279cdf0e10cSrcweir else if ( n1WDay == nMinimumNumberOfDaysInWeek )
280cdf0e10cSrcweir nWeek = 53;
281cdf0e10cSrcweir // saturday
282cdf0e10cSrcweir else if ( n1WDay == nMinimumNumberOfDaysInWeek + 1 )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir // Jahr nach Schaltjahr
285cdf0e10cSrcweir if ( Date( 1, 1, GetYear()-1 ).IsLeapYear() )
286cdf0e10cSrcweir nWeek = 53;
287cdf0e10cSrcweir else
288cdf0e10cSrcweir nWeek = 52;
289cdf0e10cSrcweir }
290cdf0e10cSrcweir // sunday
291cdf0e10cSrcweir else
292cdf0e10cSrcweir nWeek = 52;
293cdf0e10cSrcweir
294cdf0e10cSrcweir if ( (nWeek == 1) || (nDayOfYear + n1WDay > 6) )
295cdf0e10cSrcweir {
296cdf0e10cSrcweir if ( nWeek == 1 )
297cdf0e10cSrcweir nWeek += (nDayOfYear + n1WDay) / 7;
298cdf0e10cSrcweir else
299cdf0e10cSrcweir nWeek = (nDayOfYear + n1WDay) / 7;
300cdf0e10cSrcweir if ( nWeek == 53 )
301cdf0e10cSrcweir {
302cdf0e10cSrcweir // naechster x_Sonntag == erster x_Sonntag im neuen Jahr
303cdf0e10cSrcweir // == noch gleiche Woche
304cdf0e10cSrcweir long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
305cdf0e10cSrcweir nTempDays += 6 - (GetDayOfWeek()+(7-(short)eStartDay)) % 7;
306cdf0e10cSrcweir sal_uInt16 nDay;
307cdf0e10cSrcweir sal_uInt16 nMonth;
308cdf0e10cSrcweir sal_uInt16 nYear;
309cdf0e10cSrcweir DaysToDate( nTempDays, nDay, nMonth, nYear );
310cdf0e10cSrcweir nWeek = Date( nDay, nMonth, nYear ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
311cdf0e10cSrcweir }
312cdf0e10cSrcweir }
313cdf0e10cSrcweir }
314cdf0e10cSrcweir
315cdf0e10cSrcweir return (sal_uInt16)nWeek;
316cdf0e10cSrcweir }
317cdf0e10cSrcweir
318cdf0e10cSrcweir // -----------------------------------------------------------------------
319cdf0e10cSrcweir
GetDaysInMonth() const320cdf0e10cSrcweir sal_uInt16 Date::GetDaysInMonth() const
321cdf0e10cSrcweir {
322cdf0e10cSrcweir return DaysInMonth( GetMonth(), GetYear() );
323cdf0e10cSrcweir }
324cdf0e10cSrcweir
325cdf0e10cSrcweir // -----------------------------------------------------------------------
326cdf0e10cSrcweir
IsLeapYear() const327cdf0e10cSrcweir sal_Bool Date::IsLeapYear() const
328cdf0e10cSrcweir {
329cdf0e10cSrcweir sal_uInt16 nYear = GetYear();
330cdf0e10cSrcweir return ImpIsLeapYear( nYear );
331cdf0e10cSrcweir }
332cdf0e10cSrcweir
333cdf0e10cSrcweir // -----------------------------------------------------------------------
334cdf0e10cSrcweir
IsValid() const335cdf0e10cSrcweir sal_Bool Date::IsValid() const
336cdf0e10cSrcweir {
337cdf0e10cSrcweir sal_uInt16 nDay = GetDay();
338cdf0e10cSrcweir sal_uInt16 nMonth = GetMonth();
339cdf0e10cSrcweir sal_uInt16 nYear = GetYear();
340cdf0e10cSrcweir
341cdf0e10cSrcweir if ( !nMonth || (nMonth > 12) )
342cdf0e10cSrcweir return sal_False;
343cdf0e10cSrcweir if ( !nDay || (nDay > DaysInMonth( nMonth, nYear )) )
344cdf0e10cSrcweir return sal_False;
345cdf0e10cSrcweir else if ( nYear <= 1582 )
346cdf0e10cSrcweir {
347cdf0e10cSrcweir if ( nYear < 1582 )
348cdf0e10cSrcweir return sal_False;
349cdf0e10cSrcweir else if ( nMonth < 10 )
350cdf0e10cSrcweir return sal_False;
351cdf0e10cSrcweir else if ( (nMonth == 10) && (nDay < 15) )
352cdf0e10cSrcweir return sal_False;
353cdf0e10cSrcweir }
354cdf0e10cSrcweir
355cdf0e10cSrcweir return sal_True;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir
358cdf0e10cSrcweir // -----------------------------------------------------------------------
359cdf0e10cSrcweir
operator +=(long nDays)360cdf0e10cSrcweir Date& Date::operator +=( long nDays )
361cdf0e10cSrcweir {
362cdf0e10cSrcweir sal_uInt16 nDay;
363cdf0e10cSrcweir sal_uInt16 nMonth;
364cdf0e10cSrcweir sal_uInt16 nYear;
365cdf0e10cSrcweir long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
366cdf0e10cSrcweir
367cdf0e10cSrcweir nTempDays += nDays;
368cdf0e10cSrcweir if ( nTempDays > MAX_DAYS )
369cdf0e10cSrcweir nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
370cdf0e10cSrcweir else if ( nTempDays <= 0 )
371cdf0e10cSrcweir nDate = 1 + 100;
372cdf0e10cSrcweir else
373cdf0e10cSrcweir {
374cdf0e10cSrcweir DaysToDate( nTempDays, nDay, nMonth, nYear );
375cdf0e10cSrcweir nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
376cdf0e10cSrcweir }
377cdf0e10cSrcweir
378cdf0e10cSrcweir return *this;
379cdf0e10cSrcweir }
380cdf0e10cSrcweir
381cdf0e10cSrcweir // -----------------------------------------------------------------------
382cdf0e10cSrcweir
operator -=(long nDays)383cdf0e10cSrcweir Date& Date::operator -=( long nDays )
384cdf0e10cSrcweir {
385cdf0e10cSrcweir sal_uInt16 nDay;
386cdf0e10cSrcweir sal_uInt16 nMonth;
387cdf0e10cSrcweir sal_uInt16 nYear;
388cdf0e10cSrcweir long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
389cdf0e10cSrcweir
390cdf0e10cSrcweir nTempDays -= nDays;
391cdf0e10cSrcweir if ( nTempDays > MAX_DAYS )
392cdf0e10cSrcweir nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
393cdf0e10cSrcweir else if ( nTempDays <= 0 )
394cdf0e10cSrcweir nDate = 1 + 100;
395cdf0e10cSrcweir else
396cdf0e10cSrcweir {
397cdf0e10cSrcweir DaysToDate( nTempDays, nDay, nMonth, nYear );
398cdf0e10cSrcweir nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
399cdf0e10cSrcweir }
400cdf0e10cSrcweir
401cdf0e10cSrcweir return *this;
402cdf0e10cSrcweir }
403cdf0e10cSrcweir
404cdf0e10cSrcweir // -----------------------------------------------------------------------
405cdf0e10cSrcweir
operator ++()406cdf0e10cSrcweir Date& Date::operator ++()
407cdf0e10cSrcweir {
408cdf0e10cSrcweir sal_uInt16 nDay;
409cdf0e10cSrcweir sal_uInt16 nMonth;
410cdf0e10cSrcweir sal_uInt16 nYear;
411cdf0e10cSrcweir long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
412cdf0e10cSrcweir
413cdf0e10cSrcweir if ( nTempDays < MAX_DAYS )
414cdf0e10cSrcweir {
415cdf0e10cSrcweir nTempDays++;
416cdf0e10cSrcweir DaysToDate( nTempDays, nDay, nMonth, nYear );
417cdf0e10cSrcweir nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
418cdf0e10cSrcweir }
419cdf0e10cSrcweir
420cdf0e10cSrcweir return *this;
421cdf0e10cSrcweir }
422cdf0e10cSrcweir
423cdf0e10cSrcweir // -----------------------------------------------------------------------
424cdf0e10cSrcweir
operator --()425cdf0e10cSrcweir Date& Date::operator --()
426cdf0e10cSrcweir {
427cdf0e10cSrcweir sal_uInt16 nDay;
428cdf0e10cSrcweir sal_uInt16 nMonth;
429cdf0e10cSrcweir sal_uInt16 nYear;
430cdf0e10cSrcweir long nTempDays = DateToDays( GetDay(), GetMonth(), GetYear() );
431cdf0e10cSrcweir
432cdf0e10cSrcweir if ( nTempDays > 1 )
433cdf0e10cSrcweir {
434cdf0e10cSrcweir nTempDays--;
435cdf0e10cSrcweir DaysToDate( nTempDays, nDay, nMonth, nYear );
436cdf0e10cSrcweir nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
437cdf0e10cSrcweir }
438cdf0e10cSrcweir return *this;
439cdf0e10cSrcweir }
440cdf0e10cSrcweir
441cdf0e10cSrcweir #ifndef MPW33
442cdf0e10cSrcweir
443cdf0e10cSrcweir // -----------------------------------------------------------------------
444cdf0e10cSrcweir
operator ++(int)445cdf0e10cSrcweir Date Date::operator ++( int )
446cdf0e10cSrcweir {
447cdf0e10cSrcweir Date aOldDate = *this;
448cdf0e10cSrcweir Date::operator++();
449cdf0e10cSrcweir return aOldDate;
450cdf0e10cSrcweir }
451cdf0e10cSrcweir
452cdf0e10cSrcweir // -----------------------------------------------------------------------
453cdf0e10cSrcweir
operator --(int)454cdf0e10cSrcweir Date Date::operator --( int )
455cdf0e10cSrcweir {
456cdf0e10cSrcweir Date aOldDate = *this;
457cdf0e10cSrcweir Date::operator--();
458cdf0e10cSrcweir return aOldDate;
459cdf0e10cSrcweir }
460cdf0e10cSrcweir
461cdf0e10cSrcweir #endif
462cdf0e10cSrcweir
463cdf0e10cSrcweir // -----------------------------------------------------------------------
464cdf0e10cSrcweir
operator +(const Date & rDate,long nDays)465cdf0e10cSrcweir Date operator +( const Date& rDate, long nDays )
466cdf0e10cSrcweir {
467cdf0e10cSrcweir Date aDate( rDate );
468cdf0e10cSrcweir aDate += nDays;
469cdf0e10cSrcweir return aDate;
470cdf0e10cSrcweir }
471cdf0e10cSrcweir
472cdf0e10cSrcweir // -----------------------------------------------------------------------
473cdf0e10cSrcweir
operator -(const Date & rDate,long nDays)474cdf0e10cSrcweir Date operator -( const Date& rDate, long nDays )
475cdf0e10cSrcweir {
476cdf0e10cSrcweir Date aDate( rDate );
477cdf0e10cSrcweir aDate -= nDays;
478cdf0e10cSrcweir return aDate;
479cdf0e10cSrcweir }
480cdf0e10cSrcweir
481cdf0e10cSrcweir // -----------------------------------------------------------------------
482cdf0e10cSrcweir
operator -(const Date & rDate1,const Date & rDate2)483cdf0e10cSrcweir long operator -( const Date& rDate1, const Date& rDate2 )
484cdf0e10cSrcweir {
485cdf0e10cSrcweir sal_uIntPtr nTempDays1 = Date::DateToDays( rDate1.GetDay(), rDate1.GetMonth(),
486cdf0e10cSrcweir rDate1.GetYear() );
487cdf0e10cSrcweir sal_uIntPtr nTempDays2 = Date::DateToDays( rDate2.GetDay(), rDate2.GetMonth(),
488cdf0e10cSrcweir rDate2.GetYear() );
489cdf0e10cSrcweir return nTempDays1 - nTempDays2;
490cdf0e10cSrcweir }
491