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