]>
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 char *dayname
[] = {
52 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
55 static const char *mname
[] = {
56 "January", "February", "March", "April", "May", "June", "July", "August",
57 "September", "October", "November", "December"
60 static int GauDays
[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_DYNAMIC_CLASS(wxDate
, wxObject
)
66 ////////////////////////////////////////////////////////////
68 ////////////////////////////////////////////////////////////
74 month
= day
= year
= day_of_week
= 0;
78 wxDate::wxDate (long j
) : julian(j
)
85 wxDate::wxDate (int m
, int d
, int y
) : month(m
), day(d
), year(y
)
92 wxDate::wxDate (const wxString
& dat
)
96 if (strcmp(dat
, "TODAY") == 0 || strcmp(dat
, "today") == 0)
98 // Sets the current date
104 strcpy(buf
, (char *) (const char *)dat
);
106 char *token
= strtok(buf
,"/-");
108 day
= atoi(strtok((char *) NULL
,"/-"));
109 year
= atoi(strtok((char *) NULL
," "));
115 wxDate::wxDate (const wxDate
&dt
)
117 DisplayFormat
=dt
.DisplayFormat
;
118 DisplayOptions
=dt
.DisplayOptions
;
125 void wxDate::operator = (const wxDate
&dt
)
127 DisplayFormat
=dt
.DisplayFormat
;
128 DisplayOptions
=dt
.DisplayOptions
;
132 mdy_to_julian (); // wxUSE_TIMEDATE
135 void wxDate::operator = (const wxString
& dat
)
139 if (strcmp(dat
, "TODAY") == 0 || strcmp(dat
, "today") == 0)
141 // Sets the current date
147 strcpy(buf
, (char *)(const char *)dat
);
149 char *token
= strtok(buf
,"/-");
151 day
= atoi(strtok((char *) NULL
,"/-"));
152 year
= atoi(strtok((char *) NULL
," "));
158 //////////////////////////////////////////////////////////////
159 // Conversion operations
160 //////////////////////////////////////////////////////////////
163 wxDate::operator wxString( void )
169 //////////////////////////////////////////////////////////////
171 //////////////////////////////////////////////////////////////
173 wxDate
wxDate::operator + (long i
)
175 wxDate
dp(julian
+ i
);
179 wxDate
wxDate::operator + (int i
)
181 wxDate
dp(julian
+ (long)i
);
185 wxDate
wxDate::operator - (long i
)
187 wxDate
dp(julian
- i
);
191 wxDate
wxDate::operator - (int i
)
193 wxDate
dp(julian
- (long)i
);
197 long wxDate::operator - (const wxDate
&dt
)
199 return ( julian
- dt
.julian
);
202 wxDate
&wxDate::operator += (long i
)
209 wxDate
&wxDate::operator -= (long i
)
216 wxDate
&wxDate::operator ++()
223 wxDate
&wxDate::operator ++(int)
230 wxDate
&wxDate::operator --()
237 wxDate
&wxDate::operator --(int)
244 //////////////////////////////////////////////////////////////
246 //////////////////////////////////////////////////////////////
248 bool WXDLLEXPORT
operator < (const wxDate
&dt1
, const wxDate
&dt2
)
250 return ( dt1
.julian
< dt2
.julian
);
253 bool WXDLLEXPORT
operator <= (const wxDate
&dt1
, const wxDate
&dt2
)
255 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
< dt2
.julian
) );
258 bool WXDLLEXPORT
operator > (const wxDate
&dt1
, const wxDate
&dt2
)
260 return ( dt1
.julian
> dt2
.julian
);
263 bool WXDLLEXPORT
operator >= (const wxDate
&dt1
, const wxDate
&dt2
)
265 return ( (dt1
.julian
== dt2
.julian
) || (dt1
.julian
> dt2
.julian
) );
268 bool WXDLLEXPORT
operator == (const wxDate
&dt1
, const wxDate
&dt2
)
270 return ( dt1
.julian
== dt2
.julian
);
273 bool WXDLLEXPORT
operator != (const wxDate
&dt1
, const wxDate
&dt2
)
275 return ( dt1
.julian
!= dt2
.julian
);
278 ////////////////////////////////////////////////////////////////
279 // Ostream operations
280 ////////////////////////////////////////////////////////////////
282 ostream WXDLLEXPORT
& operator << (ostream
&os
, const wxDate
&dt
)
284 return os
<< (const char *) dt
.FormatDate();
287 //////////////////////////////////////////////////////////////
288 // Conversion routines
289 //////////////////////////////////////////////////////////////
291 void wxDate::julian_to_wday (void)
293 day_of_week
= (int) ((julian
+ 2) % 7 + 1);
296 void wxDate::julian_to_mdy ()
298 long a
,b
,c
,d
,e
,z
,alpha
;
300 // dealing with Gregorian calendar reform
304 alpha
= (long) ((z
-1867216.25) / 36524.25);
305 a
= z
+ 1 + alpha
- alpha
/4;
307 b
= ( a
> 1721423 ? a
+ 1524 : a
+ 1158 );
308 c
= (long) ((b
- 122.1) / 365.25);
309 d
= (long) (365.25 * c
);
310 e
= (long) ((b
- d
) / 30.6001);
311 day
= (int)(b
- d
- (long)(30.6001 * e
));
312 month
= (int)((e
< 13.5) ? e
- 1 : e
- 13);
313 year
= (int)((month
> 2.5 ) ? (c
- 4716) : c
- 4715);
317 void wxDate::mdy_to_julian (void)
320 int work_month
=month
, work_day
=day
, work_year
=year
;
321 // correct for negative year
325 { work_year
--; work_month
+=12; }
327 // deal with Gregorian calendar
328 if (work_year
*10000. + work_month
*100. + work_day
>= 15821015.)
330 a
= (int)(work_year
/100.);
333 julian
= (long) (365.25*work_year
) +
334 (long) (30.6001 * (work_month
+1)) + work_day
+ 1720994L + b
;
338 ////////////////////////////////////////////////////////////////
340 ////////////////////////////////////////////////////////////////
342 wxString
wxDate::FormatDate (int type
) const
344 int actualType
= type
;
345 if (actualType
== -1)
346 actualType
= DisplayFormat
;
350 memset( buf
, '\0', sizeof(buf
) );
351 switch ( actualType
)
354 if ( (day_of_week
< 1) || (day_of_week
> 7) )
355 strcpy(buf
, _("invalid day"));
357 strncpy( buf
, _(dayname
[day_of_week
-1]),
358 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
359 return wxString(buf
);
362 if ( (month
< 1) || (month
> 12) )
363 strcpy(buf
, _("invalid month"));
365 strncpy( buf
, _(mname
[month
-1]),
366 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
367 return wxString(buf
);
370 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
373 strcpy(buf
, _("invalid date"));
374 return wxString(buf
);
376 strncpy( buf
, _(dayname
[day_of_week
-1]),
377 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
379 strncat( buf
, _(mname
[month
-1]),
380 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
382 sprintf( buf
+strlen(buf
), "%d, %d", day
, abs(year
) );
384 strcat(buf
,_(" B.C."));
385 return wxString(buf
);
388 if ( (month
< 1) || (month
> 12) || (day_of_week
< 0) ||
391 strcpy(buf
, _("invalid date"));
392 return wxString(buf
);
394 sprintf(buf
,"%d ", day
);
395 strncat(buf
, _(mname
[month
-1]),
396 (DisplayOptions
& wxDATE_ABBR
) ? ABBR_LENGTH
: 9);
397 sprintf( buf
+strlen(buf
), " %d", abs(year
) );
399 strcat(buf
, _(" B.C."));
400 return wxString(buf
);
404 if (day
==0 || month
==0 || year
==0)
405 strcpy(buf
, _("invalid date"));
407 sprintf( buf
+strlen(buf
), "%1d/%1d/%02d", month
, day
,
408 (DisplayOptions
& wxNO_CENTURY
) && (abs(year
) > 1899)
409 ? (abs(year
) - (abs(year
) / 100 * 100))
411 return wxString(buf
);
416 void wxDate::SetFormat( int format
)
418 DisplayFormat
= format
;
421 int wxDate::SetOption( int option
, bool action
)
427 DisplayOptions
|= wxNO_CENTURY
;
430 DisplayOptions
&= (~wxNO_CENTURY
);
435 DisplayOptions
|= wxDATE_ABBR
;
438 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.