source: NonGTP/Boost/boost/date_time/date.hpp @ 857

Revision 857, 6.3 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]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 or http://www.boost.org/LICENSE-1.0)
8 * Author: Jeff Garland, Bart Garst
9 * $Date: 2004/01/11 17:41:21 $
10 */
11
12#include "boost/date_time/year_month_day.hpp"
13#include "boost/date_time/special_defs.hpp"
14#include "boost/operators.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      date_rep_type val = date_rep_type(days_) - date_rep_type(d.days_);
142      return duration_type(val.as_number());
143    }
144   
145    date_type operator-(const duration_type& dd) const
146    {
147      if(dd.is_special())
148      {
149        return date_type(date_rep_type(days_) - dd.get_rep());
150      }
151      return date_type(date_rep_type(days_) - dd.days());
152    }
153    date_type operator-=(const duration_type& dd)
154    {
155      *this = *this - dd;
156      return date_type(days_);
157    }
158    date_rep_type day_count() const
159    {
160      return days_;
161    };
162    //allow internal access from operators
163    date_type operator+(const duration_type& dd) const
164    {
165      if(dd.is_special())
166      {
167        return date_type(date_rep_type(days_) + dd.get_rep());
168      }
169      return date_type(date_rep_type(days_) + dd.days());
170    }
171    date_type operator+=(const duration_type& dd)
172    {
173      *this = *this + dd;
174      return date_type(days_);
175    }
176
177    //see reference
178  protected:
179    /*! This is a private constructor which allows for the creation of new
180      dates.  It is not exposed to users since that would require class
181      users to understand the inner workings of the date class.
182    */
183    explicit date(date_int_type days) : days_(days) {};
184    explicit date(date_rep_type days) : days_(days.as_number()) {};
185    date_int_type days_;
186   
187  };
188
189
190
191 
192} } // namespace date_time
193
194
195
196
197#endif
Note: See TracBrowser for help on using the repository browser.