]> git.saurik.com Git - wxWidgets.git/blob - include/wx/date.h
1. fixed crash in wxDateTime::ParseDate
[wxWidgets.git] / include / wx / date.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/date.h
3 // Purpose: wxDate class: this class is deprecated, use wxDateTime instead!
4 // Author: Julian Smart, Steve Marcus, Eric Simon, Chris Hill,
5 // Charles D. Price
6 // Modified by: 18.12.99 by VZ to use the new wxDateTime class
7 // Created: 01/02/97
8 // RCS-ID: $Id$
9 // Copyright:(c)
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DATE_H_
14 #define _WX_DATE_H_
15
16 #ifdef __GNUG__
17 #pragma interface "date.h"
18 #endif
19
20 #include "wx/defs.h"
21
22 #if wxUSE_TIMEDATE
23
24 #include "wx/object.h"
25 #include "wx/string.h"
26 #include "wx/datetime.h"
27
28 // ----------------------------------------------------------------------------
29 // constants
30 // ----------------------------------------------------------------------------
31
32 enum wxdate_format_type
33 {
34 wxMDY,
35 wxDAY,
36 wxMONTH,
37 wxFULL,
38 wxEUROPEAN
39 };
40
41 enum // wxdate_format_flags
42 {
43 wxNO_CENTURY = 0x02,
44 wxDATE_ABBR = 0x04
45 };
46
47 // ----------------------------------------------------------------------------
48 // wxDate
49 // ----------------------------------------------------------------------------
50
51 class WXDLLEXPORT wxDate : public wxObject
52 {
53 public:
54 wxDate() { Init(); }
55 wxDate(long j) : m_date((double)(j + 0.5)) { Init(); }
56 wxDate(int m, int d, int y) : m_date(d, (wxDateTime::Month)m, y) { Init(); }
57 wxDate(const wxString& dat) { Init(); (void)m_date.ParseDate(dat); }
58 wxDate(const wxDate &date) { *this = date; }
59
60 wxDate(const wxDateTime& dt) { Init(); m_date = dt; }
61
62 #ifndef __SALFORDC__
63 operator wxString() const { return FormatDate(); }
64 #endif
65
66 void operator=(const wxDate& date)
67 {
68 m_date = date.m_date;
69 m_displayFormat = date.m_displayFormat;
70 m_displayOptions = date.m_displayOptions;
71 }
72
73 void operator=(const wxString& dat) { (void)m_date.ParseDate(dat); }
74
75 wxDate operator+(long i) { return wxDate(GetJulianDate() + i); }
76 wxDate operator+(int i) { return wxDate(GetJulianDate() + (long)i); }
77
78 wxDate operator-(long i) { return wxDate(GetJulianDate() - i); }
79 wxDate operator-(int i) { return wxDate(GetJulianDate() - (long)i); }
80
81 long operator-(const wxDate &dt) const
82 { return GetJulianDate() - dt.GetJulianDate(); }
83
84 wxDate &operator+=(long i) { m_date += wxTimeSpan::Days((int)i); return *this; }
85 wxDate &operator-=(long i) { m_date -= wxTimeSpan::Days((int)i); return *this; }
86
87 wxDate &operator++() { return *this += 1; }
88 wxDate &operator++(int) { return *this += 1; }
89 wxDate &operator--() { return *this -= 1; }
90 wxDate &operator--(int) { return *this -= 1; }
91
92 #if wxUSE_STD_IOSTREAM
93 friend ostream WXDLLEXPORT & operator <<(ostream &os, const wxDate &dt)
94 { return os << dt.FormatDate().mb_str(); }
95 #endif
96
97 void SetFormat(int format) { m_displayFormat = format; }
98 int SetOption(int option, bool enable = TRUE)
99 {
100 if ( enable )
101 m_displayOptions |= option;
102 else
103 m_displayOptions &= ~option;
104
105 return 1; // (VZ: whatever it means)
106 }
107
108 // returns julian date (VZ: the integral part of Julian Day Number)
109 long GetJulianDate() const
110 { return (long)(m_date.GetJulianDayNumber() - 0.5); }
111
112 // returns relative date since Jan. 1
113 int GetDayOfYear() const
114 { return m_date.GetDayOfYear(); }
115
116 // returns TRUE if leap year, FALSE if not
117 bool IsLeapYear() const
118 { return wxDateTime::IsLeapYear(m_date.GetYear()); }
119
120 // Sets to current system date
121 wxDate &Set()
122 { m_date = wxDateTime::Today(); return (wxDate&)*this; }
123 wxDate &Set(long lJulian)
124 { m_date.Set((double)(lJulian + 0.5)); return (wxDate&)*this; }
125 wxDate &Set(int nMonth, int nDay, int nYear)
126 { m_date.Set(nDay, (wxDateTime::Month)nMonth, nYear); return *this; }
127
128 // May also pass neg# to decrement
129 wxDate &AddWeeks(int nCount = 1)
130 { m_date += wxDateSpan::Weeks(nCount); return *this; }
131 wxDate &AddMonths(int nCount = 1)
132 { m_date += wxDateSpan::Months(nCount); return *this; }
133 wxDate &AddYears(int nCount = 1)
134 { m_date += wxDateSpan::Years(nCount); return *this; }
135
136 // Numeric Day of date object
137 int GetDay() const { return m_date.GetDay(); }
138 // Number of days in month(1..31)
139 int GetDaysInMonth() const
140 {
141 return wxDateTime::GetNumberOfDays((wxDateTime::Month)m_date.GetMonth(),
142 m_date.GetYear());
143 }
144
145 // First Day Of Month(1..7)
146 int GetFirstDayOfMonth() const
147 { return wxDate(GetMonth(), 1, GetYear()).GetDayOfWeek(); }
148
149 // Character Day Of Week('Sunday'..'Saturday')
150 wxString GetDayOfWeekName() const { return FormatDate(wxDAY); }
151 int GetDayOfWeek() const { return (int)m_date.GetWeekDay() + 1; }
152
153 // Numeric Week Of Month(1..6) (VZ: I'd love to see a month with 6 weeks)
154 int GetWeekOfMonth() const { return m_date.GetWeekOfMonth(); }
155 // Numeric Week Of Year(1..52) (VZ: but there are years with 53 weeks)
156 int GetWeekOfYear() const { return m_date.GetWeekOfYear(); }
157
158 // Character Month name
159 wxString GetMonthName() { return FormatDate(wxMONTH); }
160 // Month Number(1..12)
161 int GetMonth() const { return m_date.GetMonth() + 1; }
162
163 // First Date Of Month
164 wxDate GetMonthStart() const { return(wxDate(GetMonth(), 1, GetYear())); }
165 // Last Date Of Month
166 wxDate GetMonthEnd() const { return wxDate(GetMonth()+1, 1, GetYear())-1; }
167
168 // eg. 1992
169 int GetYear() const { return m_date.GetYear(); }
170 // First Date Of Year
171 wxDate GetYearStart() const { return wxDate(1, 1, GetYear()); }
172 // Last Date Of Year
173 wxDate GetYearEnd() const { return wxDate(1, 1, GetYear()+1) - 1; }
174
175 bool IsBetween(const wxDate& first, const wxDate& second) const
176 {
177 return m_date.IsBetween(first.m_date, second.m_date);
178 }
179
180 wxDate Previous(int dayOfWeek) const
181 {
182 wxDate prev = *this;
183 int dow = GetDayOfWeek();
184 prev -= dayOfWeek > dow ? 7 - (dayOfWeek - dow) : dow - dayOfWeek;
185
186 return prev;
187 }
188
189 wxString FormatDate(int type = -1) const
190 {
191 static const wxChar *formats[] =
192 {
193 // MDY (week)DAY MONTH FULL EUROPEAN
194 _T("%m/%d/%Y"), _T("%A"), _T("%B"), _T("%A, %B %d, %Y"), _T("%d %B %Y")
195 };
196
197 wxString fmt = formats[type == -1 ? m_displayFormat : type];
198
199 if ( m_displayOptions & wxDATE_ABBR )
200 {
201 fmt.Replace(_T("A"), _T("a"));
202 fmt.Replace(_T("B"), _T("b"));
203 }
204 if ( m_displayOptions & wxNO_CENTURY )
205 {
206 fmt.Replace(_T("Y"), _T("y"));
207 }
208
209 return m_date.Format(fmt);
210 }
211
212 protected:
213 void Init() { m_displayFormat = wxMDY; m_displayOptions = 0; }
214
215 #if 0 // the old wxDate members - unused any more
216 unsigned long julian; // see julDate(); days since 1/1/4713 B.C.
217 int month; // see NMonth()
218 int day; // see Day()
219 int year; // see NYear4()
220 int day_of_week; // see NDOW(); 1 = Sunday, ... 7 = Saturday
221
222 void julian_to_mdy(); // convert julian day to mdy
223 void julian_to_wday(); // convert julian day to day_of_week
224 void mdy_to_julian(); // convert mdy to julian day
225 #endif // 0
226
227 private:
228 wxDateTime m_date;
229
230 int m_displayFormat;
231 int m_displayOptions;
232
233 private:
234 DECLARE_DYNAMIC_CLASS(wxDate)
235 };
236
237 // ----------------------------------------------------------------------------
238 // global functions
239 // ----------------------------------------------------------------------------
240
241 inline bool WXDLLEXPORT operator <(const wxDate &dt1, const wxDate &dt2)
242 { return dt1.GetJulianDate() < dt2.GetJulianDate(); }
243 inline bool WXDLLEXPORT operator <=(const wxDate &dt1, const wxDate &dt2)
244 { return dt1.GetJulianDate() <= dt2.GetJulianDate(); }
245 inline bool WXDLLEXPORT operator >(const wxDate &dt1, const wxDate &dt2)
246 { return dt1.GetJulianDate() > dt2.GetJulianDate(); }
247 inline bool WXDLLEXPORT operator >=(const wxDate &dt1, const wxDate &dt2)
248 { return dt1.GetJulianDate() >= dt2.GetJulianDate(); }
249 inline bool WXDLLEXPORT operator ==(const wxDate &dt1, const wxDate &dt2)
250 { return dt1.GetJulianDate() == dt2.GetJulianDate(); }
251 inline bool WXDLLEXPORT operator !=(const wxDate &dt1, const wxDate &dt2)
252 { return dt1.GetJulianDate() != dt2.GetJulianDate(); }
253
254 #endif // wxUSE_TIMEDATE
255 #endif
256 // _WX_DATE_H_