local_time_adjustor.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef DATE_TIME_LOCAL_TIME_ADJUSTOR_HPP__
  2. #define DATE_TIME_LOCAL_TIME_ADJUSTOR_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
  8. * $Date$
  9. */
  10. /*! @file local_time_adjustor.hpp
  11. Time adjustment calculations for local times
  12. */
  13. #include <stdexcept>
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/date_time/compiler_config.hpp>
  16. #include <boost/date_time/date_generators.hpp>
  17. #include <boost/date_time/dst_rules.hpp>
  18. #include <boost/date_time/time_defs.hpp> // boost::date_time::dst_flags
  19. #include <boost/date_time/special_defs.hpp> // not_a_date_time
  20. namespace boost {
  21. namespace date_time {
  22. //! Provides a base offset adjustment from utc
  23. template<class time_duration_type,
  24. short hours, unsigned short minutes = 0>
  25. class utc_adjustment
  26. {
  27. public:
  28. static time_duration_type local_to_utc_base_offset()
  29. {
  30. time_duration_type td(hours,minutes,0);
  31. return td.invert_sign();
  32. }
  33. static time_duration_type utc_to_local_base_offset()
  34. {
  35. return time_duration_type(hours,minutes,0);
  36. }
  37. };
  38. //! Allow sliding utc adjustment with fixed dst rules
  39. template<class time_type, class dst_rules>
  40. class dynamic_local_time_adjustor : public dst_rules
  41. {
  42. public:
  43. typedef typename time_type::time_duration_type time_duration_type;
  44. typedef typename time_type::date_type date_type;
  45. dynamic_local_time_adjustor(time_duration_type utc_offset) :
  46. utc_offset_(utc_offset)
  47. {}
  48. //! Presumes local time
  49. time_duration_type utc_offset(bool is_dst)
  50. {
  51. if (is_dst) {
  52. return utc_offset_ + this->dst_offset();
  53. }
  54. else {
  55. return utc_offset_;
  56. }
  57. }
  58. private:
  59. time_duration_type utc_offset_;
  60. };
  61. //! Embed the rules for local time adjustments at compile time
  62. template<class time_type, class dst_rules, class utc_offset_rules>
  63. class static_local_time_adjustor: public dst_rules, public utc_offset_rules
  64. {
  65. public:
  66. typedef typename time_type::time_duration_type time_duration_type;
  67. typedef typename time_type::date_type date_type;
  68. //! Calculates the offset from a utc time to local based on dst and utc offset
  69. /*! @param t UTC time to calculate offset to local time
  70. * This adjustment depends on the following observations about the
  71. * workings of the DST boundary offset. Since UTC time labels are
  72. * monotonically increasing we can determine if a given local time
  73. * is in DST or not and therefore adjust the offset appropriately.
  74. *
  75. * The logic is as follows. Starting with UTC time use the offset to
  76. * create a label for an non-dst adjusted local time. Then call
  77. * dst_rules::local_is_dst with the non adjust local time. The
  78. * results of this function will either unabiguously decide that
  79. * the initial local time is in dst or return an illegal or
  80. * ambiguous result. An illegal result only occurs at the end
  81. * of dst (where labels are skipped) and indicates that dst has
  82. * ended. An ambiguous result means that we need to recheck by
  83. * making a dst adjustment and then rechecking. If the dst offset
  84. * is added to the utc time and the recheck proves non-ambiguous
  85. * then we are past the boundary. If it is still ambiguous then
  86. * we are ahead of the boundary and dst is still in effect.
  87. *
  88. * TODO -- check if all dst offsets are positive. If not then
  89. * the algorithm needs to check for this and reverse the
  90. * illegal/ambiguous logic.
  91. */
  92. static time_duration_type utc_to_local_offset(const time_type& t)
  93. {
  94. //get initial local time guess by applying utc offset
  95. time_type initial = t + utc_offset_rules::utc_to_local_base_offset();
  96. time_is_dst_result dst_flag =
  97. dst_rules::local_is_dst(initial.date(), initial.time_of_day());
  98. switch(dst_flag) {
  99. case is_in_dst: return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
  100. case is_not_in_dst: return utc_offset_rules::utc_to_local_base_offset();
  101. case invalid_time_label:return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
  102. case ambiguous: {
  103. time_type retry = initial + dst_rules::dst_offset();
  104. dst_flag = dst_rules::local_is_dst(retry.date(), retry.time_of_day());
  105. //if still ambibuous then the utc time still translates to a dst time
  106. if (dst_flag == ambiguous) {
  107. return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
  108. }
  109. // we are past the dst boundary
  110. else {
  111. return utc_offset_rules::utc_to_local_base_offset();
  112. }
  113. }
  114. }//case
  115. //TODO better exception type
  116. boost::throw_exception(std::out_of_range("Unreachable case"));
  117. BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time)); // should never reach
  118. }
  119. //! Get the offset to UTC given a local time
  120. static time_duration_type local_to_utc_offset(const time_type& t,
  121. date_time::dst_flags dst=date_time::calculate)
  122. {
  123. switch (dst) {
  124. case is_dst:
  125. return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
  126. case not_dst:
  127. return utc_offset_rules::local_to_utc_base_offset();
  128. case calculate:
  129. time_is_dst_result res =
  130. dst_rules::local_is_dst(t.date(), t.time_of_day());
  131. switch(res) {
  132. case is_in_dst: return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
  133. case is_not_in_dst: return utc_offset_rules::local_to_utc_base_offset();
  134. case ambiguous: return utc_offset_rules::local_to_utc_base_offset();
  135. case invalid_time_label: break;
  136. }
  137. }
  138. boost::throw_exception(std::out_of_range("Time label invalid"));
  139. BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time)); // should never reach
  140. }
  141. private:
  142. };
  143. void dummy_to_prevent_msvc6_ice(); //why ask why?
  144. //! Template that simplifies the creation of local time calculator
  145. /*! Use this template to create the timezone to utc convertors as required.
  146. *
  147. * This class will also work for other regions that don't use dst and
  148. * have a utc offset which is an integral number of hours.
  149. *
  150. * <b>Template Parameters</b>
  151. * -time_type -- Time class to use
  152. * -utc_offset -- Number hours local time is adjust from utc
  153. * -use_dst -- true (default) if region uses dst, false otherwise
  154. * For example:
  155. * @code
  156. * //eastern timezone is utc-5
  157. typedef date_time::local_adjustor<ptime, -5, us_dst> us_eastern;
  158. typedef date_time::local_adjustor<ptime, -6, us_dst> us_central;
  159. typedef date_time::local_adjustor<ptime, -7, us_dst> us_mountain;
  160. typedef date_time::local_adjustor<ptime, -8, us_dst> us_pacific;
  161. typedef date_time::local_adjustor<ptime, -7, no_dst> us_arizona;
  162. @endcode
  163. */
  164. template<class time_type, short utc_offset, class dst_rule>
  165. class local_adjustor
  166. {
  167. public:
  168. typedef typename time_type::time_duration_type time_duration_type;
  169. typedef typename time_type::date_type date_type;
  170. typedef static_local_time_adjustor<time_type,
  171. dst_rule,
  172. utc_adjustment<time_duration_type,
  173. utc_offset> > dst_adjustor;
  174. //! Convert a utc time to local time
  175. static time_type utc_to_local(const time_type& t)
  176. {
  177. time_duration_type td = dst_adjustor::utc_to_local_offset(t);
  178. return t + td;
  179. }
  180. //! Convert a local time to utc
  181. static time_type local_to_utc(const time_type& t,
  182. date_time::dst_flags dst=date_time::calculate)
  183. {
  184. time_duration_type td = dst_adjustor::local_to_utc_offset(t, dst);
  185. return t + td;
  186. }
  187. };
  188. } } //namespace date_time
  189. #endif