]>
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"
38 #include "wx/ioswrap.h"
45 static const wxChar
*dayname
[] = {
46 wxT("Sunday"), wxT("Monday"), wxT("Tuesday"), wxT("Wednesday"),
47 wxT("Thursday"), wxT("Friday"), wxT("Saturday")
50 static const wxChar
*mname
[] = {
51 wxT("January"), wxT("February"), wxT("March"), wxT("April"), wxT("May"), wxT("June"),
52 wxT("July"), wxT("August"), wxT("September"), wxT("October"), wxT("November"), wxT("December")
55 static int GauDays
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
57 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
59 ////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////
67 month
= day
= year
= day_of_week
= 0;
71 wxDate::wxDate (long j
) : julian(j
)
78 wxDate::wxDate (int m
, int d
, int y
) : month(m
), day(d
), year(y
)
85 wxDate::wxDate (const wxString
& dat
)
89 if (wxStrcmp(dat
, wxT("TODAY")) == 0 || wxStrcmp(dat
, wxT("today")) == 0)
91 // Sets the current date
99 wxChar
*save_ptr
, *token
= wxStrtok(buf
,wxT("/-"),&save_ptr
);
100 month
= wxAtoi(token
);
101 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,wxT("/-"),&save_ptr
));
102 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,wxT(" "),&save_ptr
));
108 wxDate::wxDate (const wxDate
&dt
)
110 DisplayFormat
=dt
.DisplayFormat
;
111 DisplayOptions
=dt
.DisplayOptions
;
118 void wxDate::operator = (const wxDate
&dt
)
120 DisplayFormat
=dt
.DisplayFormat
;
121 DisplayOptions
=dt
.DisplayOptions
;
125 mdy_to_julian (); // wxUSE_TIMEDATE
128 void wxDate::operator = (const wxString
& dat
)
132 if (wxStrcmp(dat
, wxT("TODAY")) == 0 || wxStrcmp(dat
, wxT("today")) == 0)
134 // Sets the current date
142 wxChar
*save_ptr
, *token
= wxStrtok(buf
,wxT("/-"),&save_ptr
);
143 month
= wxAtoi(token
);
144 day
= wxAtoi(wxStrtok((wxChar
*) NULL
,wxT("/-"),&save_ptr
));
145 year
= wxAtoi(wxStrtok((wxChar
*) NULL
,wxT(" "),&save_ptr
));
151 //////////////////////////////////////////////////////////////
152 // Conversion operations
153 //////////////////////////////////////////////////////////////
156 wxDate::operator wxString( void )
162 //////////////////////////////////////////////////////////////
164 //////////////////////////////////////////////////////////////
166 wxDate
wxDate::operator + (long i
)
168 wxDate
dp(julian
+ i
);
172 wxDate
wxDate::operator + (int i
)
174 wxDate
dp(julian
+ (long)i
);
178 wxDate
wxDate::operator - (long i
)
180 wxDate
dp(julian
- i
);
184 wxDate
wxDate::operator - (int i
)
186 wxDate
dp(julian
- (long)i
);
190 long wxDate::operator - (const wxDate
&dt
)
192 return ( julian
- dt
.julian
);
195 wxDate
&wxDate::operator += (long i
)
202 wxDate
&wxDate::operator -= (long i
)
209 wxDate
&wxDate::operator ++()
216 wxDate
&wxDate::operator ++(int)
223 wxDate
&wxDate::operator --()
230 wxDate
&wxDate::operator --(int)
237 //////////////////////////////////////////////////////////////
239 //////////////////////////////////////////////////////////////
241 bool WXDLLEXPORT
operator < (const wxDate
&dt1
, const wxDate
&dt2
)
243 return ( dt1
.julian
< dt2
.julian
);
246 bool WXDLLEXPORT
operator <= (const wxDate
&dt1
, const wxDate
&dt2
)
248 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
< dt2
.julian
) );
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
);
272 #if wxUSE_STD_IOSTREAM
274 ////////////////////////////////////////////////////////////////
275 // Ostream operations
276 ////////////////////////////////////////////////////////////////
278 ostream WXDLLEXPORT
& operator << (ostream
&os
, const wxDate
&dt
)
280 return os
<< dt
.FormatDate().mb_str();
285 //////////////////////////////////////////////////////////////
286 // Conversion routines
287 //////////////////////////////////////////////////////////////
289 void wxDate::julian_to_wday (void)
291 // Correction by Peter Stadel <peters@jetcity.com>
292 day_of_week
= (int)((julian
- 2) % 7L);
294 day_of_week = (int) ((julian + 2) % 7 + 1);
298 void wxDate::julian_to_mdy ()
300 long a
,b
,c
,d
,e
,z
,alpha
;
302 // dealing with Gregorian calendar reform
306 alpha
= (long) ((z
-1867216.25) / 36524.25);
307 a
= z
+ 1 + alpha
- alpha
/4;
309 b
= ( a
> 1721423 ? a
+ 1524 : a
+ 1158 );
310 c
= (long) ((b
- 122.1) / 365.25);
311 d
= (long) (365.25 * c
);
312 e
= (long) ((b
- d
) / 30.6001);
313 day
= (int)(b
- d
- (long)(30.6001 * e
));
314 month
= (int)((e
< 13.5) ? e
- 1 : e
- 13);
315 year
= (int)((month
> 2.5 ) ? (c
- 4716) : c
- 4715);
319 void wxDate::mdy_to_julian (void)
322 int work_month
=month
, work_day
=day
, work_year
=year
;
323 // correct for negative year
327 { work_year
--; work_month
+=12; }
329 // deal with Gregorian calendar
330 if (work_year
*10000. + work_month
*100. + work_day
>= 15821015.)
332 a
= (int)(work_year
/100.);
335 julian
= (long) (365.25*work_year
) +
336 (long) (30.6001 * (work_month
+1)) + work_day
+ 1720994L + b
;
340 ////////////////////////////////////////////////////////////////
342 ////////////////////////////////////////////////////////////////
344 wxString
wxDate::FormatDate (int type
) const
346 int actualType
= type
;
347 if (actualType
== -1)
348 actualType
= DisplayFormat
;
352 memset( buf
, '\0', sizeof(buf
) );
353 switch ( actualType
)
356 if ( (day_of_week
< 1) || (day_of_week
> 7) )
357 wxStrcpy(buf
, _("invalid day"));
359 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
360 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
361 return wxString(buf
);
364 if ( (month
< 1) || (month
> 12) )
365 wxStrcpy(buf
, _("invalid month"));
367 wxStrncpy( buf
, wxGetTranslation(mname
[month
-1]),
368 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
369 return wxString(buf
);
372 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
375 wxStrcpy(buf
, _("invalid date"));
376 return wxString(buf
);
378 wxStrncpy( buf
, wxGetTranslation(dayname
[day_of_week
-1]),
379 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
380 wxStrcat( buf
, wxT(", "));
381 wxStrncat( buf
, wxGetTranslation(mname
[month
-1]),
382 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
383 wxStrcat( buf
, wxT(" "));
384 wxSprintf( buf
+wxStrlen(buf
), wxT("%d, %d"), day
, abs(year
) );
386 wxStrcat(buf
,_(" B.C."));
387 return wxString(buf
);
390 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
393 wxStrcpy(buf
, _("invalid date"));
394 return wxString(buf
);
396 wxSprintf(buf
,wxT("%d "), day
);
397 wxStrncat(buf
, wxGetTranslation(mname
[month
-1]),
398 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
399 wxSprintf( buf
+wxStrlen(buf
), wxT(" %d"), abs(year
) );
401 wxStrcat(buf
, _(" B.C."));
402 return wxString(buf
);
406 if (day
==0 || month
==0 || year
==0)
407 wxStrcpy(buf
, _("invalid date"));
409 wxSprintf( buf
+wxStrlen(buf
), wxT("%1d/%1d/%02d"), month
, day
,
410 (DisplayOptions
& wxNO_CENTURY
) && (abs(year
) > 1899)
411 ? (abs(year
) - (abs(year
) / 100 * 100))
413 return wxString(buf
);
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
);
447 ///////////////////////////////////////////////////////////////
448 // Miscellaneous Routines
449 ///////////////////////////////////////////////////////////////
451 long wxDate::GetJulianDate( void ) const
456 int wxDate::GetDayOfYear( void ) const
458 wxDate
temp( 1, 1, year
);
460 return (int) (julian
- temp
.julian
+ 1);
464 bool wxDate::IsLeapYear( void ) const
466 return ( (year
>= 1582) ?
467 (year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0 ):
471 // Version 4.0 Extension to Public Interface - CDP
473 wxDate
& wxDate::Set()
477 struct _dosdate_t sDate
;
478 _dos_getdate(&sDate
);
486 time_t now
= time((time_t *) NULL
);
487 struct tm
*localTime
= localtime(&now
);
489 month
= localTime
->tm_mon
+ 1;
490 day
= localTime
->tm_mday
;
491 year
= localTime
->tm_year
+ 1900;
504 year
= nYear
< 0 ? 9999 : nYear
;
505 year
= nYear
> 9999 ? 0 : nYear
;
506 day
= nDay
< GetDaysInMonth() ? nDay
: GetDaysInMonth();
522 int wxDate::GetDaysInMonth()
524 return GauDays
[month
-1] + (month
==2 && IsLeapYear());
527 int wxDate::GetFirstDayOfMonth() const
529 return wxDate(month
, 1, year
).GetDayOfWeek();
532 int wxDate::GetDay() const
537 int wxDate::GetDayOfWeek() const
542 int wxDate::GetYear() const
547 int wxDate::GetMonth() const
552 wxDate
& wxDate::AddWeeks(int nCount
)
554 Set(julian
+ (long)nCount
*7);
558 wxDate
& wxDate::AddMonths(int nCount
)
575 wxDate
& wxDate::AddYears(int nCount
)
582 int wxDate::GetWeekOfMonth()
584 // Abs day includes the days from previous month that fills up
585 // the begin. of the week.
586 int nAbsDay
= day
+ GetFirstDayOfMonth()-1;
587 return (nAbsDay
-GetDayOfWeek())/7 + 1;
590 int wxDate::GetWeekOfYear()
592 wxDate
doTemp(1, 1, year
);
593 return (int)(((julian
- doTemp
.julian
+1)/7) + 1);
596 wxDate
wxDate::GetMonthStart()
598 return(wxDate(month
, 1, year
));
601 wxDate
wxDate::GetMonthEnd()
603 return(wxDate(month
+1, 1, year
)-1);
606 wxDate
wxDate::GetYearStart()
608 return(wxDate(1, 1, year
));
611 wxDate
wxDate::GetYearEnd()
613 return(wxDate(1, 1, year
+1)-1);
616 wxString
wxDate::GetMonthName()
618 return(FormatDate(wxMONTH
));
621 wxString
wxDate::GetDayOfWeekName()
623 return(FormatDate(wxDAY
));
626 bool wxDate::IsBetween(const wxDate
& first
, const wxDate
& second
) const
628 return (julian
>= first
.julian
&& julian
<= second
.julian
);
631 // This function is from NIHCL
632 wxDate
wxDate::Previous(int dayOfWeek
) const
634 int this_day_Of_Week
, desired_day_Of_Week
;
637 // Set the desired and current day of week to start at 0 (Monday)
638 // and end at 6 (Sunday).
640 desired_day_Of_Week
= dayOfWeek
- 1; // These functions return a value
641 this_day_Of_Week
= GetDayOfWeek() - 1; // from 1-7. Subtract 1 for 0-6.
644 // Have to determine how many days difference from current day back to
645 // desired, if any. Special calculation under the 'if' statement to
646 // effect the wraparound counting from Monday (0) back to Sunday (6).
648 if (desired_day_Of_Week
> this_day_Of_Week
)
649 this_day_Of_Week
+= 7 - desired_day_Of_Week
;
651 this_day_Of_Week
-= desired_day_Of_Week
;
652 j
-= this_day_Of_Week
; // Adjust j to set it at the desired day of week.