1 | #ifndef DATE_TIME_TIME_HPP___ |
---|
2 | #define DATE_TIME_TIME_HPP___ |
---|
3 | |
---|
4 | /* Copyright (c) 2002,2003,2005 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 | |
---|
13 | /*! @file time.hpp |
---|
14 | This file contains the interface for the time associated classes. |
---|
15 | */ |
---|
16 | #include <string> |
---|
17 | #include <boost/operators.hpp> |
---|
18 | #include <boost/date_time/time_defs.hpp> |
---|
19 | #include <boost/date_time/special_defs.hpp> |
---|
20 | |
---|
21 | namespace boost { |
---|
22 | namespace date_time { |
---|
23 | |
---|
24 | //! Representation of a precise moment in time, including the date. |
---|
25 | /*! |
---|
26 | This class is a skeleton for the interface of a temporal type |
---|
27 | with a resolution that is higher than a day. It is intended that |
---|
28 | this class be the base class and that the actual time |
---|
29 | class be derived using the BN pattern. In this way, the derived |
---|
30 | class can make decisions such as 'should there be a default constructor' |
---|
31 | and what should it set its value to, should there be optional constructors |
---|
32 | say allowing only an time_durations that generate a time from a clock,etc. |
---|
33 | So, in fact multiple time types can be created for a time_system with |
---|
34 | different construction policies, and all of them can perform basic |
---|
35 | operations by only writing a copy constructor. Finally, compiler |
---|
36 | errors are also shorter. |
---|
37 | |
---|
38 | The real behavior of the time class is provided by the time_system |
---|
39 | template parameter. This class must provide all the logic |
---|
40 | for addition, subtraction, as well as define all the interface |
---|
41 | types. |
---|
42 | |
---|
43 | */ |
---|
44 | |
---|
45 | template <class T, class time_system> |
---|
46 | class base_time : private |
---|
47 | boost::less_than_comparable<T |
---|
48 | , boost::equality_comparable<T |
---|
49 | > > |
---|
50 | { |
---|
51 | public: |
---|
52 | typedef T time_type; |
---|
53 | typedef typename time_system::time_rep_type time_rep_type; |
---|
54 | typedef typename time_system::date_type date_type; |
---|
55 | typedef typename time_system::date_duration_type date_duration_type; |
---|
56 | typedef typename time_system::time_duration_type time_duration_type; |
---|
57 | //typedef typename time_system::hms_type hms_type; |
---|
58 | |
---|
59 | base_time(const date_type& day, |
---|
60 | const time_duration_type& td, |
---|
61 | dst_flags dst=not_dst) : |
---|
62 | time_(time_system::get_time_rep(day, td, dst)) |
---|
63 | {} |
---|
64 | base_time(special_values sv) : |
---|
65 | time_(time_system::get_time_rep(sv)) |
---|
66 | {} |
---|
67 | base_time(const time_rep_type& rhs) : |
---|
68 | time_(rhs) |
---|
69 | {} |
---|
70 | date_type date() const |
---|
71 | { |
---|
72 | return time_system::get_date(time_); |
---|
73 | } |
---|
74 | time_duration_type time_of_day() const |
---|
75 | { |
---|
76 | return time_system::get_time_of_day(time_); |
---|
77 | } |
---|
78 | /*! Optional bool parameter will return time zone as an offset |
---|
79 | * (ie "+07:00"). Empty string is returned for classes that do |
---|
80 | * not use a time_zone */ |
---|
81 | std::string zone_name(bool /*as_offset*/=false) const |
---|
82 | { |
---|
83 | return time_system::zone_name(time_); |
---|
84 | } |
---|
85 | /*! Optional bool parameter will return time zone as an offset |
---|
86 | * (ie "+07:00"). Empty string is returned for classes that do |
---|
87 | * not use a time_zone */ |
---|
88 | std::string zone_abbrev(bool /*as_offset*/=false) const |
---|
89 | { |
---|
90 | return time_system::zone_name(time_); |
---|
91 | } |
---|
92 | //! An empty string is returned for classes that do not use a time_zone |
---|
93 | std::string zone_as_posix_string() const |
---|
94 | { |
---|
95 | return std::string(); |
---|
96 | } |
---|
97 | |
---|
98 | //! check to see if date is not a value |
---|
99 | bool is_not_a_date_time() const |
---|
100 | { |
---|
101 | return time_.is_not_a_date_time(); |
---|
102 | } |
---|
103 | //! check to see if date is one of the infinity values |
---|
104 | bool is_infinity() const |
---|
105 | { |
---|
106 | return (is_pos_infinity() || is_neg_infinity()); |
---|
107 | } |
---|
108 | //! check to see if date is greater than all possible dates |
---|
109 | bool is_pos_infinity() const |
---|
110 | { |
---|
111 | return time_.is_pos_infinity(); |
---|
112 | } |
---|
113 | //! check to see if date is greater than all possible dates |
---|
114 | bool is_neg_infinity() const |
---|
115 | { |
---|
116 | return time_.is_neg_infinity(); |
---|
117 | } |
---|
118 | //! check to see if time is a special value |
---|
119 | bool is_special() const |
---|
120 | { |
---|
121 | return(is_not_a_date_time() || is_infinity()); |
---|
122 | } |
---|
123 | //!Equality operator -- others generated by boost::equality_comparable |
---|
124 | bool operator==(const time_type& rhs) const |
---|
125 | { |
---|
126 | return time_system::is_equal(time_,rhs.time_); |
---|
127 | } |
---|
128 | //!Equality operator -- others generated by boost::less_than_comparable |
---|
129 | bool operator<(const time_type& rhs) const |
---|
130 | { |
---|
131 | return time_system::is_less(time_,rhs.time_); |
---|
132 | } |
---|
133 | //! difference between two times |
---|
134 | time_duration_type operator-(const time_type& rhs) const |
---|
135 | { |
---|
136 | return time_system::subtract_times(time_, rhs.time_); |
---|
137 | } |
---|
138 | //! add date durations |
---|
139 | time_type operator+(const date_duration_type& dd) const |
---|
140 | { |
---|
141 | return time_system::add_days(time_, dd); |
---|
142 | } |
---|
143 | time_type operator+=(const date_duration_type& dd) |
---|
144 | { |
---|
145 | time_ = (time_system::get_time_rep(date() + dd, time_of_day())); |
---|
146 | return time_type(time_); |
---|
147 | } |
---|
148 | //! subtract date durations |
---|
149 | time_type operator-(const date_duration_type& dd) const |
---|
150 | { |
---|
151 | return time_system::subtract_days(time_, dd); |
---|
152 | } |
---|
153 | time_type operator-=(const date_duration_type& dd) |
---|
154 | { |
---|
155 | time_ = (time_system::get_time_rep(date() - dd, time_of_day())); |
---|
156 | return time_type(time_); |
---|
157 | } |
---|
158 | //! add time durations |
---|
159 | time_type operator+(const time_duration_type& td) const |
---|
160 | { |
---|
161 | return time_type(time_system::add_time_duration(time_, td)); |
---|
162 | } |
---|
163 | time_type operator+=(const time_duration_type& td) |
---|
164 | { |
---|
165 | time_ = (time_system::get_time_rep(date(), time_of_day() + td)); |
---|
166 | return time_type(time_); |
---|
167 | } |
---|
168 | //! subtract time durations |
---|
169 | time_type operator-(const time_duration_type& rhs) const |
---|
170 | { |
---|
171 | return time_system::subtract_time_duration(time_, rhs); |
---|
172 | } |
---|
173 | time_type operator-=(const time_duration_type& td) |
---|
174 | { |
---|
175 | time_ = (time_system::get_time_rep(date(), time_of_day() - td)); |
---|
176 | return time_type(time_); |
---|
177 | } |
---|
178 | |
---|
179 | protected: |
---|
180 | time_rep_type time_; |
---|
181 | }; |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | } } //namespace date_time::boost |
---|
188 | |
---|
189 | |
---|
190 | #endif |
---|
191 | |
---|