time_system_split.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef DATE_TIME_TIME_SYSTEM_SPLIT_HPP
  2. #define DATE_TIME_TIME_SYSTEM_SPLIT_HPP
  3. /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include <string>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/date_time/compiler_config.hpp>
  13. #include <boost/date_time/time_defs.hpp>
  14. #include <boost/date_time/special_defs.hpp>
  15. #include <boost/date_time/wrapping_int.hpp>
  16. namespace boost {
  17. namespace date_time {
  18. //! An unadjusted time system implementation.
  19. #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT))
  20. template<typename config, boost::int32_t ticks_per_second>
  21. #else
  22. template<typename config>
  23. #endif
  24. class split_timedate_system
  25. {
  26. public:
  27. typedef typename config::time_rep_type time_rep_type;
  28. typedef typename config::date_type date_type;
  29. typedef typename config::time_duration_type time_duration_type;
  30. typedef typename config::date_duration_type date_duration_type;
  31. typedef typename config::int_type int_type;
  32. typedef typename config::resolution_traits resolution_traits;
  33. //86400 is number of seconds in a day...
  34. #if (defined(BOOST_DATE_TIME_NO_MEMBER_INIT))
  35. typedef date_time::wrapping_int<int_type, INT64_C(86400) * ticks_per_second > wrap_int_type;
  36. #else
  37. private:
  38. BOOST_STATIC_CONSTANT(int_type, ticks_per_day = INT64_C(86400) * config::tick_per_second);
  39. public:
  40. # if BOOST_WORKAROUND( BOOST_BORLANDC, BOOST_TESTED_AT(0X581) )
  41. typedef date_time::wrapping_int< split_timedate_system::int_type, split_timedate_system::ticks_per_day> wrap_int_type;
  42. # else
  43. typedef date_time::wrapping_int<int_type, ticks_per_day> wrap_int_type;
  44. #endif
  45. #endif
  46. static
  47. BOOST_CXX14_CONSTEXPR
  48. time_rep_type get_time_rep(special_values sv)
  49. {
  50. switch (sv) {
  51. case not_a_date_time:
  52. return time_rep_type(date_type(not_a_date_time),
  53. time_duration_type(not_a_date_time));
  54. case pos_infin:
  55. return time_rep_type(date_type(pos_infin),
  56. time_duration_type(pos_infin));
  57. case neg_infin:
  58. return time_rep_type(date_type(neg_infin),
  59. time_duration_type(neg_infin));
  60. case max_date_time: {
  61. time_duration_type td = time_duration_type(24,0,0,0) - time_duration_type(0,0,0,1);
  62. return time_rep_type(date_type(max_date_time), td);
  63. }
  64. case min_date_time:
  65. return time_rep_type(date_type(min_date_time), time_duration_type(0,0,0,0));
  66. default:
  67. return time_rep_type(date_type(not_a_date_time),
  68. time_duration_type(not_a_date_time));
  69. }
  70. }
  71. static
  72. BOOST_CXX14_CONSTEXPR
  73. 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 BOOST_CONSTEXPR date_type get_date(const time_rep_type& val)
  122. {
  123. return date_type(val.day);
  124. }
  125. static BOOST_CONSTEXPR 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 std::string();
  132. }
  133. static BOOST_CONSTEXPR
  134. bool is_equal(const time_rep_type& lhs, const time_rep_type& rhs)
  135. {
  136. return ((lhs.day == rhs.day) && (lhs.time_of_day == rhs.time_of_day));
  137. }
  138. static BOOST_CXX14_CONSTEXPR
  139. bool is_less(const time_rep_type& lhs, const time_rep_type& rhs)
  140. {
  141. if (lhs.day < rhs.day) return true;
  142. if (lhs.day > rhs.day) return false;
  143. return (lhs.time_of_day < rhs.time_of_day);
  144. }
  145. static BOOST_CXX14_CONSTEXPR
  146. time_rep_type add_days(const time_rep_type& base,
  147. const date_duration_type& dd)
  148. {
  149. return time_rep_type(base.day+dd, base.time_of_day);
  150. }
  151. static BOOST_CXX14_CONSTEXPR
  152. 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 BOOST_CXX14_CONSTEXPR
  158. time_rep_type subtract_time_duration(const time_rep_type& base,
  159. const time_duration_type& td)
  160. {
  161. if(base.day.is_special() || td.is_special())
  162. {
  163. return split_timedate_system::get_time_rep(base.day, -td);
  164. }
  165. if (td.is_negative()) {
  166. time_duration_type td1 = td.invert_sign();
  167. return add_time_duration(base,td1);
  168. }
  169. wrap_int_type day_offset(base.time_of_day.ticks());
  170. date_duration_type day_overflow(static_cast<typename date_duration_type::duration_rep_type>(day_offset.subtract(td.ticks())));
  171. return time_rep_type(base.day-day_overflow,
  172. time_duration_type(0,0,0,day_offset.as_int()));
  173. }
  174. static BOOST_CXX14_CONSTEXPR
  175. time_rep_type add_time_duration(const time_rep_type& base,
  176. time_duration_type td)
  177. {
  178. if(base.day.is_special() || td.is_special()) {
  179. return split_timedate_system::get_time_rep(base.day, td);
  180. }
  181. if (td.is_negative()) {
  182. time_duration_type td1 = td.invert_sign();
  183. return subtract_time_duration(base,td1);
  184. }
  185. wrap_int_type day_offset(base.time_of_day.ticks());
  186. date_duration_type day_overflow(static_cast< typename date_duration_type::duration_rep_type >(day_offset.add(td.ticks())));
  187. return time_rep_type(base.day+day_overflow,
  188. time_duration_type(0,0,0,day_offset.as_int()));
  189. }
  190. static BOOST_CXX14_CONSTEXPR
  191. time_duration_type subtract_times(const time_rep_type& lhs,
  192. const time_rep_type& rhs)
  193. {
  194. date_duration_type dd = lhs.day - rhs.day;
  195. if (BOOST_LIKELY(!dd.is_special())) {
  196. time_duration_type td(dd.days()*24,0,0); // days * 24 hours
  197. time_duration_type td2 = lhs.time_of_day - rhs.time_of_day;
  198. return td+td2;
  199. } else {
  200. time_duration_type td(dd.as_special());
  201. time_duration_type td2 = lhs.time_of_day - rhs.time_of_day;
  202. return td+td2;
  203. }
  204. }
  205. };
  206. } } //namespace date_time
  207. #endif