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