| 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: 2006/02/26 20:10:18 $ |
|---|
| 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 | # if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0X581) ) |
|---|
| 44 | typedef date_time::wrapping_int< split_timedate_system::int_type, split_timedate_system::ticks_per_day> wrap_int_type; |
|---|
| 45 | # else |
|---|
| 46 | typedef date_time::wrapping_int<int_type, ticks_per_day> wrap_int_type; |
|---|
| 47 | #endif |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | static time_rep_type get_time_rep(special_values sv) |
|---|
| 51 | { |
|---|
| 52 | switch (sv) { |
|---|
| 53 | case not_a_date_time: |
|---|
| 54 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 55 | time_duration_type(not_a_date_time)); |
|---|
| 56 | case pos_infin: |
|---|
| 57 | return time_rep_type(date_type(pos_infin), |
|---|
| 58 | time_duration_type(pos_infin)); |
|---|
| 59 | case neg_infin: |
|---|
| 60 | return time_rep_type(date_type(neg_infin), |
|---|
| 61 | time_duration_type(neg_infin)); |
|---|
| 62 | case max_date_time: { |
|---|
| 63 | time_duration_type td = time_duration_type(24,0,0,0) - time_duration_type(0,0,0,1); |
|---|
| 64 | return time_rep_type(date_type(max_date_time), td); |
|---|
| 65 | } |
|---|
| 66 | case min_date_time: |
|---|
| 67 | return time_rep_type(date_type(min_date_time), time_duration_type(0,0,0,0)); |
|---|
| 68 | |
|---|
| 69 | default: |
|---|
| 70 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 71 | time_duration_type(not_a_date_time)); |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | static time_rep_type get_time_rep(const date_type& day, |
|---|
| 78 | const time_duration_type& tod, |
|---|
| 79 | date_time::dst_flags dst=not_dst) |
|---|
| 80 | { |
|---|
| 81 | if(day.is_special() || tod.is_special()) { |
|---|
| 82 | if(day.is_not_a_date() || tod.is_not_a_date_time()) { |
|---|
| 83 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 84 | time_duration_type(not_a_date_time)); |
|---|
| 85 | } |
|---|
| 86 | else if(day.is_pos_infinity()) { |
|---|
| 87 | if(tod.is_neg_infinity()) { |
|---|
| 88 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 89 | time_duration_type(not_a_date_time)); |
|---|
| 90 | } |
|---|
| 91 | else { |
|---|
| 92 | return time_rep_type(day, time_duration_type(pos_infin)); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | else if(day.is_neg_infinity()) { |
|---|
| 96 | if(tod.is_pos_infinity()) { |
|---|
| 97 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 98 | time_duration_type(not_a_date_time)); |
|---|
| 99 | } |
|---|
| 100 | else { |
|---|
| 101 | return time_rep_type(day, time_duration_type(neg_infin)); |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | else if(tod.is_pos_infinity()) { |
|---|
| 105 | if(day.is_neg_infinity()) { |
|---|
| 106 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 107 | time_duration_type(not_a_date_time)); |
|---|
| 108 | } |
|---|
| 109 | else { |
|---|
| 110 | return time_rep_type(date_type(pos_infin), tod); |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | else if(tod.is_neg_infinity()) { |
|---|
| 114 | if(day.is_pos_infinity()) { |
|---|
| 115 | return time_rep_type(date_type(not_a_date_time), |
|---|
| 116 | time_duration_type(not_a_date_time)); |
|---|
| 117 | } |
|---|
| 118 | else { |
|---|
| 119 | return time_rep_type(date_type(neg_infin), tod); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | return time_rep_type(day, tod); |
|---|
| 124 | } |
|---|
| 125 | static date_type get_date(const time_rep_type& val) |
|---|
| 126 | { |
|---|
| 127 | return date_type(val.day); |
|---|
| 128 | } |
|---|
| 129 | static time_duration_type get_time_of_day(const time_rep_type& val) |
|---|
| 130 | { |
|---|
| 131 | return time_duration_type(val.time_of_day); |
|---|
| 132 | } |
|---|
| 133 | static std::string zone_name(const time_rep_type&) |
|---|
| 134 | { |
|---|
| 135 | return ""; |
|---|
| 136 | } |
|---|
| 137 | static bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs) |
|---|
| 138 | { |
|---|
| 139 | return ((lhs.day == rhs.day) && (lhs.time_of_day == rhs.time_of_day)); |
|---|
| 140 | } |
|---|
| 141 | static bool is_less(const time_rep_type& lhs, const time_rep_type& rhs) |
|---|
| 142 | { |
|---|
| 143 | if (lhs.day < rhs.day) return true; |
|---|
| 144 | if (lhs.day > rhs.day) return false; |
|---|
| 145 | return (lhs.time_of_day < rhs.time_of_day); |
|---|
| 146 | } |
|---|
| 147 | static time_rep_type add_days(const time_rep_type& base, |
|---|
| 148 | const date_duration_type& dd) |
|---|
| 149 | { |
|---|
| 150 | return time_rep_type(base.day+dd, base.time_of_day); |
|---|
| 151 | } |
|---|
| 152 | static time_rep_type subtract_days(const time_rep_type& base, |
|---|
| 153 | const date_duration_type& dd) |
|---|
| 154 | { |
|---|
| 155 | return split_timedate_system::get_time_rep(base.day-dd, base.time_of_day); |
|---|
| 156 | } |
|---|
| 157 | static time_rep_type subtract_time_duration(const time_rep_type& base, |
|---|
| 158 | const time_duration_type& td) |
|---|
| 159 | { |
|---|
| 160 | if(base.day.is_special() || td.is_special()) |
|---|
| 161 | { |
|---|
| 162 | return split_timedate_system::get_time_rep(base.day, -td); |
|---|
| 163 | } |
|---|
| 164 | if (td.is_negative()) { |
|---|
| 165 | time_duration_type td1 = td.invert_sign(); |
|---|
| 166 | return add_time_duration(base,td1); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | //std::cout << td.ticks() << std::endl; |
|---|
| 170 | wrap_int_type day_offset(base.time_of_day.ticks()); |
|---|
| 171 | date_duration_type day_overflow(static_cast<typename date_duration_type::duration_rep_type>(day_offset.subtract(td.ticks()))); |
|---|
| 172 | // std::cout << "sub: " << base.time_of_day.ticks() << "|" |
|---|
| 173 | // << day_offset.as_int() << "|" |
|---|
| 174 | // << day_overflow.days() << std::endl; |
|---|
| 175 | return time_rep_type(base.day-day_overflow, |
|---|
| 176 | time_duration_type(0,0,0,day_offset.as_int())); |
|---|
| 177 | } |
|---|
| 178 | static time_rep_type add_time_duration(const time_rep_type& base, |
|---|
| 179 | time_duration_type td) |
|---|
| 180 | { |
|---|
| 181 | if(base.day.is_special() || td.is_special()) { |
|---|
| 182 | return split_timedate_system::get_time_rep(base.day, td); |
|---|
| 183 | } |
|---|
| 184 | if (td.is_negative()) { |
|---|
| 185 | time_duration_type td1 = td.invert_sign(); |
|---|
| 186 | return subtract_time_duration(base,td1); |
|---|
| 187 | } |
|---|
| 188 | wrap_int_type day_offset(base.time_of_day.ticks()); |
|---|
| 189 | typename date_duration_type::duration_rep_type doff = day_offset.add(td.ticks()); |
|---|
| 190 | // std::cout << "day overflow: " << doff << std::endl; |
|---|
| 191 | // std::cout << "ticks: " << td.ticks() << std::endl; |
|---|
| 192 | date_duration_type day_overflow(doff); |
|---|
| 193 | // std::cout << "base: " << to_simple_string(base.day) << std::endl; |
|---|
| 194 | // std::cout << "overflow " << day_overflow.days() << std::endl; |
|---|
| 195 | return time_rep_type(base.day+day_overflow, |
|---|
| 196 | time_duration_type(0,0,0,day_offset.as_int())); |
|---|
| 197 | } |
|---|
| 198 | static time_duration_type subtract_times(const time_rep_type& lhs, |
|---|
| 199 | const time_rep_type& rhs) |
|---|
| 200 | { |
|---|
| 201 | date_duration_type dd = lhs.day - rhs.day; |
|---|
| 202 | time_duration_type td(dd.days()*24,0,0); //days * 24 hours |
|---|
| 203 | time_duration_type td2 = lhs.time_of_day - rhs.time_of_day; |
|---|
| 204 | return td+td2; |
|---|
| 205 | // return time_rep_type(base.day-dd, base.time_of_day); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | } } //namespace date_time |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | #endif |
|---|