]>
Commit | Line | Data |
---|---|---|
15b6757b FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: datetime | |
3 | // Purpose: topic overview | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /*! | |
36c9828f | 10 | |
15b6757b | 11 | @page datetime_overview Date and time classes overview |
36c9828f | 12 | |
15b6757b FM |
13 | Classes: #wxDateTime, #wxDateSpan, #wxTimeSpan, #wxCalendarCtrl |
14 | #Introduction | |
15 | @ref alldatetimeclasses_overview | |
16 | @ref datetimecharacteristics_overview | |
17 | @ref dateandtimespansdifference_overview | |
18 | @ref tdatearithm_overview | |
19 | @ref tdatetimezones_overview | |
20 | #Daylight saving time (DST) | |
21 | @ref tdateholidays_overview | |
22 | #Compatibility | |
36c9828f FM |
23 | |
24 | ||
15b6757b | 25 | @section introductiontowxdatetime Introduction |
36c9828f | 26 | |
15b6757b FM |
27 | wxWidgets provides a set of powerful classes to work with dates and times. Some |
28 | of the supported features of #wxDateTime class are: | |
36c9828f FM |
29 | |
30 | ||
31 | ||
32 | ||
33 | ||
34 | ||
35 | ||
15b6757b | 36 | Wide range |
36c9828f FM |
37 | |
38 | ||
39 | ||
40 | ||
15b6757b FM |
41 | The range of supported dates goes from about 4714 B.C. to |
42 | some 480 million years in the future. | |
36c9828f FM |
43 | |
44 | ||
45 | ||
46 | ||
47 | ||
15b6757b | 48 | Precision |
36c9828f FM |
49 | |
50 | ||
51 | ||
52 | ||
15b6757b FM |
53 | Not using floating point calculations anywhere ensures that |
54 | the date calculations don't suffer from rounding errors. | |
36c9828f FM |
55 | |
56 | ||
57 | ||
58 | ||
59 | ||
15b6757b | 60 | Many features |
36c9828f FM |
61 | |
62 | ||
63 | ||
64 | ||
15b6757b FM |
65 | Not only all usual calculations with dates are supported, |
66 | but also more exotic week and year day calculations, work day testing, standard | |
67 | astronomical functions, conversion to and from strings in either strict or free | |
68 | format. | |
36c9828f FM |
69 | |
70 | ||
71 | ||
72 | ||
73 | ||
15b6757b | 74 | Efficiency |
36c9828f FM |
75 | |
76 | ||
77 | ||
78 | ||
15b6757b FM |
79 | Objects of wxDateTime are small (8 bytes) and working with |
80 | them is fast | |
36c9828f FM |
81 | |
82 | ||
83 | ||
84 | ||
85 | ||
86 | ||
15b6757b | 87 | @section alldatetimeclasses All date/time classes at a glance |
36c9828f FM |
88 | |
89 | There are 3 main classes declared in @c wx/datetime.h: except | |
15b6757b | 90 | #wxDateTime itself which represents an absolute |
36c9828f | 91 | moment in time, there are also two classes - |
15b6757b FM |
92 | #wxTimeSpan and #wxDateSpan - which |
93 | represent the intervals of time. | |
36c9828f | 94 | There are also helper classes which are used together with wxDateTime: |
15b6757b | 95 | #wxDateTimeHolidayAuthority which is used |
36c9828f | 96 | to determine whether a given date is a holiday or not and |
15b6757b FM |
97 | #wxDateTimeWorkDays which is a derivation of this |
98 | class for which (only) Saturdays and Sundays are the holidays. See more about | |
99 | these classes in the discussion of the #holidays. | |
100 | Finally, in other parts of this manual you may find mentions of wxDate and | |
101 | wxTime classes. @ref tdatecompatibility_overview are obsolete and | |
102 | superseded by wxDateTime. | |
36c9828f | 103 | |
15b6757b | 104 | @section wxdatetimecharacteristics wxDateTime characteristics |
36c9828f | 105 | |
15b6757b FM |
106 | #wxDateTime stores the time as a signed number of |
107 | milliseconds since the Epoch which is fixed, by convention, to Jan 1, 1970 - | |
108 | however this is not visible to the class users (in particular, dates prior to | |
109 | the Epoch are handled just as well (or as bad) as the dates after it). But it | |
110 | does mean that the best resolution which can be achieved with this class is 1 | |
111 | millisecond. | |
112 | The size of wxDateTime object is 8 bytes because it is represented as a 64 bit | |
113 | integer. The resulting range of supported dates is thus approximatively 580 | |
114 | million years, but due to the current limitations in the Gregorian calendar | |
115 | support, only dates from Nov 24, 4714BC are supported (this is subject to | |
116 | change if there is sufficient interest in doing it). | |
117 | Finally, the internal representation is time zone independent (always in GMT) | |
118 | and the time zones only come into play when a date is broken into | |
36c9828f | 119 | year/month/day components. See more about #timezones |
15b6757b FM |
120 | below. |
121 | Currently, the only supported calendar is Gregorian one (which is used even | |
122 | for the dates prior to the historic introduction of this calendar which was | |
123 | first done on Oct 15, 1582 but is, generally speaking, country, and even | |
124 | region, dependent). Future versions will probably have Julian calendar support | |
125 | as well and support for other calendars (Maya, Hebrew, Chinese...) is not | |
126 | ruled out. | |
36c9828f | 127 | |
15b6757b | 128 | @section dateandtimespansdifference Difference between wxDateSpan and wxTimeSpan |
36c9828f | 129 | |
15b6757b FM |
130 | While there is only one logical way to represent an absolute moment in the |
131 | time (and hence only one wxDateTime class), there are at least two methods to | |
132 | describe a time interval. | |
36c9828f | 133 | First, there is the direct and self-explaining way implemented by |
15b6757b FM |
134 | #wxTimeSpan: it is just a difference in milliseconds |
135 | between two moments in time. Adding or subtracting such an interval to | |
136 | wxDateTime is always well-defined and is a fast operation. | |
137 | But in the daily life other, calendar-dependent time interval specifications are | |
138 | used. For example, 'one month later' is commonly used. However, it is clear | |
139 | that this is not the same as wxTimeSpan of 60*60*24*31 seconds because 'one | |
140 | month later' Feb 15 is Mar 15 and not Mar 17 or Mar 16 (depending on whether | |
141 | the year is leap or not). | |
36c9828f | 142 | This is why there is another class for representing such intervals called |
15b6757b FM |
143 | #wxDateSpan. It handles these sort of operations in the |
144 | most natural way possible, but note that manipulating with intervals of | |
145 | this kind is not always well-defined. Consider, for example, Jan 31 + '1 | |
146 | month': this will give Feb 28 (or 29), i.e. the last day of February and not | |
147 | the non-existent Feb 31. Of course, this is what is usually wanted, but you | |
148 | still might be surprised to notice that now subtracting back the same | |
149 | interval from Feb 28 will result in Jan 28 and @b not Jan 31 we started | |
150 | with! | |
151 | So, unless you plan to implement some kind of natural language parsing in the | |
152 | program, you should probably use wxTimeSpan instead of wxDateSpan (which is | |
153 | also more efficient). However, wxDateSpan may be very useful in situations | |
154 | when you do need to understand what 'in a month' means (of course, it is | |
155 | just @c wxDateTime::Now() + wxDateSpan::Month()). | |
36c9828f | 156 | |
15b6757b | 157 | @section tdatearithm Date arithmetics |
36c9828f | 158 | |
15b6757b FM |
159 | Many different operations may be performed with the dates, however not all of |
160 | them make sense. For example, multiplying a date by a number is an invalid | |
161 | operation, even though multiplying either of the time span classes by a number | |
162 | is perfectly valid. | |
163 | Here is what can be done: | |
36c9828f FM |
164 | |
165 | ||
166 | ||
167 | ||
168 | ||
169 | ||
15b6757b | 170 | @b Addition |
36c9828f FM |
171 | |
172 | ||
173 | ||
174 | ||
15b6757b FM |
175 | a wxTimeSpan or wxDateSpan can be added to wxDateTime |
176 | resulting in a new wxDateTime object and also 2 objects of the same span class | |
177 | can be added together giving another object of the same class. | |
36c9828f FM |
178 | |
179 | ||
180 | ||
181 | ||
182 | ||
15b6757b | 183 | @b Subtraction |
36c9828f FM |
184 | |
185 | ||
186 | ||
187 | ||
15b6757b FM |
188 | the same types of operations as above are |
189 | allowed and, additionally, a difference between two wxDateTime objects can be | |
190 | taken and this will yield wxTimeSpan. | |
36c9828f FM |
191 | |
192 | ||
193 | ||
194 | ||
195 | ||
15b6757b | 196 | @b Multiplication |
36c9828f FM |
197 | |
198 | ||
199 | ||
200 | ||
15b6757b FM |
201 | a wxTimeSpan or wxDateSpan object can be |
202 | multiplied by an integer number resulting in an object of the same type. | |
36c9828f FM |
203 | |
204 | ||
205 | ||
206 | ||
207 | ||
15b6757b | 208 | @b Unary minus |
36c9828f FM |
209 | |
210 | ||
211 | ||
212 | ||
15b6757b FM |
213 | a wxTimeSpan or wxDateSpan object may finally be |
214 | negated giving an interval of the same magnitude but of opposite time | |
215 | direction. | |
36c9828f FM |
216 | |
217 | ||
218 | ||
219 | ||
220 | ||
15b6757b FM |
221 | For all these operations there are corresponding global (overloaded) operators |
222 | and also member functions which are synonyms for them: Add(), Subtract() and | |
223 | Multiply(). Unary minus as well as composite assignment operations (like +=) | |
224 | are only implemented as members and Neg() is the synonym for unary minus. | |
36c9828f | 225 | |
15b6757b | 226 | @section tdatetimezones Time zone considerations |
36c9828f | 227 | |
15b6757b FM |
228 | Although the time is always stored internally in GMT, you will usually work in |
229 | the local time zone. Because of this, all wxDateTime constructors and setters | |
230 | which take the broken down date assume that these values are for the local | |
231 | time zone. Thus, @c wxDateTime(1, wxDateTime::Jan, 1970) will not | |
232 | correspond to the wxDateTime Epoch unless you happen to live in the UK. | |
233 | All methods returning the date components (year, month, day, hour, minute, | |
234 | second...) will also return the correct values for the local time zone by | |
235 | default, so, generally, doing the natural things will lead to natural and | |
236 | correct results. | |
237 | If you only want to do this, you may safely skip the rest of this section. | |
238 | However, if you want to work with different time zones, you should read it to | |
239 | the end. | |
240 | In this (rare) case, you are still limited to the local time zone when | |
241 | constructing wxDateTime objects, i.e. there is no way to construct a | |
242 | wxDateTime corresponding to the given date in, say, Pacific Standard Time. | |
36c9828f | 243 | To do it, you will need to call #ToTimezone or |
15b6757b | 244 | #MakeTimezone methods to adjust the date for |
36c9828f | 245 | the target time zone. There are also special versions of these functions |
15b6757b FM |
246 | #ToUTC and #MakeUTC for |
247 | the most common case - when the date should be constructed in UTC. | |
248 | You also can just retrieve the value for some time zone without converting the | |
249 | object to it first. For this you may pass TimeZone argument to any of the | |
250 | methods which are affected by the time zone (all methods getting date | |
251 | components and the date formatting ones, for example). In particular, the | |
252 | Format() family of methods accepts a TimeZone parameter and this allows to | |
253 | simply print time in any time zone. | |
254 | To see how to do it, the last issue to address is how to construct a TimeZone | |
255 | object which must be passed to all these methods. First of all, you may construct | |
256 | it manually by specifying the time zone offset in seconds from GMT, but | |
257 | usually you will just use one of the @ref datetime_overview and | |
258 | let the conversion constructor do the job. | |
259 | I.e. you would just write | |
36c9828f | 260 | |
15b6757b FM |
261 | @code |
262 | wxDateTime dt(...whatever...); | |
263 | printf("The time is %s in local time zone", dt.FormatTime().c_str()); | |
264 | printf("The time is %s in GMT", dt.FormatTime(wxDateTime::GMT).c_str()); | |
265 | @endcode | |
36c9828f FM |
266 | |
267 | ||
15b6757b | 268 | @section tdatedst Daylight saving time (DST) |
36c9828f | 269 | |
15b6757b FM |
270 | DST (a.k.a. 'summer time') handling is always a delicate task which is better |
271 | left to the operating system which is supposed to be configured by the | |
272 | administrator to behave correctly. Unfortunately, when doing calculations with | |
273 | date outside of the range supported by the standard library, we are forced to | |
274 | deal with these issues ourselves. | |
275 | Several functions are provided to calculate the beginning and end of DST in | |
276 | the given year and to determine whether it is in effect at the given moment or | |
277 | not, but they should not be considered as absolutely correct because, first of | |
278 | all, they only work more or less correctly for only a handful of countries | |
279 | (any information about other ones appreciated!) and even for them the rules | |
280 | may perfectly well change in the future. | |
281 | The time zone handling #methods use these functions | |
282 | too, so they are subject to the same limitations. | |
36c9828f | 283 | |
15b6757b | 284 | @section tdateholidays wxDateTime and Holidays |
36c9828f | 285 | |
15b6757b | 286 | TODO. |
36c9828f | 287 | |
15b6757b | 288 | @section tdatecompatibility Compatibility |
36c9828f | 289 | |
15b6757b FM |
290 | The old classes for date/time manipulations ported from wxWidgets version 1.xx |
291 | are still included but are reimplemented in terms of wxDateTime. However, using | |
292 | them is strongly discouraged because they have a few quirks/bugs and were not | |
293 | 'Y2K' compatible. | |
36c9828f | 294 | |
15b6757b | 295 | */ |
36c9828f FM |
296 | |
297 |