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