date and time classes overviewclasses: wxdatetime, wxdatespan, wxtimespan, wxcalendarctrl
introduction
introductionwxwidgets provides a set of powerful classes to work with dates and times. some of the supported features of wxdatetime class are:
all date/time classes at a glancethere are 3 main classes declared in <wx/datetime.h>: except wxdatetime itself which represents an absolute moment in time, there are also two classes - wxtimespan and wxdatespan - which represent the intervals of time. there are also helper classes which are used together with wxdatetime: wxdatetimeholidayauthority which is used to determine whether a given date is a holiday or not and wxdatetimeworkdays which is a derivation of this class for which (only) saturdays and sundays are the holidays. see more about these classes in the discussion of the holidays. finally, in other parts of this manual you may find mentions of wxdate and wxtime classes. these classes are obsolete and superseded by wxdatetime.
wxdatetime characteristicswxdatetime stores the time as a signed number of milliseconds 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 to the epoch are handled just as well (or as bad) as the dates after it). but it does mean that the best resolution which can be achieved with this class is 1 millisecond. the size of wxdatetime object is 8 bytes because it is represented as a 64 bit integer. the resulting range of supported dates is thus approximatively 580 million years, but due to the current limitations in the gregorian calendar support, only dates from nov 24, 4714bc are supported (this is subject to change 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 into year/month/day components. see more about timezones below. currently, the only supported calendar is gregorian one (which is used even for the dates prior to the historic introduction of this calendar which was first done on oct 15, 1582 but is, generally speaking, country, and even region, dependent). future versions will probably have julian calendar support as well and support for other calendars (maya, hebrew, chinese...) is not ruled out.
difference between wxdatespan and wxtimespanwhile there is only one logical way to represent an absolute moment in the time (and hence only one wxdatetime class), there are at least two methods to describe a time interval. first, there is the direct and self-explaining way implemented by wxtimespan: it is just a difference in milliseconds between two moments in time. adding or subtracting such an interval to wxdatetime is always well-defined and is a fast operation. but in the daily life other, calendar-dependent time interval specifications are used. for example, 'one month later' is commonly used. however, it is clear that this is not the same as wxtimespan of 60*60*24*31 seconds because 'one month later' feb 15 is mar 15 and not mar 17 or mar 16 (depending on whether the year is leap or not). this is why there is another class for representing such intervals called wxdatespan. it handles these sort of operations in the most natural way possible, but note that manipulating with intervals of this kind is not always well-defined. consider, for example, jan 31 + '1 month': this will give feb 28 (or 29), i.e. the last day of february and not the non-existent feb 31. of course, this is what is usually wanted, but you still might be surprised to notice that now subtracting back the same interval from feb 28 will result in jan 28 and not jan 31 we started with! so, unless you plan to implement some kind of natural language parsing in the program, you should probably use wxtimespan instead of wxdatespan (which is also more efficient). however, wxdatespan may be very useful in situations when you do need to understand what 'in a month' means (of course, it is just wxdatetime::now() + wxdatespan::month()).
date arithmeticsmany different operations may be performed with the dates, however not all of them make sense. for example, multiplying a date by a number is an invalid operation, even though multiplying either of the time span classes by a number is perfectly valid. here is what can be done:
for all these operations there are corresponding global (overloaded) operators and also member functions which are synonyms for them: add(), subtract() and multiply(). unary minus as well as composite assignment operations (like +=) are only implemented as members and neg() is the synonym for unary minus.
time zone considerationsalthough the time is always stored internally in gmt, you will usually work in the local time zone. because of this, all wxdatetime constructors and setters which take the broken down date assume that these values are for the local time zone. thus, wxdatetime(1, wxdatetime::jan, 1970) will not correspond 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 by default, so, generally, doing the natural things will lead to natural and correct 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 to the end. in this (rare) case, you are still limited to the local time zone when constructing wxdatetime objects, i.e. there is no way to construct a wxdatetime corresponding to the given date in, say, pacific standard time. to do it, you will need to call totimezone or maketimezone methods to adjust the date for the target time zone. there are also special versions of these functions toutc and makeutc for the most common case - when the date should be constructed in utc. you also can just retrieve the value for some time zone without converting the object to it first. for this you may pass timezone argument to any of the methods which are affected by the time zone (all methods getting date components and the date formatting ones, for example). in particular, the format() family of methods accepts a timezone parameter and this allows to simply print time in any time zone. to see how to do it, the last issue to address is how to construct a timezone object which must be passed to all these methods. first of all, you may construct it manually by specifying the time zone offset in seconds from gmt, but usually you will just use one of the symbolic time zone names and let the conversion constructor do the job. i.e. you would just write
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()); daylight saving time (dst)dst (a.k.a. 'summer time') handling is always a delicate task which is better left to the operating system which is supposed to be configured by the administrator to behave correctly. unfortunately, when doing calculations with date outside of the range supported by the standard library, we are forced to deal with these issues ourselves. several functions are provided to calculate the beginning and end of dst in the given year and to determine whether it is in effect at the given moment or not, but they should not be considered as absolutely correct because, first of all, they only work more or less correctly for only a handful of countries (any information about other ones appreciated!) and even for them the rules may perfectly well change in the future. the time zone handling methods use these functions too, so they are subject to the same limitations.
wxdatetime and holidaystodo.
compatibilitythe old classes for date/time manipulations ported from wxwidgets version 1.xx are still included but are reimplemented in terms of wxdatetime. however, using them is strongly discouraged because they have a few quirks/bugs and were not 'y2k' compatible.
|