Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:21

0001 #ifndef DATE_TIME_DATE_HPP___
0002 #define DATE_TIME_DATE_HPP___
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 #include <boost/operators.hpp>
0013 #include <boost/date_time/compiler_config.hpp>
0014 #include <boost/date_time/year_month_day.hpp>
0015 #include <boost/date_time/special_defs.hpp>
0016 
0017 namespace boost {
0018 namespace date_time {
0019 
0020   //!Representation of timepoint at the one day level resolution.
0021   /*!
0022     The date template represents an interface shell for a date class
0023     that is based on a year-month-day system such as the gregorian
0024     or ISO 8601 systems.  It provides basic operations to enable calculation
0025     and comparisons.
0026 
0027     <b>Theory</b>
0028 
0029     This date representation fundamentally departs from the C tm struct
0030     approach.  The goal for this type is to provide efficient date
0031     operations (add, subtract) and storage (minimize space to represent)
0032     in a concrete class.  Thus, the date uses a count internally to
0033     represent a particular date.  The calendar parameter defines
0034     the policies for converting the the year-month-day and internal
0035     counted form here.  Applications that need to perform heavy
0036     formatting of the same date repeatedly will perform better
0037     by using the year-month-day representation.
0038 
0039     Internally the date uses a day number to represent the date.
0040     This is a monotonic time representation. This representation
0041     allows for fast comparison as well as simplifying
0042     the creation of writing numeric operations.  Essentially, the
0043     internal day number is like adjusted julian day.  The adjustment
0044     is determined by the Epoch date which is represented as day 1 of
0045     the calendar.  Day 0 is reserved for negative infinity so that
0046     any actual date is automatically greater than negative infinity.
0047     When a date is constructed from a date or formatted for output,
0048     the appropriate conversions are applied to create the year, month,
0049     day representations.
0050   */
0051 
0052 
0053   template<class T, class calendar, class duration_type_>
0054   class BOOST_SYMBOL_VISIBLE date : private
0055        boost::less_than_comparable<T
0056      , boost::equality_comparable<T
0057     > >
0058   {
0059   public:
0060     typedef T date_type;
0061     typedef calendar calendar_type;
0062     typedef typename calendar::date_traits_type traits_type;
0063     typedef duration_type_ duration_type;
0064     typedef typename calendar::year_type year_type;
0065     typedef typename calendar::month_type month_type;
0066     typedef typename calendar::day_type day_type;
0067     typedef typename calendar::ymd_type ymd_type;
0068     typedef typename calendar::date_rep_type date_rep_type;
0069     typedef typename calendar::date_int_type date_int_type;
0070     typedef typename calendar::day_of_week_type day_of_week_type;
0071     BOOST_CXX14_CONSTEXPR date(year_type y, month_type m, day_type d)
0072       : days_(calendar::day_number(ymd_type(y, m, d)))
0073     {}
0074     BOOST_CXX14_CONSTEXPR date(const ymd_type& ymd)
0075       : days_(calendar::day_number(ymd))
0076     {}
0077     //let the compiler write copy, assignment, and destructor
0078     BOOST_CXX14_CONSTEXPR year_type        year() const
0079     {
0080       ymd_type ymd = calendar::from_day_number(days_);
0081       return ymd.year;
0082     }
0083     BOOST_CXX14_CONSTEXPR month_type       month() const
0084     {
0085       ymd_type ymd = calendar::from_day_number(days_);
0086       return ymd.month;
0087     }
0088     BOOST_CXX14_CONSTEXPR day_type         day() const
0089     {
0090       ymd_type ymd = calendar::from_day_number(days_);
0091       return ymd.day;
0092     }
0093     BOOST_CXX14_CONSTEXPR day_of_week_type day_of_week() const
0094     {
0095       ymd_type ymd = calendar::from_day_number(days_);
0096       return calendar::day_of_week(ymd);
0097     }
0098     BOOST_CXX14_CONSTEXPR ymd_type         year_month_day() const
0099     {
0100       return calendar::from_day_number(days_);
0101     }
0102     BOOST_CONSTEXPR bool operator<(const date_type& rhs)  const
0103     {
0104       return days_ < rhs.days_;
0105     }
0106     BOOST_CONSTEXPR bool operator==(const date_type& rhs) const
0107     {
0108       return days_ == rhs.days_;
0109     }
0110     //! check to see if date is a special value
0111     BOOST_CONSTEXPR bool is_special()const
0112     {
0113       return(is_not_a_date() || is_infinity());
0114     }
0115     //! check to see if date is not a value
0116     BOOST_CONSTEXPR bool is_not_a_date()  const
0117     {
0118       return traits_type::is_not_a_number(days_);
0119     }
0120     //! check to see if date is one of the infinity values
0121     BOOST_CONSTEXPR bool is_infinity()  const
0122     {
0123       return traits_type::is_inf(days_);
0124     }
0125     //! check to see if date is greater than all possible dates
0126     BOOST_CONSTEXPR bool is_pos_infinity()  const
0127     {
0128       return traits_type::is_pos_inf(days_);
0129     }
0130     //! check to see if date is greater than all possible dates
0131     BOOST_CONSTEXPR bool is_neg_infinity()  const
0132     {
0133       return traits_type::is_neg_inf(days_);
0134     }
0135     //! return as a special value or a not_special if a normal date
0136     BOOST_CXX14_CONSTEXPR special_values as_special()  const
0137     {
0138       return traits_type::to_special(days_);
0139     }
0140     BOOST_CXX14_CONSTEXPR duration_type operator-(const date_type& d) const
0141     {
0142       if (!this->is_special() && !d.is_special())
0143       {
0144         // The duration underlying type may be wider than the date underlying type.
0145         // Thus we calculate the difference in terms of two durations from some common fixed base date.
0146         typedef typename duration_type::duration_rep_type duration_rep_type;
0147         return duration_type(static_cast< duration_rep_type >(days_) - static_cast< duration_rep_type >(d.days_));
0148       }
0149       else
0150       {
0151         // In this case the difference will be a special value, too
0152         date_rep_type val = date_rep_type(days_) - date_rep_type(d.days_);
0153         return duration_type(val.as_special());
0154       }
0155     }
0156 
0157     BOOST_CXX14_CONSTEXPR date_type operator-(const duration_type& dd) const
0158     {
0159       if(dd.is_special())
0160       {
0161         return date_type(date_rep_type(days_) - dd.get_rep());
0162       }
0163       return date_type(date_rep_type(days_) - static_cast<date_int_type>(dd.days()));
0164     }
0165     BOOST_CXX14_CONSTEXPR date_type operator-=(const duration_type& dd)
0166     {
0167       *this = *this - dd;
0168       return date_type(days_);
0169     }
0170     BOOST_CONSTEXPR date_rep_type day_count() const
0171     {
0172       return days_;
0173     }
0174     //allow internal access from operators
0175     BOOST_CXX14_CONSTEXPR date_type operator+(const duration_type& dd) const
0176     {
0177       if(dd.is_special())
0178       {
0179         return date_type(date_rep_type(days_) + dd.get_rep());
0180       }
0181       return date_type(date_rep_type(days_) + static_cast<date_int_type>(dd.days()));
0182     }
0183     BOOST_CXX14_CONSTEXPR date_type operator+=(const duration_type& dd)
0184     {
0185       *this = *this + dd;
0186       return date_type(days_);
0187     }
0188 
0189     //see reference
0190   protected:
0191     /*! This is a private constructor which allows for the creation of new
0192       dates.  It is not exposed to users since that would require class
0193       users to understand the inner workings of the date class.
0194     */
0195     BOOST_CONSTEXPR explicit date(date_int_type days) : days_(days) {}
0196     BOOST_CXX14_CONSTEXPR explicit date(date_rep_type days) : days_(days.as_number()) {}
0197     date_int_type days_;
0198 
0199   };
0200 
0201 
0202 
0203 
0204 } } // namespace date_time
0205 
0206 
0207 
0208 
0209 #endif