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