time_system_split.hpp 7.6 KB

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