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