local_timezone_defs.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef DATE_TIME_LOCAL_TIMEZONE_DEFS_HPP__
  2. #define DATE_TIME_LOCAL_TIMEZONE_DEFS_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. #include "boost/date_time/dst_rules.hpp"
  11. namespace boost {
  12. namespace date_time {
  13. // Configurations for common dst rules cases:
  14. // See http://www.wharton.co.uk/Support/sup_dst.htm for more
  15. // information on how various locales use dst rules
  16. //! Specification for daylight savings start rules in US
  17. /*! This class is used to configure dst_calc_engine template typically
  18. as follows:
  19. @code
  20. using namespace boost::gregorian;
  21. using namespace boost::posix_time;
  22. typedef us_dst_trait<date> us_dst_traits;
  23. typedef boost::date_time::dst_calc_engine<date, time_duration,
  24. us_dst_traits>
  25. us_dst_calc;
  26. //calculate the 2002 transition day of USA April 7 2002
  27. date dst_start = us_dst_calc::local_dst_start_day(2002);
  28. //calculate the 2002 transition day of USA Oct 27 2002
  29. date dst_end = us_dst_calc::local_dst_end_day(2002);
  30. //check if a local time is in dst or not -- posible answers
  31. //are yes, no, invalid time label, ambiguous
  32. ptime t(...some time...);
  33. if (us_dst::local_is_dst(t.date(), t.time_of_day())
  34. == boost::date_time::is_not_in_dst)
  35. {
  36. }
  37. @endcode
  38. This generates a type suitable for the calculation of dst
  39. transitions for the United States. Of course other templates
  40. can be used for other locales.
  41. */
  42. template<class date_type>
  43. struct us_dst_trait
  44. {
  45. typedef typename date_type::day_of_week_type day_of_week_type;
  46. typedef typename date_type::month_type month_type;
  47. typedef typename date_type::year_type year_type;
  48. typedef date_time::nth_kday_of_month<date_type> start_rule_functor;
  49. typedef date_time::first_kday_of_month<date_type> end_rule_functor;
  50. typedef date_time::first_kday_of_month<date_type> start_rule_functor_pre2007;
  51. typedef date_time::last_kday_of_month<date_type> end_rule_functor_pre2007;
  52. static day_of_week_type start_day(year_type) {return Sunday;}
  53. static month_type start_month(year_type y)
  54. {
  55. if (y < 2007) return Apr;
  56. return Mar;
  57. }
  58. static day_of_week_type end_day(year_type) {return Sunday;}
  59. static month_type end_month(year_type y)
  60. {
  61. if (y < 2007) return Oct;
  62. return Nov;
  63. }
  64. static date_type local_dst_start_day(year_type year)
  65. {
  66. if (year < 2007) {
  67. start_rule_functor_pre2007 start1(start_day(year),
  68. start_month(year));
  69. return start1.get_date(year);
  70. }
  71. start_rule_functor start(start_rule_functor::second,
  72. start_day(year),
  73. start_month(year));
  74. return start.get_date(year);
  75. }
  76. static date_type local_dst_end_day(year_type year)
  77. {
  78. if (year < 2007) {
  79. end_rule_functor_pre2007 end_rule(end_day(year),
  80. end_month(year));
  81. return end_rule.get_date(year);
  82. }
  83. end_rule_functor end(end_day(year),
  84. end_month(year));
  85. return end.get_date(year);
  86. }
  87. static int dst_start_offset_minutes() { return 120;}
  88. static int dst_end_offset_minutes() { return 120; }
  89. static int dst_shift_length_minutes() { return 60; }
  90. };
  91. //!Rules for daylight savings start in the EU (Last Sun in Mar)
  92. /*!These amount to the following:
  93. - Start of dst day is last Sunday in March
  94. - End day of dst is last Sunday in Oct
  95. - Going forward switch time is 2:00 am (offset 120 minutes)
  96. - Going back switch time is 3:00 am (off set 180 minutes)
  97. - Shift duration is one hour (60 minutes)
  98. */
  99. template<class date_type>
  100. struct eu_dst_trait
  101. {
  102. typedef typename date_type::day_of_week_type day_of_week_type;
  103. typedef typename date_type::month_type month_type;
  104. typedef typename date_type::year_type year_type;
  105. typedef date_time::last_kday_of_month<date_type> start_rule_functor;
  106. typedef date_time::last_kday_of_month<date_type> end_rule_functor;
  107. static day_of_week_type start_day(year_type) {return Sunday;}
  108. static month_type start_month(year_type) {return Mar;}
  109. static day_of_week_type end_day(year_type) {return Sunday;}
  110. static month_type end_month(year_type) {return Oct;}
  111. static int dst_start_offset_minutes() { return 120;}
  112. static int dst_end_offset_minutes() { return 180; }
  113. static int dst_shift_length_minutes() { return 60; }
  114. static date_type local_dst_start_day(year_type year)
  115. {
  116. start_rule_functor start(start_day(year),
  117. start_month(year));
  118. return start.get_date(year);
  119. }
  120. static date_type local_dst_end_day(year_type year)
  121. {
  122. end_rule_functor end(end_day(year),
  123. end_month(year));
  124. return end.get_date(year);
  125. }
  126. };
  127. //! Alternative dst traits for some parts of the United Kingdom
  128. /* Several places in the UK use EU start and end rules for the
  129. day, but different local conversion times (eg: forward change at 1:00
  130. am local and backward change at 2:00 am dst instead of 2:00am
  131. forward and 3:00am back for the EU).
  132. */
  133. template<class date_type>
  134. struct uk_dst_trait : public eu_dst_trait<date_type>
  135. {
  136. static int dst_start_offset_minutes() { return 60;}
  137. static int dst_end_offset_minutes() { return 120; }
  138. static int dst_shift_length_minutes() { return 60; }
  139. };
  140. //Rules for Adelaide Australia
  141. template<class date_type>
  142. struct acst_dst_trait
  143. {
  144. typedef typename date_type::day_of_week_type day_of_week_type;
  145. typedef typename date_type::month_type month_type;
  146. typedef typename date_type::year_type year_type;
  147. typedef date_time::last_kday_of_month<date_type> start_rule_functor;
  148. typedef date_time::last_kday_of_month<date_type> end_rule_functor;
  149. static day_of_week_type start_day(year_type) {return Sunday;}
  150. static month_type start_month(year_type) {return Oct;}
  151. static day_of_week_type end_day(year_type) {return Sunday;}
  152. static month_type end_month(year_type) {return Mar;}
  153. static int dst_start_offset_minutes() { return 120;}
  154. static int dst_end_offset_minutes() { return 180; }
  155. static int dst_shift_length_minutes() { return 60; }
  156. static date_type local_dst_start_day(year_type year)
  157. {
  158. start_rule_functor start(start_day(year),
  159. start_month(year));
  160. return start.get_date(year);
  161. }
  162. static date_type local_dst_end_day(year_type year)
  163. {
  164. end_rule_functor end(end_day(year),
  165. end_month(year));
  166. return end.get_date(year);
  167. }
  168. };
  169. } } //namespace boost::date_time
  170. #endif