]>
Commit | Line | Data |
---|---|---|
cd0b1709 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/date.h | |
3 | // Purpose: wxDate class: this class is deprecated, use wxDateTime instead! | |
c801d85f KB |
4 | // Author: Julian Smart, Steve Marcus, Eric Simon, Chris Hill, |
5 | // Charles D. Price | |
cd0b1709 | 6 | // Modified by: 18.12.99 by VZ to use the new wxDateTime class |
c801d85f KB |
7 | // Created: 01/02/97 |
8 | // RCS-ID: $Id$ | |
cd0b1709 | 9 | // Copyright:(c) |
c30aaf75 | 10 | // Licence: wxWindows licence |
cd0b1709 | 11 | /////////////////////////////////////////////////////////////////////////////// |
c801d85f | 12 | |
34138703 JS |
13 | #ifndef _WX_DATE_H_ |
14 | #define _WX_DATE_H_ | |
c801d85f KB |
15 | |
16 | #ifdef __GNUG__ | |
cd0b1709 | 17 | #pragma interface "date.h" |
c801d85f KB |
18 | #endif |
19 | ||
cd0b1709 | 20 | #include "wx/defs.h" |
c801d85f | 21 | |
9838df2c | 22 | #if wxUSE_TIMEDATE |
c801d85f | 23 | |
cd0b1709 VZ |
24 | #include "wx/object.h" |
25 | #include "wx/string.h" | |
26 | #include "wx/datetime.h" | |
c801d85f | 27 | |
cd0b1709 VZ |
28 | // ---------------------------------------------------------------------------- |
29 | // constants | |
30 | // ---------------------------------------------------------------------------- | |
c801d85f | 31 | |
cd0b1709 | 32 | enum wxdate_format_type |
c801d85f | 33 | { |
cd0b1709 VZ |
34 | wxMDY, |
35 | wxDAY, | |
36 | wxMONTH, | |
37 | wxFULL, | |
38 | wxEUROPEAN | |
39 | }; | |
c30aaf75 | 40 | |
cd0b1709 VZ |
41 | enum // wxdate_format_flags |
42 | { | |
43 | wxNO_CENTURY = 0x02, | |
44 | wxDATE_ABBR = 0x04 | |
45 | }; | |
c30aaf75 | 46 | |
cd0b1709 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // wxDate | |
49 | // ---------------------------------------------------------------------------- | |
c30aaf75 | 50 | |
cd0b1709 VZ |
51 | class WXDLLEXPORT wxDate : public wxObject |
52 | { | |
c30aaf75 | 53 | public: |
cd0b1709 VZ |
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); } | |
d84afea9 | 58 | wxDate(const wxDate &date) : wxObject() { *this = date; } |
cd0b1709 VZ |
59 | |
60 | wxDate(const wxDateTime& dt) { Init(); m_date = dt; } | |
c801d85f | 61 | |
ce3ed50d | 62 | #ifndef __SALFORDC__ |
cd0b1709 | 63 | operator wxString() const { return FormatDate(); } |
ce3ed50d JS |
64 | #endif |
65 | ||
cd0b1709 VZ |
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 | } | |
c801d85f | 72 | |
cd0b1709 | 73 | void operator=(const wxString& dat) { (void)m_date.ParseDate(dat); } |
c801d85f | 74 | |
cd0b1709 VZ |
75 | wxDate operator+(long i) { return wxDate(GetJulianDate() + i); } |
76 | wxDate operator+(int i) { return wxDate(GetJulianDate() + (long)i); } | |
c801d85f | 77 | |
cd0b1709 VZ |
78 | wxDate operator-(long i) { return wxDate(GetJulianDate() - i); } |
79 | wxDate operator-(int i) { return wxDate(GetJulianDate() - (long)i); } | |
c801d85f | 80 | |
cd0b1709 VZ |
81 | long operator-(const wxDate &dt) const |
82 | { return GetJulianDate() - dt.GetJulianDate(); } | |
c801d85f | 83 | |
479cd5de VZ |
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; } | |
c801d85f | 86 | |
cd0b1709 VZ |
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; } | |
d851f77e | 91 | |
38830220 | 92 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 93 | friend wxSTD ostream WXDLLEXPORT & operator <<(wxSTD ostream &os, const wxDate &dt) |
cd0b1709 | 94 | { return os << dt.FormatDate().mb_str(); } |
38830220 | 95 | #endif |
c801d85f | 96 | |
cd0b1709 VZ |
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() | |
75399efb | 122 | { m_date = wxDateTime::Today(); return (wxDate&)*this; } |
cd0b1709 | 123 | wxDate &Set(long lJulian) |
75399efb | 124 | { m_date.Set((double)(lJulian + 0.5)); return (wxDate&)*this; } |
cd0b1709 | 125 | wxDate &Set(int nMonth, int nDay, int nYear) |
68ee7c47 | 126 | { m_date.Set(nDay, (wxDateTime::Month)nMonth, nYear); return *this; } |
cd0b1709 VZ |
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 | |
f6bcfd97 | 164 | wxDate GetMonthStart() const { return(wxDate(GetMonth()-1, 1, GetYear())); } |
cd0b1709 | 165 | // Last Date Of Month |
f6bcfd97 | 166 | wxDate GetMonthEnd() const { return wxDate(GetMonth(), 1, GetYear())-1; } |
cd0b1709 VZ |
167 | |
168 | // eg. 1992 | |
169 | int GetYear() const { return m_date.GetYear(); } | |
170 | // First Date Of Year | |
f6bcfd97 | 171 | wxDate GetYearStart() const { return wxDate(0, 1, GetYear()); } |
cd0b1709 | 172 | // Last Date Of Year |
f6bcfd97 | 173 | wxDate GetYearEnd() const { return wxDate(0, 1, GetYear()+1) - 1; } |
cd0b1709 VZ |
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 | { | |
9c2882d9 | 182 | wxDate prev = *this; |
cd0b1709 | 183 | int dow = GetDayOfWeek(); |
9c2882d9 VZ |
184 | prev -= dayOfWeek > dow ? 7 - (dayOfWeek - dow) : dow - dayOfWeek; |
185 | ||
186 | return prev; | |
cd0b1709 VZ |
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 | } | |
c801d85f | 211 | |
cd0b1709 VZ |
212 | protected: |
213 | void Init() { m_displayFormat = wxMDY; m_displayOptions = 0; } | |
c801d85f | 214 | |
cd0b1709 VZ |
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 | |
c801d85f | 221 | |
cd0b1709 VZ |
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 | |
c801d85f | 226 | |
cd0b1709 VZ |
227 | private: |
228 | wxDateTime m_date; | |
c801d85f | 229 | |
cd0b1709 VZ |
230 | int m_displayFormat; |
231 | int m_displayOptions; | |
c30aaf75 | 232 | |
cd0b1709 VZ |
233 | private: |
234 | DECLARE_DYNAMIC_CLASS(wxDate) | |
c801d85f KB |
235 | }; |
236 | ||
cd0b1709 VZ |
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 | ||
c30aaf75 | 254 | #endif // wxUSE_TIMEDATE |
c801d85f | 255 | #endif |
34138703 | 256 | // _WX_DATE_H_ |