]> git.saurik.com Git - wxWidgets.git/blob - src/common/date.cpp
just to resolve a dummy conflict
[wxWidgets.git] / src / common / date.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: date.cpp
3 // Purpose: wxDate class
4 // Author:
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
11 // Modified by:
12 // Created: 01/02/97
13 // RCS-ID: $Id$
14 // Copyright: (c) Julian Smart and Markus Holzem
15 // Licence: wxWindows licence
16 /////////////////////////////////////////////////////////////////////////////
17
18 #ifdef __GNUG__
19 #pragma implementation "date.h"
20 #endif
21
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
24
25 #ifdef __BORLANDC__
26 #pragma hdrstop
27 #endif
28
29 #if wxUSE_TIMEDATE
30
31 #include "wx/date.h"
32 #include <wx/intl.h>
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #if wxUSE_IOSTREAMH
39 #include <iostream.h>
40 #else
41 #include <iostream>
42 #endif
43
44 #include <time.h>
45 #include <string.h>
46
47 #define ABBR_LENGTH 3
48
49 static const wxChar *dayname[] = {
50 _T("Sunday"), _T("Monday"), _T("Tuesday"), _T("Wednesday"),
51 _T("Thursday"), _T("Friday"), _T("Saturday")
52 };
53
54 static const wxChar *mname[] = {
55 _T("January"), _T("February"), _T("March"), _T("April"), _T("May"), _T("June"),
56 _T("July"), _T("August"), _T("September"), _T("October"), _T("November"), _T("December")
57 };
58
59 static int GauDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
60
61 #if !USE_SHARED_LIBRARY
62 IMPLEMENT_DYNAMIC_CLASS(wxDate, wxObject)
63 #endif
64
65 ////////////////////////////////////////////////////////////
66 // Constructors
67 ////////////////////////////////////////////////////////////
68
69 wxDate::wxDate()
70 {
71 DisplayFormat=wxMDY;
72 DisplayOptions='\0';
73 month = day = year = day_of_week = 0;
74 julian = 0;
75 }
76
77 wxDate::wxDate (long j) : julian(j)
78 {
79 DisplayFormat=wxMDY;
80 DisplayOptions='\0';
81 julian_to_mdy ();
82 }
83
84 wxDate::wxDate (int m, int d, int y) : month(m), day(d), year(y)
85 {
86 DisplayFormat=wxMDY;
87 DisplayOptions='\0';
88 mdy_to_julian ();
89 }
90
91 wxDate::wxDate (const wxString& dat)
92 {
93 DisplayFormat=wxMDY;
94 DisplayOptions='\0';
95 if (wxStrcmp(dat, _T("TODAY")) == 0 || wxStrcmp(dat, _T("today")) == 0)
96 {
97 // Sets the current date
98 Set();
99 }
100 else
101 {
102 wxChar buf[100];
103 wxStrcpy(buf, WXSTRINGCAST dat);
104
105 wxChar *save_ptr, *token = wxStrtok(buf,_T("/-"),&save_ptr);
106 month = wxAtoi(token);
107 day = wxAtoi(wxStrtok((wxChar *) NULL,_T("/-"),&save_ptr));
108 year = wxAtoi(wxStrtok((wxChar *) NULL,_T(" "),&save_ptr));
109 }
110
111 mdy_to_julian ();
112 }
113
114 wxDate::wxDate (const wxDate &dt)
115 {
116 DisplayFormat=dt.DisplayFormat;
117 DisplayOptions=dt.DisplayOptions;
118 month = dt.month;
119 day = dt.day;
120 year = dt.year;
121 mdy_to_julian ();
122 }
123
124 void wxDate::operator = (const wxDate &dt)
125 {
126 DisplayFormat=dt.DisplayFormat;
127 DisplayOptions=dt.DisplayOptions;
128 month = dt.month;
129 day = dt.day;
130 year = dt.year;
131 mdy_to_julian (); // wxUSE_TIMEDATE
132 }
133
134 void wxDate::operator = (const wxString& dat)
135 {
136 DisplayFormat=wxMDY;
137 DisplayOptions='\0';
138 if (wxStrcmp(dat, _T("TODAY")) == 0 || wxStrcmp(dat, _T("today")) == 0)
139 {
140 // Sets the current date
141 Set();
142 }
143 else
144 {
145 wxChar buf[100];
146 wxStrcpy(buf, WXSTRINGCAST dat);
147
148 wxChar *save_ptr, *token = wxStrtok(buf,_T("/-"),&save_ptr);
149 month = wxAtoi(token);
150 day = wxAtoi(wxStrtok((wxChar *) NULL,_T("/-"),&save_ptr));
151 year = wxAtoi(wxStrtok((wxChar *) NULL,_T(" "),&save_ptr));
152 }
153
154 mdy_to_julian ();
155 }
156
157 //////////////////////////////////////////////////////////////
158 // Conversion operations
159 //////////////////////////////////////////////////////////////
160
161 #ifndef __SALFORDC__
162 wxDate::operator wxString( void )
163 {
164 return FormatDate();
165 }
166 #endif
167
168 //////////////////////////////////////////////////////////////
169 // Date Arithmetic
170 //////////////////////////////////////////////////////////////
171
172 wxDate wxDate::operator + (long i)
173 {
174 wxDate dp(julian + i);
175 return dp;
176 }
177
178 wxDate wxDate::operator + (int i)
179 {
180 wxDate dp(julian + (long)i);
181 return dp;
182 }
183
184 wxDate wxDate::operator - (long i)
185 {
186 wxDate dp(julian - i);
187 return dp;
188 }
189
190 wxDate wxDate::operator - (int i)
191 {
192 wxDate dp(julian - (long)i);
193 return dp;
194 }
195
196 long wxDate::operator - (const wxDate &dt)
197 {
198 return ( julian - dt.julian );
199 }
200
201 wxDate &wxDate::operator += (long i)
202 {
203 julian += i;
204 julian_to_mdy();
205 return *this;
206 }
207
208 wxDate &wxDate::operator -= (long i)
209 {
210 julian -= i;
211 julian_to_mdy();
212 return *this;
213 }
214
215 wxDate &wxDate::operator ++()
216 {
217 julian++;
218 julian_to_mdy();
219 return *this;
220 }
221
222 wxDate &wxDate::operator ++(int)
223 {
224 julian++;
225 julian_to_mdy();
226 return *this;
227 }
228
229 wxDate &wxDate::operator --()
230 {
231 julian--;
232 julian_to_mdy();
233 return *this;
234 }
235
236 wxDate &wxDate::operator --(int)
237 {
238 julian--;
239 julian_to_mdy();
240 return *this;
241 }
242
243 //////////////////////////////////////////////////////////////
244 // Date comparison
245 //////////////////////////////////////////////////////////////
246
247 bool WXDLLEXPORT operator < (const wxDate &dt1, const wxDate &dt2)
248 {
249 return ( dt1.julian < dt2.julian );
250 }
251
252 bool WXDLLEXPORT operator <= (const wxDate &dt1, const wxDate &dt2)
253 {
254 return ( (dt1.julian == dt2.julian) || (dt1.julian < dt2.julian) );
255 }
256
257 bool WXDLLEXPORT operator > (const wxDate &dt1, const wxDate &dt2)
258 {
259 return ( dt1.julian > dt2.julian );
260 }
261
262 bool WXDLLEXPORT operator >= (const wxDate &dt1, const wxDate &dt2)
263 {
264 return ( (dt1.julian == dt2.julian) || (dt1.julian > dt2.julian) );
265 }
266
267 bool WXDLLEXPORT operator == (const wxDate &dt1, const wxDate &dt2)
268 {
269 return ( dt1.julian == dt2.julian );
270 }
271
272 bool WXDLLEXPORT operator != (const wxDate &dt1, const wxDate &dt2)
273 {
274 return ( dt1.julian != dt2.julian );
275 }
276
277 ////////////////////////////////////////////////////////////////
278 // Ostream operations
279 ////////////////////////////////////////////////////////////////
280
281 ostream WXDLLEXPORT & operator << (ostream &os, const wxDate &dt)
282 {
283 return os << dt.FormatDate().mb_str();
284 }
285
286 //////////////////////////////////////////////////////////////
287 // Conversion routines
288 //////////////////////////////////////////////////////////////
289
290 void wxDate::julian_to_wday (void)
291 {
292 // Correction by Peter Stadel <peters@jetcity.com>
293 day_of_week = ((julian - 2) % 7L);
294 /*
295 day_of_week = (int) ((julian + 2) % 7 + 1);
296 */
297 }
298
299 void wxDate::julian_to_mdy ()
300 {
301 long a,b,c,d,e,z,alpha;
302 z = julian+1;
303 // dealing with Gregorian calendar reform
304 if (z < 2299161L)
305 a = z;
306 else {
307 alpha = (long) ((z-1867216.25) / 36524.25);
308 a = z + 1 + alpha - alpha/4;
309 }
310 b = ( a > 1721423 ? a + 1524 : a + 1158 );
311 c = (long) ((b - 122.1) / 365.25);
312 d = (long) (365.25 * c);
313 e = (long) ((b - d) / 30.6001);
314 day = (int)(b - d - (long)(30.6001 * e));
315 month = (int)((e < 13.5) ? e - 1 : e - 13);
316 year = (int)((month > 2.5 ) ? (c - 4716) : c - 4715);
317 julian_to_wday ();
318 }
319
320 void wxDate::mdy_to_julian (void)
321 {
322 int a,b=0;
323 int work_month=month, work_day=day, work_year=year;
324 // correct for negative year
325 if (work_year < 0)
326 work_year++;
327 if (work_month <= 2)
328 { work_year--; work_month +=12; }
329
330 // deal with Gregorian calendar
331 if (work_year*10000. + work_month*100. + work_day >= 15821015.)
332 {
333 a = (int)(work_year/100.);
334 b = 2 - a + a/4;
335 }
336 julian = (long) (365.25*work_year) +
337 (long) (30.6001 * (work_month+1)) + work_day + 1720994L + b;
338 julian_to_wday ();
339 }
340
341 ////////////////////////////////////////////////////////////////
342 // Format routine
343 ////////////////////////////////////////////////////////////////
344
345 wxString wxDate::FormatDate (int type) const
346 {
347 int actualType = type;
348 if (actualType == -1)
349 actualType = DisplayFormat;
350
351 wxChar buf[40];
352
353 memset( buf, '\0', sizeof(buf) );
354 switch ( actualType )
355 {
356 case wxDAY:
357 if ( (day_of_week < 1) || (day_of_week > 7) )
358 wxStrcpy(buf, _("invalid day"));
359 else
360 wxStrncpy( buf, wxGetTranslation(dayname[day_of_week-1]),
361 (DisplayOptions & wxDATE_ABBR) ? ABBR_LENGTH : 9);
362 return wxString(buf);
363
364 case wxMONTH:
365 if ( (month < 1) || (month > 12) )
366 wxStrcpy(buf, _("invalid month"));
367 else
368 wxStrncpy( buf, wxGetTranslation(mname[month-1]),
369 (DisplayOptions & wxDATE_ABBR) ? ABBR_LENGTH : 9);
370 return wxString(buf);
371
372 case wxFULL:
373 if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
374 (day_of_week > 7) )
375 {
376 wxStrcpy(buf, _("invalid date"));
377 return wxString(buf);
378 }
379 wxStrncpy( buf, wxGetTranslation(dayname[day_of_week-1]),
380 (DisplayOptions & wxDATE_ABBR) ? ABBR_LENGTH : 9);
381 wxStrcat( buf, _T(", "));
382 wxStrncat( buf, wxGetTranslation(mname[month-1]),
383 (DisplayOptions & wxDATE_ABBR) ? ABBR_LENGTH : 9);
384 wxStrcat( buf, _T(" "));
385 wxSprintf( buf+wxStrlen(buf), _T("%d, %d"), day, abs(year) );
386 if (year < 0)
387 wxStrcat(buf,_(" B.C."));
388 return wxString(buf);
389
390 case wxEUROPEAN:
391 if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
392 (day_of_week > 7) )
393 {
394 wxStrcpy(buf, _("invalid date"));
395 return wxString(buf);
396 }
397 wxSprintf(buf,_T("%d "), day);
398 wxStrncat(buf, wxGetTranslation(mname[month-1]),
399 (DisplayOptions & wxDATE_ABBR) ? ABBR_LENGTH : 9);
400 wxSprintf( buf+wxStrlen(buf), _T(" %d"), abs(year) );
401 if (year < 0)
402 wxStrcat(buf, _(" B.C."));
403 return wxString(buf);
404
405 case wxMDY:
406 default:
407 if (day==0 || month==0 || year==0)
408 wxStrcpy(buf, _("invalid date"));
409 else
410 wxSprintf( buf+wxStrlen(buf), _T("%1d/%1d/%02d"), month, day,
411 (DisplayOptions & wxNO_CENTURY) && (abs(year) > 1899)
412 ? (abs(year) - (abs(year) / 100 * 100))
413 : (abs(year)) );
414 return wxString(buf);
415 }
416 return wxString(_T(""));
417 }
418
419 void wxDate::SetFormat( int format )
420 {
421 DisplayFormat = format;
422 }
423
424 int wxDate::SetOption( int option, bool action )
425 {
426 switch ( option )
427 {
428 case wxNO_CENTURY:
429 if ( action )
430 DisplayOptions |= wxNO_CENTURY;
431 else
432 {
433 DisplayOptions &= (~wxNO_CENTURY);
434 }
435 return 1;
436 case wxDATE_ABBR:
437 if ( action )
438 DisplayOptions |= wxDATE_ABBR;
439 else
440 {
441 DisplayOptions &= (~wxDATE_ABBR);
442 }
443 return 1;
444 default:
445 return 0;
446 }
447 return 0;
448 }
449
450 ///////////////////////////////////////////////////////////////
451 // Miscellaneous Routines
452 ///////////////////////////////////////////////////////////////
453
454 long wxDate::GetJulianDate( void ) const
455 {
456 return julian;
457 }
458
459 int wxDate::GetDayOfYear( void ) const
460 {
461 wxDate temp( 1, 1, year );
462
463 return (int) (julian - temp.julian + 1);
464 }
465
466
467 bool wxDate::IsLeapYear( void ) const
468 {
469 return ( (year >= 1582) ?
470 (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ):
471 (year % 4 == 0) );
472 }
473
474 // Version 4.0 Extension to Public Interface - CDP
475
476 wxDate& wxDate::Set()
477 {
478 //#ifdef __WXMSW__
479 #if 0
480 struct _dosdate_t sDate;
481 _dos_getdate(&sDate);
482
483 month = sDate.month;
484 day = sDate.day;
485 year = sDate.year;
486
487 mdy_to_julian();
488 #else
489 time_t now = time((time_t *) NULL);
490 struct tm *localTime = localtime(&now);
491
492 month = localTime->tm_mon + 1;
493 day = localTime->tm_mday;
494 year = localTime->tm_year + 1900;
495
496 mdy_to_julian ();
497 #endif
498 return *this;
499 }
500
501 wxDate& wxDate::Set(
502 int nMonth,
503 int nDay,
504 int nYear)
505 {
506 month = nMonth;
507 year = nYear < 0 ? 9999 : nYear;
508 year = nYear > 9999 ? 0 : nYear;
509 day = nDay < GetDaysInMonth() ? nDay : GetDaysInMonth();
510
511 mdy_to_julian();
512 return *this;
513 }
514
515 wxDate &
516 wxDate::Set(long j)
517 {
518 julian = j;
519
520 julian_to_mdy();
521 return *this;
522 }
523
524
525 int wxDate::GetDaysInMonth()
526 {
527 return GauDays[month-1] + (month==2 && IsLeapYear());
528 }
529
530 int wxDate::GetFirstDayOfMonth() const
531 {
532 return wxDate(month, 1, year).GetDayOfWeek();
533 }
534
535 int wxDate::GetDay() const
536 {
537 return day;
538 }
539
540 int wxDate::GetDayOfWeek() const
541 {
542 return day_of_week;
543 }
544
545 int wxDate::GetYear() const
546 {
547 return year;
548 }
549
550 int wxDate::GetMonth() const
551 {
552 return month;
553 }
554
555 wxDate& wxDate::AddWeeks(int nCount)
556 {
557 Set(julian + (long)nCount*7);
558 return *this;
559 }
560
561 wxDate& wxDate::AddMonths(int nCount)
562 {
563 month += nCount;
564
565 if (month < 1) {
566 month = 12;
567 year--;
568 }
569
570 if (month > 12) {
571 month = 1;
572 year++;
573 }
574 mdy_to_julian();
575 return *this;
576 }
577
578 wxDate& wxDate::AddYears(int nCount)
579 {
580 year += nCount;
581 mdy_to_julian();
582 return *this;
583 }
584
585 int wxDate::GetWeekOfMonth()
586 {
587 // Abs day includes the days from previous month that fills up
588 // the begin. of the week.
589 int nAbsDay = day + GetFirstDayOfMonth()-1;
590 return (nAbsDay-GetDayOfWeek())/7 + 1;
591 }
592
593 int wxDate::GetWeekOfYear()
594 {
595 wxDate doTemp(1, 1, year);
596 return (int)(((julian - doTemp.julian+1)/7) + 1);
597 }
598
599 wxDate wxDate::GetMonthStart()
600 {
601 return(wxDate(month, 1, year));
602 }
603
604 wxDate wxDate::GetMonthEnd()
605 {
606 return(wxDate(month+1, 1, year)-1);
607 }
608
609 wxDate wxDate::GetYearStart()
610 {
611 return(wxDate(1, 1, year));
612 }
613
614 wxDate wxDate::GetYearEnd()
615 {
616 return(wxDate(1, 1, year+1)-1);
617 }
618
619 wxString wxDate::GetMonthName()
620 {
621 return(FormatDate(wxMONTH));
622 }
623
624 wxString wxDate::GetDayOfWeekName()
625 {
626 return(FormatDate(wxDAY));
627 }
628
629 bool wxDate::IsBetween(const wxDate& first, const wxDate& second) const
630 {
631 return (julian >= first.julian && julian <= second.julian);
632 }
633
634 // This function is from NIHCL
635 wxDate wxDate::Previous(int dayOfWeek) const
636 {
637 int this_day_Of_Week, desired_day_Of_Week;
638 long j;
639
640 // Set the desired and current day of week to start at 0 (Monday)
641 // and end at 6 (Sunday).
642
643 desired_day_Of_Week = dayOfWeek - 1; // These functions return a value
644 this_day_Of_Week = GetDayOfWeek() - 1; // from 1-7. Subtract 1 for 0-6.
645 j = julian;
646
647 // Have to determine how many days difference from current day back to
648 // desired, if any. Special calculation under the 'if' statement to
649 // effect the wraparound counting from Monday (0) back to Sunday (6).
650
651 if (desired_day_Of_Week > this_day_Of_Week)
652 this_day_Of_Week += 7 - desired_day_Of_Week;
653 else
654 this_day_Of_Week -= desired_day_Of_Week;
655 j -= this_day_Of_Week; // Adjust j to set it at the desired day of week.
656 return wxDate(j);
657 }
658
659 #endif