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