]>
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"
51 static const wxChar
*dayname
[] = {
52 _T("Sunday"), _T("Monday"), _T("Tuesday"), _T("Wednesday"),
53 _T("Thursday"), _T("Friday"), _T("Saturday")
56 static const wxChar
*mname
[] = {
57 _T("January"), _T("February"), _T("March"), _T("April"), _T("May"), _T("June"),
58 _T("July"), _T("August"), _T("September"), _T("October"), _T("November"), _T("December")
61 static int GauDays
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
63 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
67 ////////////////////////////////////////////////////////////
69 ////////////////////////////////////////////////////////////
75 month
= day
= year
= day_of_week
= 0;
79 wxDate::wxDate (long j
) : julian(j
)
86 wxDate::wxDate (int m
, int d
, int y
) : month(m
), day(d
), year(y
)
93 wxDate::wxDate (const wxString
& dat
)
97 if (wxStrcmp(dat
, _T("TODAY")) == 0 || wxStrcmp(dat
, _T("today")) == 0)
99 // Sets the current date
105 wxStrcpy(buf
, WXSTRINGCAST dat
);
107 wxChar
*save_ptr
, *token
= wxStrtok(buf
,_T("/-"),&save_ptr
);
108 month
= wxAtoi(token
);
109 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T("/-"),&save_ptr
));
110 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T(" "),&save_ptr
));
116 wxDate::wxDate (const wxDate
&dt
)
118 DisplayFormat
=dt
.DisplayFormat
;
119 DisplayOptions
=dt
.DisplayOptions
;
126 void wxDate::operator = (const wxDate
&dt
)
128 DisplayFormat
=dt
.DisplayFormat
;
129 DisplayOptions
=dt
.DisplayOptions
;
133 mdy_to_julian (); // wxUSE_TIMEDATE
136 void wxDate::operator = (const wxString
& dat
)
140 if (wxStrcmp(dat
, _T("TODAY")) == 0 || wxStrcmp(dat
, _T("today")) == 0)
142 // Sets the current date
148 wxStrcpy(buf
, WXSTRINGCAST dat
);
150 wxChar
*save_ptr
, *token
= wxStrtok(buf
,_T("/-"),&save_ptr
);
151 month
= wxAtoi(token
);
152 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T("/-"),&save_ptr
));
153 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,_T(" "),&save_ptr
));
159 //////////////////////////////////////////////////////////////
160 // Conversion operations
161 //////////////////////////////////////////////////////////////
164 wxDate::operator wxString( void )
170 //////////////////////////////////////////////////////////////
172 //////////////////////////////////////////////////////////////
174 wxDate
wxDate::operator + (long i
)
176 wxDate
dp(julian
+ i
);
180 wxDate
wxDate::operator + (int i
)
182 wxDate
dp(julian
+ (long)i
);
186 wxDate
wxDate::operator - (long i
)
188 wxDate
dp(julian
- i
);
192 wxDate
wxDate::operator - (int i
)
194 wxDate
dp(julian
- (long)i
);
198 long wxDate::operator - (const wxDate
&dt
)
200 return ( julian
- dt
.julian
);
203 wxDate
&wxDate::operator += (long i
)
210 wxDate
&wxDate::operator -= (long i
)
217 wxDate
&wxDate::operator ++()
224 wxDate
&wxDate::operator ++(int)
231 wxDate
&wxDate::operator --()
238 wxDate
&wxDate::operator --(int)
245 //////////////////////////////////////////////////////////////
247 //////////////////////////////////////////////////////////////
249 bool WXDLLEXPORT
operator < (const wxDate
&dt1
, const wxDate
&dt2
)
251 return ( dt1
.julian
< dt2
.julian
);
254 bool WXDLLEXPORT
operator <= (const wxDate
&dt1
, const wxDate
&dt2
)
256 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
< dt2
.julian
) );
259 bool WXDLLEXPORT
operator > (const wxDate
&dt1
, const wxDate
&dt2
)
261 return ( dt1
.julian
> dt2
.julian
);
264 bool WXDLLEXPORT
operator >= (const wxDate
&dt1
, const wxDate
&dt2
)
266 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
> dt2
.julian
) );
269 bool WXDLLEXPORT
operator == (const wxDate
&dt1
, const wxDate
&dt2
)
271 return ( dt1
.julian
== dt2
.julian
);
274 bool WXDLLEXPORT
operator != (const wxDate
&dt1
, const wxDate
&dt2
)
276 return ( dt1
.julian
!= dt2
.julian
);
279 ////////////////////////////////////////////////////////////////
280 // Ostream operations
281 ////////////////////////////////////////////////////////////////
283 ostream WXDLLEXPORT
& operator << (ostream
&os
, const wxDate
&dt
)
285 return os
<< dt
.FormatDate().mb_str();
288 //////////////////////////////////////////////////////////////
289 // Conversion routines
290 //////////////////////////////////////////////////////////////
292 void wxDate::julian_to_wday (void)
294 day_of_week
= (int) ((julian
+ 2) % 7 + 1);
297 void wxDate::julian_to_mdy ()
299 long a
,b
,c
,d
,e
,z
,alpha
;
301 // dealing with Gregorian calendar reform
305 alpha
= (long) ((z
-1867216.25) / 36524.25);
306 a
= z
+ 1 + alpha
- alpha
/4;
308 b
= ( a
> 1721423 ? a
+ 1524 : a
+ 1158 );
309 c
= (long) ((b
- 122.1) / 365.25);
310 d
= (long) (365.25 * c
);
311 e
= (long) ((b
- d
) / 30.6001);
312 day
= (int)(b
- d
- (long)(30.6001 * e
));
313 month
= (int)((e
< 13.5) ? e
- 1 : e
- 13);
314 year
= (int)((month
> 2.5 ) ? (c
- 4716) : c
- 4715);
318 void wxDate::mdy_to_julian (void)
321 int work_month
=month
, work_day
=day
, work_year
=year
;
322 // correct for negative year
326 { work_year
--; work_month
+=12; }
328 // deal with Gregorian calendar
329 if (work_year
*10000. + work_month
*100. + work_day
>= 15821015.)
331 a
= (int)(work_year
/100.);
334 julian
= (long) (365.25*work_year
) +
335 (long) (30.6001 * (work_month
+1)) + work_day
+ 1720994L + b
;
339 ////////////////////////////////////////////////////////////////
341 ////////////////////////////////////////////////////////////////
343 wxString
wxDate::FormatDate (int type
) const
345 int actualType
= type
;
346 if (actualType
== -1)
347 actualType
= DisplayFormat
;
351 memset( buf
, '\0', sizeof(buf
) );
352 switch ( actualType
)
355 if ( (day_of_week
< 1) || (day_of_week
> 7) )
356 wxStrcpy(buf
, _("invalid day"));
358 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
359 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
360 return wxString(buf
);
363 if ( (month
< 1) || (month
> 12) )
364 wxStrcpy(buf
, _("invalid month"));
366 wxStrncpy( buf
, wxGetTranslation(mname
[month
-1]),
367 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
368 return wxString(buf
);
371 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
374 wxStrcpy(buf
, _("invalid date"));
375 return wxString(buf
);
377 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
378 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
379 wxStrcat( buf
, _T(", "));
380 wxStrncat( buf
, wxGetTranslation(mname
[month
-1]),
381 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
382 wxStrcat( buf
, _T(" "));
383 wxSprintf( buf
+wxStrlen(buf
), _T("%d, %d"), day
, abs(year
) );
385 wxStrcat(buf
,_(" B.C."));
386 return wxString(buf
);
389 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
392 wxStrcpy(buf
, _("invalid date"));
393 return wxString(buf
);
395 wxSprintf(buf
,_T("%d "), day
);
396 wxStrncat(buf
, wxGetTranslation(mname
[month
-1]),
397 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
398 wxSprintf( buf
+wxStrlen(buf
), _T(" %d"), abs(year
) );
400 wxStrcat(buf
, _(" B.C."));
401 return wxString(buf
);
405 if (day
==0 || month
==0 || year
==0)
406 wxStrcpy(buf
, _("invalid date"));
408 wxSprintf( buf
+wxStrlen(buf
), _T("%1d/%1d/%02d"), month
, day
,
409 (DisplayOptions
& wxNO_CENTURY
) && (abs(year
) > 1899)
410 ? (abs(year
) - (abs(year
) / 100 * 100))
412 return wxString(buf
);
414 return wxString(_T(""));
417 void wxDate::SetFormat( int format
)
419 DisplayFormat
= format
;
422 int wxDate::SetOption( int option
, bool action
)
428 DisplayOptions
|= wxNO_CENTURY
;
431 DisplayOptions
&= (~wxNO_CENTURY
);
436 DisplayOptions
|= wxDATE_ABBR
;
439 DisplayOptions
&= (~wxDATE_ABBR
);
448 ///////////////////////////////////////////////////////////////
449 // Miscellaneous Routines
450 ///////////////////////////////////////////////////////////////
452 long wxDate::GetJulianDate( void ) const
457 int wxDate::GetDayOfYear( void ) const
459 wxDate
temp( 1, 1, year
);
461 return (int) (julian
- temp
.julian
+ 1);
465 bool wxDate::IsLeapYear( void ) const
467 return ( (year
>= 1582) ?
468 (year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0 ):
472 // Version 4.0 Extension to Public Interface - CDP
474 wxDate
& wxDate::Set()
478 struct _dosdate_t sDate
;
479 _dos_getdate(&sDate
);
487 time_t now
= time((time_t *) NULL
);
488 struct tm
*localTime
= localtime(&now
);
490 month
= localTime
->tm_mon
+ 1;
491 day
= localTime
->tm_mday
;
492 year
= localTime
->tm_year
+ 1900;
505 year
= nYear
< 0 ? 9999 : nYear
;
506 year
= nYear
> 9999 ? 0 : nYear
;
507 day
= nDay
< GetDaysInMonth() ? nDay
: GetDaysInMonth();
523 int wxDate::GetDaysInMonth()
525 return GauDays
[month
-1] + (month
==2 && IsLeapYear());
528 int wxDate::GetFirstDayOfMonth() const
530 return wxDate(month
, 1, year
).GetDayOfWeek();
533 int wxDate::GetDay() const
538 int wxDate::GetDayOfWeek() const
543 int wxDate::GetYear() const
548 int wxDate::GetMonth() const
553 wxDate
& wxDate::AddWeeks(int nCount
)
555 Set(julian
+ (long)nCount
*7);
559 wxDate
& wxDate::AddMonths(int nCount
)
576 wxDate
& wxDate::AddYears(int nCount
)
583 int wxDate::GetWeekOfMonth()
585 // Abs day includes the days from previous month that fills up
586 // the begin. of the week.
587 int nAbsDay
= day
+ GetFirstDayOfMonth()-1;
588 return (nAbsDay
-GetDayOfWeek())/7 + 1;
591 int wxDate::GetWeekOfYear()
593 wxDate
doTemp(1, 1, year
);
594 return (int)(((julian
- doTemp
.julian
+1)/7) + 1);
597 wxDate
wxDate::GetMonthStart()
599 return(wxDate(month
, 1, year
));
602 wxDate
wxDate::GetMonthEnd()
604 return(wxDate(month
+1, 1, year
)-1);
607 wxDate
wxDate::GetYearStart()
609 return(wxDate(1, 1, year
));
612 wxDate
wxDate::GetYearEnd()
614 return(wxDate(1, 1, year
+1)-1);
617 wxString
wxDate::GetMonthName()
619 return(FormatDate(wxMONTH
));
622 wxString
wxDate::GetDayOfWeekName()
624 return(FormatDate(wxDAY
));
627 bool wxDate::IsBetween(const wxDate
& first
, const wxDate
& second
) const
629 return (julian
>= first
.julian
&& julian
<= second
.julian
);
632 // This function is from NIHCL
633 wxDate
wxDate::Previous(int dayOfWeek
) const
635 int this_day_Of_Week
, desired_day_Of_Week
;
638 // Set the desired and current day of week to start at 0 (Monday)
639 // and end at 6 (Sunday).
641 desired_day_Of_Week
= dayOfWeek
- 1; // These functions return a value
642 this_day_Of_Week
= GetDayOfWeek() - 1; // from 1-7. Subtract 1 for 0-6.
645 // Have to determine how many days difference from current day back to
646 // desired, if any. Special calculation under the 'if' statement to
647 // effect the wraparound counting from Monday (0) back to Sunday (6).
649 if (desired_day_Of_Week
> this_day_Of_Week
)
650 this_day_Of_Week
+= 7 - desired_day_Of_Week
;
652 this_day_Of_Week
-= desired_day_Of_Week
;
653 j
-= this_day_Of_Week
; // Adjust j to set it at the desired day of week.