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