1 | #ifndef GREG_DURATION_HPP___ |
---|
2 | #define GREG_DURATION_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, Bart Garst |
---|
9 | * $Date: 2008-11-12 14:37:53 -0500 (Wed, 12 Nov 2008) $ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <boost/date_time/date_duration.hpp> |
---|
13 | #include <boost/date_time/int_adapter.hpp> |
---|
14 | #include <boost/date_time/special_defs.hpp> |
---|
15 | |
---|
16 | namespace boost { |
---|
17 | namespace gregorian { |
---|
18 | |
---|
19 | //!An internal date representation that includes infinities, not a date |
---|
20 | typedef boost::date_time::duration_traits_adapted date_duration_rep; |
---|
21 | |
---|
22 | //! Durations in days for gregorian system |
---|
23 | /*! \ingroup date_basics |
---|
24 | */ |
---|
25 | class date_duration : |
---|
26 | public boost::date_time::date_duration< date_duration_rep > |
---|
27 | { |
---|
28 | typedef boost::date_time::date_duration< date_duration_rep > base_type; |
---|
29 | |
---|
30 | public: |
---|
31 | typedef base_type::duration_rep duration_rep; |
---|
32 | |
---|
33 | //! Construct from a day count |
---|
34 | explicit date_duration(duration_rep day_count = 0) : base_type(day_count) {} |
---|
35 | |
---|
36 | //! construct from special_values |
---|
37 | date_duration(date_time::special_values sv) : base_type(sv) {} |
---|
38 | |
---|
39 | //! Copy constructor |
---|
40 | date_duration(const date_duration& other) : base_type(static_cast< base_type const& >(other)) |
---|
41 | {} |
---|
42 | |
---|
43 | //! Construct from another date_duration |
---|
44 | date_duration(const base_type& other) : base_type(other) |
---|
45 | {} |
---|
46 | |
---|
47 | // Relational operators |
---|
48 | // NOTE: Because of date_time::date_duration< T > design choice we don't use Boost.Operators here, |
---|
49 | // because we need the class to be a direct base. Either lose EBO, or define operators by hand. |
---|
50 | // The latter is more effecient. |
---|
51 | bool operator== (const date_duration& rhs) const |
---|
52 | { |
---|
53 | return base_type::operator== (rhs); |
---|
54 | } |
---|
55 | bool operator!= (const date_duration& rhs) const |
---|
56 | { |
---|
57 | return !operator== (rhs); |
---|
58 | } |
---|
59 | bool operator< (const date_duration& rhs) const |
---|
60 | { |
---|
61 | return base_type::operator< (rhs); |
---|
62 | } |
---|
63 | bool operator> (const date_duration& rhs) const |
---|
64 | { |
---|
65 | return !(base_type::operator< (rhs) || base_type::operator== (rhs)); |
---|
66 | } |
---|
67 | bool operator<= (const date_duration& rhs) const |
---|
68 | { |
---|
69 | return (base_type::operator< (rhs) || base_type::operator== (rhs)); |
---|
70 | } |
---|
71 | bool operator>= (const date_duration& rhs) const |
---|
72 | { |
---|
73 | return !base_type::operator< (rhs); |
---|
74 | } |
---|
75 | |
---|
76 | //! Subtract another duration -- result is signed |
---|
77 | date_duration& operator-= (const date_duration& rhs) |
---|
78 | { |
---|
79 | base_type::operator-= (rhs); |
---|
80 | return *this; |
---|
81 | } |
---|
82 | friend date_duration operator- (date_duration rhs, date_duration const& lhs) |
---|
83 | { |
---|
84 | rhs -= lhs; |
---|
85 | return rhs; |
---|
86 | } |
---|
87 | |
---|
88 | //! Add a duration -- result is signed |
---|
89 | date_duration& operator+= (const date_duration& rhs) |
---|
90 | { |
---|
91 | base_type::operator+= (rhs); |
---|
92 | return *this; |
---|
93 | } |
---|
94 | friend date_duration operator+ (date_duration rhs, date_duration const& lhs) |
---|
95 | { |
---|
96 | rhs += lhs; |
---|
97 | return rhs; |
---|
98 | } |
---|
99 | |
---|
100 | //! unary- Allows for dd = -date_duration(2); -> dd == -2 |
---|
101 | date_duration operator- ()const |
---|
102 | { |
---|
103 | return date_duration(get_rep() * (-1)); |
---|
104 | } |
---|
105 | |
---|
106 | //! Division operations on a duration with an integer. |
---|
107 | date_duration& operator/= (int divisor) |
---|
108 | { |
---|
109 | base_type::operator/= (divisor); |
---|
110 | return *this; |
---|
111 | } |
---|
112 | friend date_duration operator/ (date_duration rhs, int lhs) |
---|
113 | { |
---|
114 | rhs /= lhs; |
---|
115 | return rhs; |
---|
116 | } |
---|
117 | |
---|
118 | //! Returns the smallest duration -- used by to calculate 'end' |
---|
119 | static date_duration unit() |
---|
120 | { |
---|
121 | return date_duration(base_type::unit().get_rep()); |
---|
122 | } |
---|
123 | }; |
---|
124 | |
---|
125 | //! Shorthand for date_duration |
---|
126 | typedef date_duration days; |
---|
127 | |
---|
128 | } } //namespace gregorian |
---|
129 | |
---|
130 | #if defined(BOOST_DATE_TIME_OPTIONAL_GREGORIAN_TYPES) |
---|
131 | #include <boost/date_time/date_duration_types.hpp> |
---|
132 | #endif |
---|
133 | |
---|
134 | #endif |
---|