📄 tdate.tex
字号:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Name: tdate.tex%% Purpose: wxDateTime and related classes overview%% Author: Vadim Zeitlin%% Modified by:%% Created: 07.03.00%% RCS-ID: $Id: tdate.tex,v 1.9 2005/09/17 12:53:06 JS Exp $%% Copyright: (c) Vadim Zeitlin%% License: wxWindows license%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\section{Date and time classes overview}\label{wxdatetimeoverview}Classes: \helpref{wxDateTime}{wxdatetime}, \helpref{wxDateSpan}{wxdatespan}, \helpref{wxTimeSpan}{wxtimespan}, \helpref{wxCalendarCtrl}{wxcalendarctrl}\subsection{Introduction}\label{introductiontowxdatetime}wxWidgets provides a set of powerful classes to work with dates and times. Someof the supported features of \helpref{wxDateTime}{wxdatetime} class are:\twocolwidtha{7cm}\begin{twocollist}\itemsep=0pt\twocolitem{Wide range}{The range of supported dates goes from about 4714 B.C. tosome 480 million years in the future.}\twocolitem{Precision}{Not using floating point calculations anywhere ensures thatthe date calculations don't suffer from rounding errors.}\twocolitem{Many features}{Not only all usual calculations with dates are supported,but also more exotic week and year day calculations, work day testing, standardastronomical functions, conversion to and from strings in either strict or freeformat.}\twocolitem{Efficiency}{Objects of wxDateTime are small (8 bytes) and working withthem is fast}\end{twocollist}\subsection{All date/time classes at a glance}\label{alldatetimeclasses}There are 3 main classes declared in {\tt <wx/datetime.h>}: except \helpref{wxDateTime}{wxdatetime} itself which represents an absolutemoment in time, there are also two classes - \helpref{wxTimeSpan}{wxtimespan} and \helpref{wxDateSpan}{wxdatespan} - whichrepresent the intervals of time.There are also helper classes which are used together with wxDateTime: \helpref{wxDateTimeHolidayAuthority}{wxdatetimeholidayauthority} which is usedto determine whether a given date is a holiday or not and \helpref{wxDateTimeWorkDays}{wxdatetimeworkdays} which is a derivation of thisclass for which (only) Saturdays and Sundays are the holidays. See more aboutthese classes in the discussion of the \helpref{holidays}{tdateholidays}.Finally, in other parts of this manual you may find mentions of wxDate andwxTime classes. \helpref{These classes}{tdatecompatibility} are obsolete andsuperseded by wxDateTime.\subsection{wxDateTime characteristics}\label{wxdatetimecharacteristics}\helpref{wxDateTime}{wxdatetime} stores the time as a signed number ofmilliseconds since the Epoch which is fixed, by convention, to Jan 1, 1970 -however this is not visible to the class users (in particular, dates prior tothe Epoch are handled just as well (or as bad) as the dates after it). But itdoes mean that the best resolution which can be achieved with this class is 1millisecond.The size of wxDateTime object is 8 bytes because it is represented as a 64 bitinteger. The resulting range of supported dates is thus approximatively 580million years, but due to the current limitations in the Gregorian calendarsupport, only dates from Nov 24, 4714BC are supported (this is subject tochange if there is sufficient interest in doing it).Finally, the internal representation is time zone independent (always in GMT)and the time zones only come into play when a date is broken intoyear/month/day components. See more about \helpref{timezones}{tdatetimezones} below.Currently, the only supported calendar is Gregorian one (which is used evenfor the dates prior to the historic introduction of this calendar which wasfirst done on Oct 15, 1582 but is, generally speaking, country, and evenregion, dependent). Future versions will probably have Julian calendar supportas well and support for other calendars (Maya, Hebrew, Chinese...) is notruled out.\subsection{Difference between wxDateSpan and wxTimeSpan}\label{dateandtimespansdifference}While there is only one logical way to represent an absolute moment in thetime (and hence only one wxDateTime class), there are at least two methods todescribe a time interval.First, there is the direct and self-explaining way implemented by \helpref{wxTimeSpan}{wxtimespan}: it is just a difference in millisecondsbetween two moments in time. Adding or subtracting such an interval towxDateTime is always well-defined and is a fast operation.But in the daily life other, calendar-dependent time interval specifications areused. For example, `one month later' is commonly used. However, it is clearthat this is not the same as wxTimeSpan of $60*60*24*31$ seconds because `onemonth later' Feb 15 is Mar 15 and not Mar 17 or Mar 16 (depending on whetherthe year is leap or not).This is why there is another class for representing such intervals called \helpref{wxDateSpan}{wxdatespan}. It handles these sort of operations in themost natural way possible, but note that manipulating with intervals ofthis kind is not always well-defined. Consider, for example, Jan 31 + `1month': this will give Feb 28 (or 29), i.e. the last day of February and notthe non-existent Feb 31. Of course, this is what is usually wanted, but youstill might be surprised to notice that now subtracting back the sameinterval from Feb 28 will result in Jan 28 and {\bf not} Jan 31 we startedwith!So, unless you plan to implement some kind of natural language parsing in theprogram, you should probably use wxTimeSpan instead of wxDateSpan (which isalso more efficient). However, wxDateSpan may be very useful in situationswhen you do need to understand what `in a month' means (of course, it isjust {\tt wxDateTime::Now() + wxDateSpan::Month()}).\subsection{Date arithmetics}\label{tdatearithm}Many different operations may be performed with the dates, however not all ofthem make sense. For example, multiplying a date by a number is an invalidoperation, even though multiplying either of the time span classes by a numberis perfectly valid.Here is what can be done:\begin{twocollist}\itemsep=0pt\twocolitem{{\bf Addition}}{a wxTimeSpan or wxDateSpan can be added to wxDateTimeresulting in a new wxDateTime object and also 2 objects of the same span classcan be added together giving another object of the same class.}\twocolitem{{\bf Subtraction}}{the same types of operations as above areallowed and, additionally, a difference between two wxDateTime objects can betaken and this will yield wxTimeSpan.}\twocolitem{{\bf Multiplication}}{a wxTimeSpan or wxDateSpan object can bemultiplied by an integer number resulting in an object of the same type.}\twocolitem{{\bf Unary minus}}{a wxTimeSpan or wxDateSpan object may finally benegated giving an interval of the same magnitude but of opposite timedirection.}\end{twocollist}For all these operations there are corresponding global (overloaded) operatorsand also member functions which are synonyms for them: Add(), Subtract() andMultiply(). Unary minus as well as composite assignment operations (like $+=$)are only implemented as members and Neg() is the synonym for unary minus.\subsection{Time zone considerations}\label{tdatetimezones}Although the time is always stored internally in GMT, you will usually work inthe local time zone. Because of this, all wxDateTime constructors and setterswhich take the broken down date assume that these values are for the localtime zone. Thus, {\tt wxDateTime(1, wxDateTime::Jan, 1970)} will notcorrespond to the wxDateTime Epoch unless you happen to live in the UK.All methods returning the date components (year, month, day, hour, minute,second...) will also return the correct values for the local time zone bydefault, so, generally, doing the natural things will lead to natural andcorrect results.If you only want to do this, you may safely skip the rest of this section.However, if you want to work with different time zones, you should read it tothe end.In this (rare) case, you are still limited to the local time zone whenconstructing wxDateTime objects, i.e. there is no way to construct awxDateTime corresponding to the given date in, say, Pacific Standard Time.To do it, you will need to call \helpref{ToTimezone}{wxdatetimetotimezone} or \helpref{MakeTimezone}{wxdatetimemaketimezone} methods to adjust the date forthe target time zone. There are also special versions of these functions \helpref{ToUTC}{wxdatetimetoutc} and \helpref{MakeUTC}{wxdatetimemakeutc} forthe most common case - when the date should be constructed in UTC.You also can just retrieve the value for some time zone without converting theobject to it first. For this you may pass TimeZone argument to any of themethods which are affected by the time zone (all methods getting datecomponents and the date formatting ones, for example). In particular, theFormat() family of methods accepts a TimeZone parameter and this allows tosimply print time in any time zone.To see how to do it, the last issue to address is how to construct a TimeZoneobject which must be passed to all these methods. First of all, you may constructit manually by specifying the time zone offset in seconds from GMT, butusually you will just use one of the \helpref{symbolic time zone names}{wxdatetime} andlet the conversion constructor do the job.I.e. you would just write\begin{verbatim}wxDateTime dt(...whatever...);printf("The time is %s in local time zone", dt.FormatTime().c_str());printf("The time is %s in GMT", dt.FormatTime(wxDateTime::GMT).c_str());\end{verbatim}\subsection{Daylight saving time (DST)}\label{tdatedst}DST (a.k.a. `summer time') handling is always a delicate task which is betterleft to the operating system which is supposed to be configured by theadministrator to behave correctly. Unfortunately, when doing calculations withdate outside of the range supported by the standard library, we are forced todeal with these issues ourselves.Several functions are provided to calculate the beginning and end of DST inthe given year and to determine whether it is in effect at the given moment ornot, but they should not be considered as absolutely correct because, first ofall, they only work more or less correctly for only a handful of countries(any information about other ones appreciated!) and even for them the rulesmay perfectly well change in the future.The time zone handling \helpref{methods}{tdatetimezones} use these functionstoo, so they are subject to the same limitations.% is this really needed? \subsection{Conversion to/from text}\subsection{wxDateTime and Holidays}\label{tdateholidays}TODO.\subsection{Compatibility}\label{tdatecompatibility}The old classes for date/time manipulations ported from wxWidgets version 1.xxare still included but are reimplemented in terms of wxDateTime. However, usingthem is strongly discouraged because they have a few quirks/bugs and were not`Y2K' compatible.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -