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