time_system_counted.hpp 8.8 KB

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