date.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef DATE_TIME_DATE_HPP___
  2. #define DATE_TIME_DATE_HPP___
  3. /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <boost/operators.hpp>
  11. #include <boost/date_time/compiler_config.hpp>
  12. #include <boost/date_time/year_month_day.hpp>
  13. #include <boost/date_time/special_defs.hpp>
  14. namespace boost {
  15. namespace date_time {
  16. //!Representation of timepoint at the one day level resolution.
  17. /*!
  18. The date template represents an interface shell for a date class
  19. that is based on a year-month-day system such as the gregorian
  20. or iso systems. It provides basic operations to enable calculation
  21. and comparisons.
  22. <b>Theory</b>
  23. This date representation fundamentally departs from the C tm struct
  24. approach. The goal for this type is to provide efficient date
  25. operations (add, subtract) and storage (minimize space to represent)
  26. in a concrete class. Thus, the date uses a count internally to
  27. represent a particular date. The calendar parameter defines
  28. the policies for converting the the year-month-day and internal
  29. counted form here. Applications that need to perform heavy
  30. formatting of the same date repeatedly will perform better
  31. by using the year-month-day representation.
  32. Internally the date uses a day number to represent the date.
  33. This is a monotonic time representation. This representation
  34. allows for fast comparison as well as simplifying
  35. the creation of writing numeric operations. Essentially, the
  36. internal day number is like adjusted julian day. The adjustment
  37. is determined by the Epoch date which is represented as day 1 of
  38. the calendar. Day 0 is reserved for negative infinity so that
  39. any actual date is automatically greater than negative infinity.
  40. When a date is constructed from a date or formatted for output,
  41. the appropriate conversions are applied to create the year, month,
  42. day representations.
  43. */
  44. template<class T, class calendar, class duration_type_>
  45. class BOOST_SYMBOL_VISIBLE date : private
  46. boost::less_than_comparable<T
  47. , boost::equality_comparable<T
  48. > >
  49. {
  50. public:
  51. typedef T date_type;
  52. typedef calendar calendar_type;
  53. typedef typename calendar::date_traits_type traits_type;
  54. typedef duration_type_ duration_type;
  55. typedef typename calendar::year_type year_type;
  56. typedef typename calendar::month_type month_type;
  57. typedef typename calendar::day_type day_type;
  58. typedef typename calendar::ymd_type ymd_type;
  59. typedef typename calendar::date_rep_type date_rep_type;
  60. typedef typename calendar::date_int_type date_int_type;
  61. typedef typename calendar::day_of_week_type day_of_week_type;
  62. date(year_type y, month_type m, day_type d)
  63. : days_(calendar::day_number(ymd_type(y, m, d)))
  64. {}
  65. date(const ymd_type& ymd)
  66. : days_(calendar::day_number(ymd))
  67. {}
  68. //let the compiler write copy, assignment, and destructor
  69. year_type year() const
  70. {
  71. ymd_type ymd = calendar::from_day_number(days_);
  72. return ymd.year;
  73. }
  74. month_type month() const
  75. {
  76. ymd_type ymd = calendar::from_day_number(days_);
  77. return ymd.month;
  78. }
  79. day_type day() const
  80. {
  81. ymd_type ymd = calendar::from_day_number(days_);
  82. return ymd.day;
  83. }
  84. day_of_week_type day_of_week() const
  85. {
  86. ymd_type ymd = calendar::from_day_number(days_);
  87. return calendar::day_of_week(ymd);
  88. }
  89. ymd_type year_month_day() const
  90. {
  91. return calendar::from_day_number(days_);
  92. }
  93. bool operator<(const date_type& rhs) const
  94. {
  95. return days_ < rhs.days_;
  96. }
  97. bool operator==(const date_type& rhs) const
  98. {
  99. return days_ == rhs.days_;
  100. }
  101. //! check to see if date is a special value
  102. bool is_special()const
  103. {
  104. return(is_not_a_date() || is_infinity());
  105. }
  106. //! check to see if date is not a value
  107. bool is_not_a_date() const
  108. {
  109. return traits_type::is_not_a_number(days_);
  110. }
  111. //! check to see if date is one of the infinity values
  112. bool is_infinity() const
  113. {
  114. return traits_type::is_inf(days_);
  115. }
  116. //! check to see if date is greater than all possible dates
  117. bool is_pos_infinity() const
  118. {
  119. return traits_type::is_pos_inf(days_);
  120. }
  121. //! check to see if date is greater than all possible dates
  122. bool is_neg_infinity() const
  123. {
  124. return traits_type::is_neg_inf(days_);
  125. }
  126. //! return as a special value or a not_special if a normal date
  127. special_values as_special() const
  128. {
  129. return traits_type::to_special(days_);
  130. }
  131. duration_type operator-(const date_type& d) const
  132. {
  133. if (!this->is_special() && !d.is_special())
  134. {
  135. // The duration underlying type may be wider than the date underlying type.
  136. // Thus we calculate the difference in terms of two durations from some common fixed base date.
  137. typedef typename duration_type::duration_rep_type duration_rep_type;
  138. return duration_type(static_cast< duration_rep_type >(days_) - static_cast< duration_rep_type >(d.days_));
  139. }
  140. else
  141. {
  142. // In this case the difference will be a special value, too
  143. date_rep_type val = date_rep_type(days_) - date_rep_type(d.days_);
  144. return duration_type(val.as_special());
  145. }
  146. }
  147. date_type operator-(const duration_type& dd) const
  148. {
  149. if(dd.is_special())
  150. {
  151. return date_type(date_rep_type(days_) - dd.get_rep());
  152. }
  153. return date_type(date_rep_type(days_) - static_cast<date_int_type>(dd.days()));
  154. }
  155. date_type operator-=(const duration_type& dd)
  156. {
  157. *this = *this - dd;
  158. return date_type(days_);
  159. }
  160. date_rep_type day_count() const
  161. {
  162. return days_;
  163. }
  164. //allow internal access from operators
  165. date_type operator+(const duration_type& dd) const
  166. {
  167. if(dd.is_special())
  168. {
  169. return date_type(date_rep_type(days_) + dd.get_rep());
  170. }
  171. return date_type(date_rep_type(days_) + static_cast<date_int_type>(dd.days()));
  172. }
  173. date_type operator+=(const duration_type& dd)
  174. {
  175. *this = *this + dd;
  176. return date_type(days_);
  177. }
  178. //see reference
  179. protected:
  180. /*! This is a private constructor which allows for the creation of new
  181. dates. It is not exposed to users since that would require class
  182. users to understand the inner workings of the date class.
  183. */
  184. explicit date(date_int_type days) : days_(days) {}
  185. explicit date(date_rep_type days) : days_(days.as_number()) {}
  186. date_int_type days_;
  187. };
  188. } } // namespace date_time
  189. #endif