Chris@16: #ifndef DATE_TIME_DATE_HPP___ Chris@16: #define DATE_TIME_DATE_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //!Representation of timepoint at the one day level resolution. Chris@16: /*! Chris@16: The date template represents an interface shell for a date class Chris@16: that is based on a year-month-day system such as the gregorian Chris@16: or iso systems. It provides basic operations to enable calculation Chris@16: and comparisons. Chris@16: Chris@16: Theory Chris@16: Chris@16: This date representation fundamentally departs from the C tm struct Chris@16: approach. The goal for this type is to provide efficient date Chris@16: operations (add, subtract) and storage (minimize space to represent) Chris@16: in a concrete class. Thus, the date uses a count internally to Chris@16: represent a particular date. The calendar parameter defines Chris@16: the policies for converting the the year-month-day and internal Chris@16: counted form here. Applications that need to perform heavy Chris@16: formatting of the same date repeatedly will perform better Chris@16: by using the year-month-day representation. Chris@16: Chris@16: Internally the date uses a day number to represent the date. Chris@16: This is a monotonic time representation. This representation Chris@16: allows for fast comparison as well as simplifying Chris@16: the creation of writing numeric operations. Essentially, the Chris@16: internal day number is like adjusted julian day. The adjustment Chris@16: is determined by the Epoch date which is represented as day 1 of Chris@16: the calendar. Day 0 is reserved for negative infinity so that Chris@16: any actual date is automatically greater than negative infinity. Chris@16: When a date is constructed from a date or formatted for output, Chris@16: the appropriate conversions are applied to create the year, month, Chris@16: day representations. Chris@16: */ Chris@16: Chris@16: Chris@16: template Chris@16: class date : private Chris@16: boost::less_than_comparable > Chris@16: { Chris@16: public: Chris@16: typedef T date_type; Chris@16: typedef calendar calendar_type; Chris@16: typedef typename calendar::date_traits_type traits_type; Chris@16: typedef duration_type_ duration_type; Chris@16: typedef typename calendar::year_type year_type; Chris@16: typedef typename calendar::month_type month_type; Chris@16: typedef typename calendar::day_type day_type; Chris@16: typedef typename calendar::ymd_type ymd_type; Chris@16: typedef typename calendar::date_rep_type date_rep_type; Chris@16: typedef typename calendar::date_int_type date_int_type; Chris@16: typedef typename calendar::day_of_week_type day_of_week_type; Chris@16: date(year_type y, month_type m, day_type d) Chris@16: : days_(calendar::day_number(ymd_type(y, m, d))) Chris@16: {} Chris@16: date(const ymd_type& ymd) Chris@16: : days_(calendar::day_number(ymd)) Chris@16: {} Chris@16: //let the compiler write copy, assignment, and destructor Chris@16: year_type year() const Chris@16: { Chris@16: ymd_type ymd = calendar::from_day_number(days_); Chris@16: return ymd.year; Chris@16: } Chris@16: month_type month() const Chris@16: { Chris@16: ymd_type ymd = calendar::from_day_number(days_); Chris@16: return ymd.month; Chris@16: } Chris@16: day_type day() const Chris@16: { Chris@16: ymd_type ymd = calendar::from_day_number(days_); Chris@16: return ymd.day; Chris@16: } Chris@16: day_of_week_type day_of_week() const Chris@16: { Chris@16: ymd_type ymd = calendar::from_day_number(days_); Chris@16: return calendar::day_of_week(ymd); Chris@16: } Chris@16: ymd_type year_month_day() const Chris@16: { Chris@16: return calendar::from_day_number(days_); Chris@16: } Chris@16: bool operator<(const date_type& rhs) const Chris@16: { Chris@16: return days_ < rhs.days_; Chris@16: } Chris@16: bool operator==(const date_type& rhs) const Chris@16: { Chris@16: return days_ == rhs.days_; Chris@16: } Chris@16: //! check to see if date is a special value Chris@16: bool is_special()const Chris@16: { Chris@16: return(is_not_a_date() || is_infinity()); Chris@16: } Chris@16: //! check to see if date is not a value Chris@16: bool is_not_a_date() const Chris@16: { Chris@16: return traits_type::is_not_a_number(days_); Chris@16: } Chris@16: //! check to see if date is one of the infinity values Chris@16: bool is_infinity() const Chris@16: { Chris@16: return traits_type::is_inf(days_); Chris@16: } Chris@16: //! check to see if date is greater than all possible dates Chris@16: bool is_pos_infinity() const Chris@16: { Chris@16: return traits_type::is_pos_inf(days_); Chris@16: } Chris@16: //! check to see if date is greater than all possible dates Chris@16: bool is_neg_infinity() const Chris@16: { Chris@16: return traits_type::is_neg_inf(days_); Chris@16: } Chris@16: //! return as a special value or a not_special if a normal date Chris@16: special_values as_special() const Chris@16: { Chris@16: return traits_type::to_special(days_); Chris@16: } Chris@16: duration_type operator-(const date_type& d) const Chris@16: { Chris@16: if (!this->is_special() && !d.is_special()) Chris@16: { Chris@16: // The duration underlying type may be wider than the date underlying type. Chris@16: // Thus we calculate the difference in terms of two durations from some common fixed base date. Chris@16: typedef typename duration_type::duration_rep_type duration_rep_type; Chris@16: return duration_type(static_cast< duration_rep_type >(days_) - static_cast< duration_rep_type >(d.days_)); Chris@16: } Chris@16: else Chris@16: { Chris@16: // In this case the difference will be a special value, too Chris@16: date_rep_type val = date_rep_type(days_) - date_rep_type(d.days_); Chris@16: return duration_type(val.as_special()); Chris@16: } Chris@16: } Chris@16: Chris@16: date_type operator-(const duration_type& dd) const Chris@16: { Chris@16: if(dd.is_special()) Chris@16: { Chris@16: return date_type(date_rep_type(days_) - dd.get_rep()); Chris@16: } Chris@16: return date_type(date_rep_type(days_) - dd.days()); Chris@16: } Chris@16: date_type operator-=(const duration_type& dd) Chris@16: { Chris@16: *this = *this - dd; Chris@16: return date_type(days_); Chris@16: } Chris@16: date_rep_type day_count() const Chris@16: { Chris@16: return days_; Chris@16: } Chris@16: //allow internal access from operators Chris@16: date_type operator+(const duration_type& dd) const Chris@16: { Chris@16: if(dd.is_special()) Chris@16: { Chris@16: return date_type(date_rep_type(days_) + dd.get_rep()); Chris@16: } Chris@16: return date_type(date_rep_type(days_) + dd.days()); Chris@16: } Chris@16: date_type operator+=(const duration_type& dd) Chris@16: { Chris@16: *this = *this + dd; Chris@16: return date_type(days_); Chris@16: } Chris@16: Chris@16: //see reference Chris@16: protected: Chris@16: /*! This is a private constructor which allows for the creation of new Chris@16: dates. It is not exposed to users since that would require class Chris@16: users to understand the inner workings of the date class. Chris@16: */ Chris@16: explicit date(date_int_type days) : days_(days) {} Chris@16: explicit date(date_rep_type days) : days_(days.as_number()) {} Chris@16: date_int_type days_; Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: } } // namespace date_time Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif