1 #ifndef GREG_DATE_HPP___ 2 #define GREG_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 9 * $Date$ 10 */ 11 12 #include <boost/throw_exception.hpp> 13 #include <boost/date_time/date.hpp> 14 #include <boost/date_time/special_defs.hpp> 15 #include <boost/date_time/gregorian/greg_calendar.hpp> 16 #include <boost/date_time/gregorian/greg_duration.hpp> 17 18 namespace boost { 19 namespace gregorian { 20 21 //bring special enum values into the namespace 22 using date_time::special_values; 23 using date_time::not_special; 24 using date_time::neg_infin; 25 using date_time::pos_infin; 26 using date_time::not_a_date_time; 27 using date_time::max_date_time; 28 using date_time::min_date_time; 29 30 //! A date type based on gregorian_calendar 31 /*! This class is the primary interface for programming with 32 greogorian dates. The is a lightweight type that can be 33 freely passed by value. All comparison operators are 34 supported. 35 \ingroup date_basics 36 */ 37 class date : public date_time::date<date, gregorian_calendar, date_duration> 38 { 39 public: 40 typedef gregorian_calendar::year_type year_type; 41 typedef gregorian_calendar::month_type month_type; 42 typedef gregorian_calendar::day_type day_type; 43 typedef gregorian_calendar::day_of_year_type day_of_year_type; 44 typedef gregorian_calendar::ymd_type ymd_type; 45 typedef gregorian_calendar::date_rep_type date_rep_type; 46 typedef gregorian_calendar::date_int_type date_int_type; 47 typedef date_duration duration_type; 48 #if !defined(DATE_TIME_NO_DEFAULT_CONSTRUCTOR) 49 //! Default constructor constructs with not_a_date_time date()50 date(): 51 date_time::date<date, gregorian_calendar, date_duration>(date_rep_type::from_special(not_a_date_time)) 52 {} 53 #endif // DATE_TIME_NO_DEFAULT_CONSTRUCTOR 54 //! Main constructor with year, month, day date(year_type y,month_type m,day_type d)55 date(year_type y, month_type m, day_type d) 56 : date_time::date<date, gregorian_calendar, date_duration>(y, m, d) 57 { 58 if (gregorian_calendar::end_of_month_day(y, m) < d) { 59 boost::throw_exception(bad_day_of_month(std::string("Day of month is not valid for year"))); 60 } 61 } 62 //! Constructor from a ymd_type structure date(const ymd_type & ymd)63 explicit date(const ymd_type& ymd) 64 : date_time::date<date, gregorian_calendar, date_duration>(ymd) 65 {} 66 //! Needed copy constructor date(const date_int_type & rhs)67 explicit date(const date_int_type& rhs): 68 date_time::date<date,gregorian_calendar, date_duration>(rhs) 69 {} 70 //! Needed copy constructor date(date_rep_type rhs)71 explicit date(date_rep_type rhs): 72 date_time::date<date,gregorian_calendar, date_duration>(rhs) 73 {} 74 //! Constructor for infinities, not a date, max and min date date(special_values sv)75 explicit date(special_values sv): 76 date_time::date<date, gregorian_calendar, date_duration>(date_rep_type::from_special(sv)) 77 { 78 if (sv == min_date_time) 79 { 80 *this = date(1400, 1, 1); 81 } 82 if (sv == max_date_time) 83 { 84 *this = date(9999, 12, 31); 85 } 86 87 } 88 //!Return the Julian Day number for the date. julian_day() const89 date_int_type julian_day() const 90 { 91 ymd_type ymd = year_month_day(); 92 return gregorian_calendar::julian_day_number(ymd); 93 } 94 //!Return the day of year 1..365 or 1..366 (for leap year) day_of_year() const95 day_of_year_type day_of_year() const 96 { 97 date start_of_year(year(), 1, 1); 98 unsigned short doy = static_cast<unsigned short>((*this-start_of_year).days() + 1); 99 return day_of_year_type(doy); 100 } 101 //!Return the Modified Julian Day number for the date. modjulian_day() const102 date_int_type modjulian_day() const 103 { 104 ymd_type ymd = year_month_day(); 105 return gregorian_calendar::modjulian_day_number(ymd); 106 } 107 //!Return the iso 8601 week number 1..53 week_number() const108 int week_number() const 109 { 110 ymd_type ymd = year_month_day(); 111 return gregorian_calendar::week_number(ymd); 112 } 113 //! Return the day number from the calendar day_number() const114 date_int_type day_number() const 115 { 116 return days_; 117 } 118 //! Return the last day of the current month end_of_month() const119 date end_of_month() const 120 { 121 ymd_type ymd = year_month_day(); 122 short eom_day = gregorian_calendar::end_of_month_day(ymd.year, ymd.month); 123 return date(ymd.year, ymd.month, eom_day); 124 } 125 126 private: 127 128 }; 129 130 131 132 } } //namespace gregorian 133 134 135 136 #endif 137