time_system_split.hpp 7.5 KB

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