[44] | 1 | #ifndef DATE_TIME_TIME_SYSTEM_COUNTED_HPP |
---|
| 2 | #define DATE_TIME_TIME_SYSTEM_COUNTED_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-02-27 15:00:24 -0500 (Wed, 27 Feb 2008) $ |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | #include "boost/date_time/time_defs.hpp" |
---|
| 15 | #include <string> |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | namespace boost { |
---|
| 19 | namespace date_time { |
---|
| 20 | |
---|
| 21 | //! Time representation that uses a single integer count |
---|
| 22 | template<class config> |
---|
| 23 | struct counted_time_rep |
---|
| 24 | { |
---|
| 25 | typedef typename config::int_type int_type; |
---|
| 26 | typedef typename config::date_type date_type; |
---|
| 27 | typedef typename config::impl_type impl_type; |
---|
| 28 | typedef typename date_type::duration_type date_duration_type; |
---|
| 29 | typedef typename date_type::calendar_type calendar_type; |
---|
| 30 | typedef typename date_type::ymd_type ymd_type; |
---|
| 31 | typedef typename config::time_duration_type time_duration_type; |
---|
| 32 | typedef typename config::resolution_traits resolution_traits; |
---|
| 33 | |
---|
| 34 | counted_time_rep(const date_type& d, const time_duration_type& time_of_day) |
---|
| 35 | : time_count_(1) |
---|
| 36 | { |
---|
| 37 | if(d.is_infinity() || d.is_not_a_date() || time_of_day.is_special()) { |
---|
| 38 | time_count_ = time_of_day.get_rep() + d.day_count(); |
---|
| 39 | //std::cout << time_count_ << std::endl; |
---|
| 40 | } |
---|
| 41 | else { |
---|
| 42 | time_count_ = (d.day_number() * frac_sec_per_day()) + time_of_day.ticks(); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | explicit counted_time_rep(int_type count) : |
---|
| 46 | time_count_(count) |
---|
| 47 | {} |
---|
| 48 | explicit counted_time_rep(impl_type count) : |
---|
| 49 | time_count_(count) |
---|
| 50 | {} |
---|
| 51 | date_type date() const |
---|
| 52 | { |
---|
| 53 | if(time_count_.is_special()) { |
---|
| 54 | return date_type(time_count_.as_special()); |
---|
| 55 | } |
---|
| 56 | else { |
---|
| 57 | typename calendar_type::date_int_type dc = day_count(); |
---|
| 58 | //std::cout << "time_rep here:" << dc << std::endl; |
---|
| 59 | ymd_type ymd = calendar_type::from_day_number(dc); |
---|
| 60 | return date_type(ymd); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | //int_type day_count() const |
---|
| 64 | unsigned long day_count() const |
---|
| 65 | { |
---|
| 66 | /* resolution_traits::as_number returns a boost::int64_t & |
---|
| 67 | * frac_sec_per_day is also a boost::int64_t so, naturally, |
---|
| 68 | * the division operation returns a boost::int64_t. |
---|
| 69 | * The static_cast to an unsigned long is ok (results in no data loss) |
---|
| 70 | * because frac_sec_per_day is either the number of |
---|
| 71 | * microseconds per day, or the number of nanoseconds per day. |
---|
| 72 | * Worst case scenario: resolution_traits::as_number returns the |
---|
| 73 | * maximum value an int64_t can hold and frac_sec_per_day |
---|
| 74 | * is microseconds per day (lowest possible value). |
---|
| 75 | * The division operation will then return a value of 106751991 - |
---|
| 76 | * easily fitting in an unsigned long. |
---|
| 77 | */ |
---|
| 78 | return static_cast<unsigned long>(resolution_traits::as_number(time_count_) / frac_sec_per_day()); |
---|
| 79 | } |
---|
| 80 | int_type time_count() const |
---|
| 81 | { |
---|
| 82 | return resolution_traits::as_number(time_count_); |
---|
| 83 | } |
---|
| 84 | int_type tod() const |
---|
| 85 | { |
---|
| 86 | return resolution_traits::as_number(time_count_) % frac_sec_per_day(); |
---|
| 87 | } |
---|
| 88 | static int_type frac_sec_per_day() |
---|
| 89 | { |
---|
| 90 | int_type seconds_per_day = 60*60*24; |
---|
| 91 | int_type fractional_sec_per_sec(resolution_traits::res_adjust()); |
---|
| 92 | return seconds_per_day*fractional_sec_per_sec; |
---|
| 93 | } |
---|
| 94 | bool is_pos_infinity()const |
---|
| 95 | { |
---|
| 96 | return impl_type::is_pos_inf(time_count_.as_number()); |
---|
| 97 | } |
---|
| 98 | bool is_neg_infinity()const |
---|
| 99 | { |
---|
| 100 | return impl_type::is_neg_inf(time_count_.as_number()); |
---|
| 101 | } |
---|
| 102 | bool is_not_a_date_time()const |
---|
| 103 | { |
---|
| 104 | return impl_type::is_not_a_number(time_count_.as_number()); |
---|
| 105 | } |
---|
| 106 | bool is_special()const |
---|
| 107 | { |
---|
| 108 | return time_count_.is_special(); |
---|
| 109 | } |
---|
| 110 | impl_type get_rep()const |
---|
| 111 | { |
---|
| 112 | return time_count_; |
---|
| 113 | } |
---|
| 114 | private: |
---|
| 115 | impl_type time_count_; |
---|
| 116 | }; |
---|
| 117 | |
---|
| 118 | //! An unadjusted time system implementation. |
---|
| 119 | template<class time_rep> |
---|
| 120 | class counted_time_system |
---|
| 121 | { |
---|
| 122 | public: |
---|
| 123 | typedef time_rep time_rep_type; |
---|
| 124 | typedef typename time_rep_type::impl_type impl_type; |
---|
| 125 | typedef typename time_rep_type::time_duration_type time_duration_type; |
---|
| 126 | typedef typename time_duration_type::fractional_seconds_type fractional_seconds_type; |
---|
| 127 | typedef typename time_rep_type::date_type date_type; |
---|
| 128 | typedef typename time_rep_type::date_duration_type date_duration_type; |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | template<class T> static void unused_var(const T&) {} |
---|
| 132 | |
---|
| 133 | static time_rep_type get_time_rep(const date_type& day, |
---|
| 134 | const time_duration_type& tod, |
---|
| 135 | date_time::dst_flags dst=not_dst) |
---|
| 136 | { |
---|
| 137 | unused_var(dst); |
---|
| 138 | return time_rep_type(day, tod); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static time_rep_type get_time_rep(special_values sv) |
---|
| 142 | { |
---|
| 143 | switch (sv) { |
---|
| 144 | case not_a_date_time: |
---|
| 145 | return time_rep_type(date_type(not_a_date_time), |
---|
| 146 | time_duration_type(not_a_date_time)); |
---|
| 147 | case pos_infin: |
---|
| 148 | return time_rep_type(date_type(pos_infin), |
---|
| 149 | time_duration_type(pos_infin)); |
---|
| 150 | case neg_infin: |
---|
| 151 | return time_rep_type(date_type(neg_infin), |
---|
| 152 | time_duration_type(neg_infin)); |
---|
| 153 | case max_date_time: { |
---|
| 154 | time_duration_type td = time_duration_type(24,0,0,0) - time_duration_type(0,0,0,1); |
---|
| 155 | return time_rep_type(date_type(max_date_time), td); |
---|
| 156 | } |
---|
| 157 | case min_date_time: |
---|
| 158 | return time_rep_type(date_type(min_date_time), time_duration_type(0,0,0,0)); |
---|
| 159 | |
---|
| 160 | default: |
---|
| 161 | return time_rep_type(date_type(not_a_date_time), |
---|
| 162 | time_duration_type(not_a_date_time)); |
---|
| 163 | |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | static date_type get_date(const time_rep_type& val) |
---|
| 169 | { |
---|
| 170 | return val.date(); |
---|
| 171 | } |
---|
| 172 | static time_duration_type get_time_of_day(const time_rep_type& val) |
---|
| 173 | { |
---|
| 174 | if(val.is_special()) { |
---|
| 175 | return time_duration_type(val.get_rep().as_special()); |
---|
| 176 | } |
---|
| 177 | else{ |
---|
| 178 | return time_duration_type(0,0,0,val.tod()); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | static std::string zone_name(const time_rep_type&) |
---|
| 182 | { |
---|
| 183 | return ""; |
---|
| 184 | } |
---|
| 185 | static bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs) |
---|
| 186 | { |
---|
| 187 | return (lhs.time_count() == rhs.time_count()); |
---|
| 188 | } |
---|
| 189 | static bool is_less(const time_rep_type& lhs, const time_rep_type& rhs) |
---|
| 190 | { |
---|
| 191 | return (lhs.time_count() < rhs.time_count()); |
---|
| 192 | } |
---|
| 193 | static time_rep_type add_days(const time_rep_type& base, |
---|
| 194 | const date_duration_type& dd) |
---|
| 195 | { |
---|
| 196 | if(base.is_special() || dd.is_special()) { |
---|
| 197 | return(time_rep_type(base.get_rep() + dd.get_rep())); |
---|
| 198 | } |
---|
| 199 | else { |
---|
| 200 | return time_rep_type(base.time_count() + (dd.days() * time_rep_type::frac_sec_per_day())); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | static time_rep_type subtract_days(const time_rep_type& base, |
---|
| 204 | const date_duration_type& dd) |
---|
| 205 | { |
---|
| 206 | if(base.is_special() || dd.is_special()) { |
---|
| 207 | return(time_rep_type(base.get_rep() - dd.get_rep())); |
---|
| 208 | } |
---|
| 209 | else{ |
---|
| 210 | return time_rep_type(base.time_count() - (dd.days() * time_rep_type::frac_sec_per_day())); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | static time_rep_type subtract_time_duration(const time_rep_type& base, |
---|
| 214 | const time_duration_type& td) |
---|
| 215 | { |
---|
| 216 | if(base.is_special() || td.is_special()) { |
---|
| 217 | return(time_rep_type(base.get_rep() - td.get_rep())); |
---|
| 218 | } |
---|
| 219 | else { |
---|
| 220 | return time_rep_type(base.time_count() - td.ticks()); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | static time_rep_type add_time_duration(const time_rep_type& base, |
---|
| 224 | time_duration_type td) |
---|
| 225 | { |
---|
| 226 | if(base.is_special() || td.is_special()) { |
---|
| 227 | return(time_rep_type(base.get_rep() + td.get_rep())); |
---|
| 228 | } |
---|
| 229 | else { |
---|
| 230 | return time_rep_type(base.time_count() + td.ticks()); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | static time_duration_type subtract_times(const time_rep_type& lhs, |
---|
| 234 | const time_rep_type& rhs) |
---|
| 235 | { |
---|
| 236 | if(lhs.is_special() || rhs.is_special()) { |
---|
| 237 | return(time_duration_type( |
---|
| 238 | impl_type::to_special((lhs.get_rep() - rhs.get_rep()).as_number()))); |
---|
| 239 | } |
---|
| 240 | else { |
---|
| 241 | fractional_seconds_type fs = lhs.time_count() - rhs.time_count(); |
---|
| 242 | return time_duration_type(0,0,0,fs); |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | }; |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | } } //namespace date_time |
---|
| 250 | |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | #endif |
---|
| 254 | |
---|