time_system_counted.hpp 8.2 KB

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