]> git.saurik.com Git - wxWidgets.git/blame - include/wx/date.h
- Include wxCocoa specific header
[wxWidgets.git] / include / wx / date.h
CommitLineData
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$
371a5b4e
JS
9// Copyright: Julian Smart, Steve Marcus, Eric Simon, Chris Hill,
10// Charles D. Price
c30aaf75 11// Licence: wxWindows licence
cd0b1709 12///////////////////////////////////////////////////////////////////////////////
c801d85f 13
34138703
JS
14#ifndef _WX_DATE_H_
15#define _WX_DATE_H_
c801d85f 16
af49c4b8 17#if defined(__GNUG__) && !defined(__APPLE__)
cd0b1709 18 #pragma interface "date.h"
c801d85f
KB
19#endif
20
cd0b1709 21#include "wx/defs.h"
c801d85f 22
9838df2c 23#if wxUSE_TIMEDATE
c801d85f 24
cd0b1709
VZ
25#include "wx/object.h"
26#include "wx/string.h"
27#include "wx/datetime.h"
c801d85f 28
cd0b1709
VZ
29// ----------------------------------------------------------------------------
30// constants
31// ----------------------------------------------------------------------------
c801d85f 32
cd0b1709 33enum wxdate_format_type
c801d85f 34{
cd0b1709
VZ
35 wxMDY,
36 wxDAY,
37 wxMONTH,
38 wxFULL,
39 wxEUROPEAN
40};
c30aaf75 41
cd0b1709
VZ
42enum // wxdate_format_flags
43{
44 wxNO_CENTURY = 0x02,
45 wxDATE_ABBR = 0x04
46};
c30aaf75 47
cd0b1709
VZ
48// ----------------------------------------------------------------------------
49// wxDate
50// ----------------------------------------------------------------------------
c30aaf75 51
cd0b1709
VZ
52class WXDLLEXPORT wxDate : public wxObject
53{
c30aaf75 54public:
cd0b1709
VZ
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); }
d84afea9 59 wxDate(const wxDate &date) : wxObject() { *this = date; }
cd0b1709
VZ
60
61 wxDate(const wxDateTime& dt) { Init(); m_date = dt; }
c801d85f 62
ce3ed50d 63#ifndef __SALFORDC__
cd0b1709 64 operator wxString() const { return FormatDate(); }
ce3ed50d
JS
65#endif
66
cd0b1709
VZ
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 }
c801d85f 73
cd0b1709 74 void operator=(const wxString& dat) { (void)m_date.ParseDate(dat); }
c801d85f 75
cd0b1709
VZ
76 wxDate operator+(long i) { return wxDate(GetJulianDate() + i); }
77 wxDate operator+(int i) { return wxDate(GetJulianDate() + (long)i); }
c801d85f 78
cd0b1709
VZ
79 wxDate operator-(long i) { return wxDate(GetJulianDate() - i); }
80 wxDate operator-(int i) { return wxDate(GetJulianDate() - (long)i); }
c801d85f 81
cd0b1709
VZ
82 long operator-(const wxDate &dt) const
83 { return GetJulianDate() - dt.GetJulianDate(); }
c801d85f 84
479cd5de
VZ
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; }
c801d85f 87
cd0b1709
VZ
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; }
d851f77e 92
38830220 93#if wxUSE_STD_IOSTREAM
dd107c50 94 friend wxSTD ostream WXDLLEXPORT & operator <<(wxSTD ostream &os, const wxDate &dt)
cd0b1709 95 { return os << dt.FormatDate().mb_str(); }
38830220 96#endif
c801d85f 97
cd0b1709
VZ
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()
75399efb 123 { m_date = wxDateTime::Today(); return (wxDate&)*this; }
cd0b1709 124 wxDate &Set(long lJulian)
75399efb 125 { m_date.Set((double)(lJulian + 0.5)); return (wxDate&)*this; }
cd0b1709 126 wxDate &Set(int nMonth, int nDay, int nYear)
68ee7c47 127 { m_date.Set(nDay, (wxDateTime::Month)nMonth, nYear); return *this; }
cd0b1709
VZ
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
f6bcfd97 165 wxDate GetMonthStart() const { return(wxDate(GetMonth()-1, 1, GetYear())); }
cd0b1709 166 // Last Date Of Month
f6bcfd97 167 wxDate GetMonthEnd() const { return wxDate(GetMonth(), 1, GetYear())-1; }
cd0b1709
VZ
168
169 // eg. 1992
170 int GetYear() const { return m_date.GetYear(); }
171 // First Date Of Year
f6bcfd97 172 wxDate GetYearStart() const { return wxDate(0, 1, GetYear()); }
cd0b1709 173 // Last Date Of Year
f6bcfd97 174 wxDate GetYearEnd() const { return wxDate(0, 1, GetYear()+1) - 1; }
cd0b1709
VZ
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 {
9c2882d9 183 wxDate prev = *this;
cd0b1709 184 int dow = GetDayOfWeek();
9c2882d9
VZ
185 prev -= dayOfWeek > dow ? 7 - (dayOfWeek - dow) : dow - dayOfWeek;
186
187 return prev;
cd0b1709
VZ
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 }
c801d85f 212
cd0b1709
VZ
213protected:
214 void Init() { m_displayFormat = wxMDY; m_displayOptions = 0; }
c801d85f 215
cd0b1709
VZ
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
c801d85f 222
cd0b1709
VZ
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
c801d85f 227
cd0b1709
VZ
228private:
229 wxDateTime m_date;
c801d85f 230
cd0b1709
VZ
231 int m_displayFormat;
232 int m_displayOptions;
c30aaf75 233
cd0b1709
VZ
234private:
235 DECLARE_DYNAMIC_CLASS(wxDate)
c801d85f
KB
236};
237
cd0b1709
VZ
238// ----------------------------------------------------------------------------
239// global functions
240// ----------------------------------------------------------------------------
241
242inline bool WXDLLEXPORT operator <(const wxDate &dt1, const wxDate &dt2)
243 { return dt1.GetJulianDate() < dt2.GetJulianDate(); }
244inline bool WXDLLEXPORT operator <=(const wxDate &dt1, const wxDate &dt2)
245 { return dt1.GetJulianDate() <= dt2.GetJulianDate(); }
246inline bool WXDLLEXPORT operator >(const wxDate &dt1, const wxDate &dt2)
247 { return dt1.GetJulianDate() > dt2.GetJulianDate(); }
248inline bool WXDLLEXPORT operator >=(const wxDate &dt1, const wxDate &dt2)
249 { return dt1.GetJulianDate() >= dt2.GetJulianDate(); }
250inline bool WXDLLEXPORT operator ==(const wxDate &dt1, const wxDate &dt2)
251 { return dt1.GetJulianDate() == dt2.GetJulianDate(); }
252inline bool WXDLLEXPORT operator !=(const wxDate &dt1, const wxDate &dt2)
253 { return dt1.GetJulianDate() != dt2.GetJulianDate(); }
254
c30aaf75 255#endif // wxUSE_TIMEDATE
c801d85f 256#endif
34138703 257 // _WX_DATE_H_