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