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