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