123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- #ifndef DATE_TIME_DST_RULES_HPP__
- #define DATE_TIME_DST_RULES_HPP__
- #include "boost/date_time/date_generators.hpp"
- #include "boost/date_time/period.hpp"
- #include "boost/date_time/date_defs.hpp"
- #include <stdexcept>
- namespace boost {
- namespace date_time {
- enum time_is_dst_result {is_not_in_dst, is_in_dst,
- ambiguous, invalid_time_label};
-
- template<class date_type_,
- class time_duration_type_>
- class dst_calculator
- {
- public:
- typedef time_duration_type_ time_duration_type;
- typedef date_type_ date_type;
-
-
- static time_is_dst_result
- process_local_dst_start_day(const time_duration_type& time_of_day,
- unsigned int dst_start_offset_minutes,
- long dst_length_minutes)
- {
-
- if (time_of_day < time_duration_type(0,dst_start_offset_minutes,0)) {
- return is_not_in_dst;
- }
- long offset = dst_start_offset_minutes + dst_length_minutes;
- if (time_of_day >= time_duration_type(0,offset,0)) {
- return is_in_dst;
- }
- return invalid_time_label;
- }
-
-
- static time_is_dst_result
- process_local_dst_end_day(const time_duration_type& time_of_day,
- unsigned int dst_end_offset_minutes,
- long dst_length_minutes)
- {
-
- int offset = dst_end_offset_minutes-dst_length_minutes;
- if (time_of_day < time_duration_type(0,offset,0)) {
- return is_in_dst;
- }
- if (time_of_day >= time_duration_type(0,dst_end_offset_minutes,0)) {
- return is_not_in_dst;
- }
- return ambiguous;
- }
-
-
- static time_is_dst_result
- local_is_dst(const date_type& current_day,
- const time_duration_type& time_of_day,
- const date_type& dst_start_day,
- const time_duration_type& dst_start_offset,
- const date_type& dst_end_day,
- const time_duration_type& dst_end_offset,
- const time_duration_type& dst_length)
- {
- unsigned int start_minutes = static_cast<unsigned>(
- dst_start_offset.hours() * 60 + dst_start_offset.minutes());
- unsigned int end_minutes = static_cast<unsigned>(
- dst_end_offset.hours() * 60 + dst_end_offset.minutes());
- long length_minutes = static_cast<long>(
- dst_length.hours() * 60 + dst_length.minutes());
- return local_is_dst(current_day, time_of_day,
- dst_start_day, start_minutes,
- dst_end_day, end_minutes,
- length_minutes);
- }
-
-
- static time_is_dst_result
- local_is_dst(const date_type& current_day,
- const time_duration_type& time_of_day,
- const date_type& dst_start_day,
- unsigned int dst_start_offset_minutes,
- const date_type& dst_end_day,
- unsigned int dst_end_offset_minutes,
- long dst_length_minutes)
- {
-
- if (dst_start_day < dst_end_day) {
- if ((current_day > dst_start_day) && (current_day < dst_end_day)) {
- return is_in_dst;
- }
- if ((current_day < dst_start_day) || (current_day > dst_end_day)) {
- return is_not_in_dst;
- }
- }
- else {
- if ((current_day < dst_start_day) && (current_day > dst_end_day)) {
- return is_not_in_dst;
- }
- if ((current_day > dst_start_day) || (current_day < dst_end_day)) {
- return is_in_dst;
- }
- }
- if (current_day == dst_start_day) {
- return process_local_dst_start_day(time_of_day,
- dst_start_offset_minutes,
- dst_length_minutes);
- }
- if (current_day == dst_end_day) {
- return process_local_dst_end_day(time_of_day,
- dst_end_offset_minutes,
- dst_length_minutes);
- }
-
- return invalid_time_label;
- }
- };
-
-
- template<class date_type,
- class time_duration_type,
- class dst_traits>
- class dst_calc_engine
- {
- public:
- typedef typename date_type::year_type year_type;
- typedef typename date_type::calendar_type calendar_type;
- typedef dst_calculator<date_type, time_duration_type> dstcalc;
-
-
- static time_is_dst_result local_is_dst(const date_type& d,
- const time_duration_type& td)
- {
- year_type y = d.year();
- date_type dst_start = local_dst_start_day(y);
- date_type dst_end = local_dst_end_day(y);
- return dstcalc::local_is_dst(d,td,
- dst_start,
- dst_traits::dst_start_offset_minutes(),
- dst_end,
- dst_traits::dst_end_offset_minutes(),
- dst_traits::dst_shift_length_minutes());
- }
- static bool is_dst_boundary_day(date_type d)
- {
- year_type y = d.year();
- return ((d == local_dst_start_day(y)) ||
- (d == local_dst_end_day(y)));
- }
-
- static time_duration_type dst_offset()
- {
- return time_duration_type(0,dst_traits::dst_shift_length_minutes(),0);
- }
- static date_type local_dst_start_day(year_type year)
- {
- return dst_traits::local_dst_start_day(year);
- }
- static date_type local_dst_end_day(year_type year)
- {
- return dst_traits::local_dst_end_day(year);
- }
- };
-
-
- template<class date_type_,
- class time_duration_type_,
- unsigned int dst_start_offset_minutes=120, //from start of day
- short dst_length_minutes=60> //1 hour == 60 min in US
- class us_dst_rules
- {
- public:
- typedef time_duration_type_ time_duration_type;
- typedef date_type_ date_type;
- typedef typename date_type::year_type year_type;
- typedef typename date_type::calendar_type calendar_type;
- typedef date_time::last_kday_of_month<date_type> lkday;
- typedef date_time::first_kday_of_month<date_type> fkday;
- typedef date_time::nth_kday_of_month<date_type> nkday;
- typedef dst_calculator<date_type, time_duration_type> dstcalc;
-
-
- static time_is_dst_result local_is_dst(const date_type& d,
- const time_duration_type& td)
- {
- year_type y = d.year();
- date_type dst_start = local_dst_start_day(y);
- date_type dst_end = local_dst_end_day(y);
- return dstcalc::local_is_dst(d,td,
- dst_start,dst_start_offset_minutes,
- dst_end, dst_start_offset_minutes,
- dst_length_minutes);
- }
- static bool is_dst_boundary_day(date_type d)
- {
- year_type y = d.year();
- return ((d == local_dst_start_day(y)) ||
- (d == local_dst_end_day(y)));
- }
- static date_type local_dst_start_day(year_type year)
- {
- if (year >= year_type(2007)) {
-
- nkday ssim(nkday::second, Sunday, date_time::Mar);
- return ssim.get_date(year);
- } else {
-
- fkday fsia(Sunday, date_time::Apr);
- return fsia.get_date(year);
- }
- }
- static date_type local_dst_end_day(year_type year)
- {
- if (year >= year_type(2007)) {
-
- fkday fsin(Sunday, date_time::Nov);
- return fsin.get_date(year);
- } else {
-
- lkday lsio(Sunday, date_time::Oct);
- return lsio.get_date(year);
- }
- }
- static time_duration_type dst_offset()
- {
- return time_duration_type(0,dst_length_minutes,0);
- }
- private:
- };
-
- template<class date_type_, class time_duration_type_>
- class null_dst_rules
- {
- public:
- typedef time_duration_type_ time_duration_type;
- typedef date_type_ date_type;
-
-
- static time_is_dst_result local_is_dst(const date_type&,
- const time_duration_type&)
- {
- return is_not_in_dst;
- }
-
- static time_is_dst_result utc_is_dst(const date_type&,
- const time_duration_type&)
- {
- return is_not_in_dst;
- }
- static bool is_dst_boundary_day(date_type )
- {
- return false;
- }
- static time_duration_type dst_offset()
- {
- return time_duration_type(0,0,0);
- }
- };
- } }
- #endif
|