]>
git.saurik.com Git - wxWidgets.git/blob - src/common/date.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDate class
5 // Originally inspired by Steve Marcus (CIS 72007,1233) 6/16/91
6 // Enhanced by Eric Simon (CIS 70540,1522) 6/29/91
7 // Further Enhanced by Chris Hill (CIS 72030,2606) 7/11/91
8 // Still Further Enhanced by Hill & Simon v3.10 8/05/91
9 // Version 4 by Charles D. Price 6/27/92
10 // Integrated into wxWindows by Julian Smart 9th July 1995
14 // Copyright: (c) Julian Smart and Markus Holzem
15 // Licence: wxWindows licence
16 /////////////////////////////////////////////////////////////////////////////
19 #pragma implementation "date.h"
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
53 static const wxChar
*dayname
[] = {
54 _T("Sunday"), _T("Monday"), _T("Tuesday"), _T("Wednesday"),
55 _T("Thursday"), _T("Friday"), _T("Saturday")
58 static const wxChar
*mname
[] = {
59 _T("January"), _T("February"), _T("March"), _T("April"), _T("May"), _T("June"),
60 _T("July"), _T("August"), _T("September"), _T("October"), _T("November"), _T("December")
63 static int GauDays
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
69 ////////////////////////////////////////////////////////////
71 ////////////////////////////////////////////////////////////
77 month
= day
= year
= day_of_week
= 0;
81 wxDate::wxDate (long j
) : julian(j
)
88 wxDate::wxDate (int m
, int d
, int y
) : month(m
), day(d
), year(y
)
95 wxDate::wxDate (const wxString
& dat
)
99 if (wxStrcmp(dat
, _T("TODAY")) == 0 || wxStrcmp(dat
, _T("today")) == 0)
101 // Sets the current date
107 wxStrcpy(buf
, WXSTRINGCAST dat
);
109 wxChar
*save_ptr
, *token
= wxStrtok(buf
,_T("/-"),&save_ptr
);
110 month
= wxAtoi(token
);
111 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T("/-"),&save_ptr
));
112 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T(" "),&save_ptr
));
118 wxDate::wxDate (const wxDate
&dt
)
120 DisplayFormat
=dt
.DisplayFormat
;
121 DisplayOptions
=dt
.DisplayOptions
;
128 void wxDate::operator = (const wxDate
&dt
)
130 DisplayFormat
=dt
.DisplayFormat
;
131 DisplayOptions
=dt
.DisplayOptions
;
135 mdy_to_julian (); // wxUSE_TIMEDATE
138 void wxDate::operator = (const wxString
& dat
)
142 if (wxStrcmp(dat
, _T("TODAY")) == 0 || wxStrcmp(dat
, _T("today")) == 0)
144 // Sets the current date
150 wxStrcpy(buf
, WXSTRINGCAST dat
);
152 wxChar
*save_ptr
, *token
= wxStrtok(buf
,_T("/-"),&save_ptr
);
153 month
= wxAtoi(token
);
154 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T("/-"),&save_ptr
));
155 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T(" "),&save_ptr
));
161 //////////////////////////////////////////////////////////////
162 // Conversion operations
163 //////////////////////////////////////////////////////////////
166 wxDate::operator wxString( void )
172 //////////////////////////////////////////////////////////////
174 //////////////////////////////////////////////////////////////
176 wxDate
wxDate::operator + (long i
)
178 wxDate
dp(julian
+ i
);
182 wxDate
wxDate::operator + (int i
)
184 wxDate
dp(julian
+ (long)i
);
188 wxDate
wxDate::operator - (long i
)
190 wxDate
dp(julian
- i
);
194 wxDate
wxDate::operator - (int i
)
196 wxDate
dp(julian
- (long)i
);
200 long wxDate::operator - (const wxDate
&dt
)
202 return ( julian
- dt
.julian
);
205 wxDate
&wxDate::operator += (long i
)
212 wxDate
&wxDate::operator -= (long i
)
219 wxDate
&wxDate::operator ++()
226 wxDate
&wxDate::operator ++(int)
233 wxDate
&wxDate::operator --()
240 wxDate
&wxDate::operator --(int)
247 //////////////////////////////////////////////////////////////
249 //////////////////////////////////////////////////////////////
251 bool WXDLLEXPORT
operator < (const wxDate
&dt1
, const wxDate
&dt2
)
253 return ( dt1
.julian
< dt2
.julian
);
256 bool WXDLLEXPORT
operator <= (const wxDate
&dt1
, const wxDate
&dt2
)
258 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
< dt2
.julian
) );
261 bool WXDLLEXPORT
operator > (const wxDate
&dt1
, const wxDate
&dt2
)
263 return ( dt1
.julian
> dt2
.julian
);
266 bool WXDLLEXPORT
operator >= (const wxDate
&dt1
, const wxDate
&dt2
)
268 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
> dt2
.julian
) );
271 bool WXDLLEXPORT
operator == (const wxDate
&dt1
, const wxDate
&dt2
)
273 return ( dt1
.julian
== dt2
.julian
);
276 bool WXDLLEXPORT
operator != (const wxDate
&dt1
, const wxDate
&dt2
)
278 return ( dt1
.julian
!= dt2
.julian
);
281 ////////////////////////////////////////////////////////////////
282 // Ostream operations
283 ////////////////////////////////////////////////////////////////
285 ostream WXDLLEXPORT
& operator << (ostream
&os
, const wxDate
&dt
)
287 return os
<< dt
.FormatDate().mb_str();
290 //////////////////////////////////////////////////////////////
291 // Conversion routines
292 //////////////////////////////////////////////////////////////
294 void wxDate::julian_to_wday (void)
296 // Correction by Peter Stadel <peters@jetcity.com>
297 day_of_week
= ((julian
- 2) % 7L);
299 day_of_week = (int) ((julian + 2) % 7 + 1);
303 void wxDate::julian_to_mdy ()
305 long a
,b
,c
,d
,e
,z
,alpha
;
307 // dealing with Gregorian calendar reform
311 alpha
= (long) ((z
-1867216.25) / 36524.25);
312 a
= z
+ 1 + alpha
- alpha
/4;
314 b
= ( a
> 1721423 ? a
+ 1524 : a
+ 1158 );
315 c
= (long) ((b
- 122.1) / 365.25);
316 d
= (long) (365.25 * c
);
317 e
= (long) ((b
- d
) / 30.6001);
318 day
= (int)(b
- d
- (long)(30.6001 * e
));
319 month
= (int)((e
< 13.5) ? e
- 1 : e
- 13);
320 year
= (int)((month
> 2.5 ) ? (c
- 4716) : c
- 4715);
324 void wxDate::mdy_to_julian (void)
327 int work_month
=month
, work_day
=day
, work_year
=year
;
328 // correct for negative year
332 { work_year
--; work_month
+=12; }
334 // deal with Gregorian calendar
335 if (work_year
*10000. + work_month
*100. + work_day
>= 15821015.)
337 a
= (int)(work_year
/100.);
340 julian
= (long) (365.25*work_year
) +
341 (long) (30.6001 * (work_month
+1)) + work_day
+ 1720994L + b
;
345 ////////////////////////////////////////////////////////////////
347 ////////////////////////////////////////////////////////////////
349 wxString
wxDate::FormatDate (int type
) const
351 int actualType
= type
;
352 if (actualType
== -1)
353 actualType
= DisplayFormat
;
357 memset( buf
, '\0', sizeof(buf
) );
358 switch ( actualType
)
361 if ( (day_of_week
< 1) || (day_of_week
> 7) )
362 wxStrcpy(buf
, _("invalid day"));
364 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
365 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
366 return wxString(buf
);
369 if ( (month
< 1) || (month
> 12) )
370 wxStrcpy(buf
, _("invalid month"));
372 wxStrncpy( buf
, wxGetTranslation(mname
[month
-1]),
373 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
374 return wxString(buf
);
377 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
380 wxStrcpy(buf
, _("invalid date"));
381 return wxString(buf
);
383 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
384 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
385 wxStrcat( buf
, _T(", "));
386 wxStrncat( buf
, wxGetTranslation(mname
[month
-1]),
387 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
388 wxStrcat( buf
, _T(" "));
389 wxSprintf( buf
+wxStrlen(buf
), _T("%d, %d"), day
, abs(year
) );
391 wxStrcat(buf
,_(" B.C."));
392 return wxString(buf
);
395 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
398 wxStrcpy(buf
, _("invalid date"));
399 return wxString(buf
);
401 wxSprintf(buf
,_T("%d "), day
);
402 wxStrncat(buf
, wxGetTranslation(mname
[month
-1]),
403 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
404 wxSprintf( buf
+wxStrlen(buf
), _T(" %d"), abs(year
) );
406 wxStrcat(buf
, _(" B.C."));
407 return wxString(buf
);
411 if (day
==0 || month
==0 || year
==0)
412 wxStrcpy(buf
, _("invalid date"));
414 wxSprintf( buf
+wxStrlen(buf
), _T("%1d/%1d/%02d"), month
, day
,
415 (DisplayOptions
& wxNO_CENTURY
) && (abs(year
) > 1899)
416 ? (abs(year
) - (abs(year
) / 100 * 100))
418 return wxString(buf
);
420 return wxString(_T(""));
423 void wxDate::SetFormat( int format
)
425 DisplayFormat
= format
;
428 int wxDate::SetOption( int option
, bool action
)
434 DisplayOptions
|= wxNO_CENTURY
;
437 DisplayOptions
&= (~wxNO_CENTURY
);
442 DisplayOptions
|= wxDATE_ABBR
;
445 DisplayOptions
&= (~wxDATE_ABBR
);
454 ///////////////////////////////////////////////////////////////
455 // Miscellaneous Routines
456 ///////////////////////////////////////////////////////////////
458 long wxDate::GetJulianDate( void ) const
463 int wxDate::GetDayOfYear( void ) const
465 wxDate
temp( 1, 1, year
);
467 return (int) (julian
- temp
.julian
+ 1);
471 bool wxDate::IsLeapYear( void ) const
473 return ( (year
>= 1582) ?
474 (year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0 ):
478 // Version 4.0 Extension to Public Interface - CDP
480 wxDate
& wxDate::Set()
484 struct _dosdate_t sDate
;
485 _dos_getdate(&sDate
);
493 time_t now
= time((time_t *) NULL
);
494 struct tm
*localTime
= localtime(&now
);
496 month
= localTime
->tm_mon
+ 1;
497 day
= localTime
->tm_mday
;
498 year
= localTime
->tm_year
+ 1900;
511 year
= nYear
< 0 ? 9999 : nYear
;
512 year
= nYear
> 9999 ? 0 : nYear
;
513 day
= nDay
< GetDaysInMonth() ? nDay
: GetDaysInMonth();
529 int wxDate::GetDaysInMonth()
531 return GauDays
[month
-1] + (month
==2 && IsLeapYear());
534 int wxDate::GetFirstDayOfMonth() const
536 return wxDate(month
, 1, year
).GetDayOfWeek();
539 int wxDate::GetDay() const
544 int wxDate::GetDayOfWeek() const
549 int wxDate::GetYear() const
554 int wxDate::GetMonth() const
559 wxDate
& wxDate::AddWeeks(int nCount
)
561 Set(julian
+ (long)nCount
*7);
565 wxDate
& wxDate::AddMonths(int nCount
)
582 wxDate
& wxDate::AddYears(int nCount
)
589 int wxDate::GetWeekOfMonth()
591 // Abs day includes the days from previous month that fills up
592 // the begin. of the week.
593 int nAbsDay
= day
+ GetFirstDayOfMonth()-1;
594 return (nAbsDay
-GetDayOfWeek())/7 + 1;
597 int wxDate::GetWeekOfYear()
599 wxDate
doTemp(1, 1, year
);
600 return (int)(((julian
- doTemp
.julian
+1)/7) + 1);
603 wxDate
wxDate::GetMonthStart()
605 return(wxDate(month
, 1, year
));
608 wxDate
wxDate::GetMonthEnd()
610 return(wxDate(month
+1, 1, year
)-1);
613 wxDate
wxDate::GetYearStart()
615 return(wxDate(1, 1, year
));
618 wxDate
wxDate::GetYearEnd()
620 return(wxDate(1, 1, year
+1)-1);
623 wxString
wxDate::GetMonthName()
625 return(FormatDate(wxMONTH
));
628 wxString
wxDate::GetDayOfWeekName()
630 return(FormatDate(wxDAY
));
633 bool wxDate::IsBetween(const wxDate
& first
, const wxDate
& second
) const
635 return (julian
>= first
.julian
&& julian
<= second
.julian
);
638 // This function is from NIHCL
639 wxDate
wxDate::Previous(int dayOfWeek
) const
641 int this_day_Of_Week
, desired_day_Of_Week
;
644 // Set the desired and current day of week to start at 0 (Monday)
645 // and end at 6 (Sunday).
647 desired_day_Of_Week
= dayOfWeek
- 1; // These functions return a value
648 this_day_Of_Week
= GetDayOfWeek() - 1; // from 1-7. Subtract 1 for 0-6.
651 // Have to determine how many days difference from current day back to
652 // desired, if any. Special calculation under the 'if' statement to
653 // effect the wraparound counting from Monday (0) back to Sunday (6).
655 if (desired_day_Of_Week
> this_day_Of_Week
)
656 this_day_Of_Week
+= 7 - desired_day_Of_Week
;
658 this_day_Of_Week
-= desired_day_Of_Week
;
659 j
-= this_day_Of_Week
; // Adjust j to set it at the desired day of week.