datetime.h 9.9 KB

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