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

source code of boost/boost/date_time/date.hpp