Chris@16
|
1 #ifndef DATE_TIME_TIME_SYSTEM_SPLIT_HPP
|
Chris@16
|
2 #define DATE_TIME_TIME_SYSTEM_SPLIT_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@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12
|
Chris@16
|
13 #include <string>
|
Chris@16
|
14 #include "boost/date_time/compiler_config.hpp"
|
Chris@16
|
15 #include "boost/date_time/special_defs.hpp"
|
Chris@16
|
16
|
Chris@16
|
17 namespace boost {
|
Chris@16
|
18 namespace date_time {
|
Chris@16
|
19
|
Chris@16
|
20 //! An unadjusted time system implementation.
|
Chris@16
|
21 #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT))
|
Chris@16
|
22 template<typename config, boost::int32_t ticks_per_second>
|
Chris@16
|
23 #else
|
Chris@16
|
24 template<typename config>
|
Chris@16
|
25 #endif
|
Chris@16
|
26 class split_timedate_system
|
Chris@16
|
27 {
|
Chris@16
|
28 public:
|
Chris@16
|
29 typedef typename config::time_rep_type time_rep_type;
|
Chris@16
|
30 typedef typename config::date_type date_type;
|
Chris@16
|
31 typedef typename config::time_duration_type time_duration_type;
|
Chris@16
|
32 typedef typename config::date_duration_type date_duration_type;
|
Chris@16
|
33 typedef typename config::int_type int_type;
|
Chris@16
|
34 typedef typename config::resolution_traits resolution_traits;
|
Chris@16
|
35
|
Chris@16
|
36 //86400 is number of seconds in a day...
|
Chris@16
|
37 #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT))
|
Chris@16
|
38 typedef date_time::wrapping_int<int_type, INT64_C(86400) * ticks_per_second > wrap_int_type;
|
Chris@16
|
39 #else
|
Chris@16
|
40 private:
|
Chris@16
|
41 BOOST_STATIC_CONSTANT(int_type, ticks_per_day = INT64_C(86400) * config::tick_per_second);
|
Chris@16
|
42 public:
|
Chris@16
|
43 # if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0X581) )
|
Chris@16
|
44 typedef date_time::wrapping_int< split_timedate_system::int_type, split_timedate_system::ticks_per_day> wrap_int_type;
|
Chris@16
|
45 # else
|
Chris@16
|
46 typedef date_time::wrapping_int<int_type, ticks_per_day> wrap_int_type;
|
Chris@16
|
47 #endif
|
Chris@16
|
48 #endif
|
Chris@16
|
49
|
Chris@16
|
50 static time_rep_type get_time_rep(special_values sv)
|
Chris@16
|
51 {
|
Chris@16
|
52 switch (sv) {
|
Chris@16
|
53 case not_a_date_time:
|
Chris@16
|
54 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
55 time_duration_type(not_a_date_time));
|
Chris@16
|
56 case pos_infin:
|
Chris@16
|
57 return time_rep_type(date_type(pos_infin),
|
Chris@16
|
58 time_duration_type(pos_infin));
|
Chris@16
|
59 case neg_infin:
|
Chris@16
|
60 return time_rep_type(date_type(neg_infin),
|
Chris@16
|
61 time_duration_type(neg_infin));
|
Chris@16
|
62 case max_date_time: {
|
Chris@16
|
63 time_duration_type td = time_duration_type(24,0,0,0) - time_duration_type(0,0,0,1);
|
Chris@16
|
64 return time_rep_type(date_type(max_date_time), td);
|
Chris@16
|
65 }
|
Chris@16
|
66 case min_date_time:
|
Chris@16
|
67 return time_rep_type(date_type(min_date_time), time_duration_type(0,0,0,0));
|
Chris@16
|
68
|
Chris@16
|
69 default:
|
Chris@16
|
70 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
71 time_duration_type(not_a_date_time));
|
Chris@16
|
72
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 static time_rep_type get_time_rep(const date_type& day,
|
Chris@16
|
78 const time_duration_type& tod,
|
Chris@16
|
79 date_time::dst_flags /* dst */ = not_dst)
|
Chris@16
|
80 {
|
Chris@16
|
81 if(day.is_special() || tod.is_special()) {
|
Chris@16
|
82 if(day.is_not_a_date() || tod.is_not_a_date_time()) {
|
Chris@16
|
83 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
84 time_duration_type(not_a_date_time));
|
Chris@16
|
85 }
|
Chris@16
|
86 else if(day.is_pos_infinity()) {
|
Chris@16
|
87 if(tod.is_neg_infinity()) {
|
Chris@16
|
88 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
89 time_duration_type(not_a_date_time));
|
Chris@16
|
90 }
|
Chris@16
|
91 else {
|
Chris@16
|
92 return time_rep_type(day, time_duration_type(pos_infin));
|
Chris@16
|
93 }
|
Chris@16
|
94 }
|
Chris@16
|
95 else if(day.is_neg_infinity()) {
|
Chris@16
|
96 if(tod.is_pos_infinity()) {
|
Chris@16
|
97 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
98 time_duration_type(not_a_date_time));
|
Chris@16
|
99 }
|
Chris@16
|
100 else {
|
Chris@16
|
101 return time_rep_type(day, time_duration_type(neg_infin));
|
Chris@16
|
102 }
|
Chris@16
|
103 }
|
Chris@16
|
104 else if(tod.is_pos_infinity()) {
|
Chris@16
|
105 if(day.is_neg_infinity()) {
|
Chris@16
|
106 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
107 time_duration_type(not_a_date_time));
|
Chris@16
|
108 }
|
Chris@16
|
109 else {
|
Chris@16
|
110 return time_rep_type(date_type(pos_infin), tod);
|
Chris@16
|
111 }
|
Chris@16
|
112 }
|
Chris@16
|
113 else if(tod.is_neg_infinity()) {
|
Chris@16
|
114 if(day.is_pos_infinity()) {
|
Chris@16
|
115 return time_rep_type(date_type(not_a_date_time),
|
Chris@16
|
116 time_duration_type(not_a_date_time));
|
Chris@16
|
117 }
|
Chris@16
|
118 else {
|
Chris@16
|
119 return time_rep_type(date_type(neg_infin), tod);
|
Chris@16
|
120 }
|
Chris@16
|
121 }
|
Chris@16
|
122 }
|
Chris@16
|
123 return time_rep_type(day, tod);
|
Chris@16
|
124 }
|
Chris@16
|
125 static date_type get_date(const time_rep_type& val)
|
Chris@16
|
126 {
|
Chris@16
|
127 return date_type(val.day);
|
Chris@16
|
128 }
|
Chris@16
|
129 static time_duration_type get_time_of_day(const time_rep_type& val)
|
Chris@16
|
130 {
|
Chris@16
|
131 return time_duration_type(val.time_of_day);
|
Chris@16
|
132 }
|
Chris@16
|
133 static std::string zone_name(const time_rep_type&)
|
Chris@16
|
134 {
|
Chris@16
|
135 return std::string();
|
Chris@16
|
136 }
|
Chris@16
|
137 static bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs)
|
Chris@16
|
138 {
|
Chris@16
|
139 return ((lhs.day == rhs.day) && (lhs.time_of_day == rhs.time_of_day));
|
Chris@16
|
140 }
|
Chris@16
|
141 static bool is_less(const time_rep_type& lhs, const time_rep_type& rhs)
|
Chris@16
|
142 {
|
Chris@16
|
143 if (lhs.day < rhs.day) return true;
|
Chris@16
|
144 if (lhs.day > rhs.day) return false;
|
Chris@16
|
145 return (lhs.time_of_day < rhs.time_of_day);
|
Chris@16
|
146 }
|
Chris@16
|
147 static time_rep_type add_days(const time_rep_type& base,
|
Chris@16
|
148 const date_duration_type& dd)
|
Chris@16
|
149 {
|
Chris@16
|
150 return time_rep_type(base.day+dd, base.time_of_day);
|
Chris@16
|
151 }
|
Chris@16
|
152 static time_rep_type subtract_days(const time_rep_type& base,
|
Chris@16
|
153 const date_duration_type& dd)
|
Chris@16
|
154 {
|
Chris@16
|
155 return split_timedate_system::get_time_rep(base.day-dd, base.time_of_day);
|
Chris@16
|
156 }
|
Chris@16
|
157 static time_rep_type subtract_time_duration(const time_rep_type& base,
|
Chris@16
|
158 const time_duration_type& td)
|
Chris@16
|
159 {
|
Chris@16
|
160 if(base.day.is_special() || td.is_special())
|
Chris@16
|
161 {
|
Chris@16
|
162 return split_timedate_system::get_time_rep(base.day, -td);
|
Chris@16
|
163 }
|
Chris@16
|
164 if (td.is_negative()) {
|
Chris@16
|
165 time_duration_type td1 = td.invert_sign();
|
Chris@16
|
166 return add_time_duration(base,td1);
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 wrap_int_type day_offset(base.time_of_day.ticks());
|
Chris@16
|
170 date_duration_type day_overflow(static_cast<typename date_duration_type::duration_rep_type>(day_offset.subtract(td.ticks())));
|
Chris@16
|
171
|
Chris@16
|
172 return time_rep_type(base.day-day_overflow,
|
Chris@16
|
173 time_duration_type(0,0,0,day_offset.as_int()));
|
Chris@16
|
174 }
|
Chris@16
|
175 static time_rep_type add_time_duration(const time_rep_type& base,
|
Chris@16
|
176 time_duration_type td)
|
Chris@16
|
177 {
|
Chris@16
|
178 if(base.day.is_special() || td.is_special()) {
|
Chris@16
|
179 return split_timedate_system::get_time_rep(base.day, td);
|
Chris@16
|
180 }
|
Chris@16
|
181 if (td.is_negative()) {
|
Chris@16
|
182 time_duration_type td1 = td.invert_sign();
|
Chris@16
|
183 return subtract_time_duration(base,td1);
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 wrap_int_type day_offset(base.time_of_day.ticks());
|
Chris@16
|
187 date_duration_type day_overflow(static_cast< typename date_duration_type::duration_rep_type >(day_offset.add(td.ticks())));
|
Chris@16
|
188
|
Chris@16
|
189 return time_rep_type(base.day+day_overflow,
|
Chris@16
|
190 time_duration_type(0,0,0,day_offset.as_int()));
|
Chris@16
|
191 }
|
Chris@16
|
192 static time_duration_type subtract_times(const time_rep_type& lhs,
|
Chris@16
|
193 const time_rep_type& rhs)
|
Chris@16
|
194 {
|
Chris@16
|
195 date_duration_type dd = lhs.day - rhs.day;
|
Chris@16
|
196 time_duration_type td(dd.days()*24,0,0); //days * 24 hours
|
Chris@16
|
197 time_duration_type td2 = lhs.time_of_day - rhs.time_of_day;
|
Chris@16
|
198 return td+td2;
|
Chris@16
|
199 // return time_rep_type(base.day-dd, base.time_of_day);
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 };
|
Chris@16
|
203
|
Chris@16
|
204 } } //namespace date_time
|
Chris@16
|
205
|
Chris@16
|
206
|
Chris@16
|
207 #endif
|