123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #ifndef DATE_TIME_LOCAL_TIME_ADJUSTOR_HPP__
- #define DATE_TIME_LOCAL_TIME_ADJUSTOR_HPP__
- #include <stdexcept>
- #include <boost/throw_exception.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/date_generators.hpp>
- #include <boost/date_time/dst_rules.hpp>
- #include <boost/date_time/time_defs.hpp> // boost::date_time::dst_flags
- #include <boost/date_time/special_defs.hpp> // not_a_date_time
- namespace boost {
- namespace date_time {
-
- template<class time_duration_type,
- short hours, unsigned short minutes = 0>
- class utc_adjustment
- {
- public:
- static time_duration_type local_to_utc_base_offset()
- {
- time_duration_type td(hours,minutes,0);
- return td.invert_sign();
- }
- static time_duration_type utc_to_local_base_offset()
- {
- return time_duration_type(hours,minutes,0);
- }
- };
-
- template<class time_type, class dst_rules>
- class dynamic_local_time_adjustor : public dst_rules
- {
- public:
- typedef typename time_type::time_duration_type time_duration_type;
- typedef typename time_type::date_type date_type;
- dynamic_local_time_adjustor(time_duration_type utc_offset) :
- utc_offset_(utc_offset)
- {}
-
-
- time_duration_type utc_offset(bool is_dst)
- {
- if (is_dst) {
- return utc_offset_ + this->dst_offset();
- }
- else {
- return utc_offset_;
- }
- }
- private:
- time_duration_type utc_offset_;
- };
-
- template<class time_type, class dst_rules, class utc_offset_rules>
- class static_local_time_adjustor: public dst_rules, public utc_offset_rules
- {
- public:
- typedef typename time_type::time_duration_type time_duration_type;
- typedef typename time_type::date_type date_type;
-
-
- static time_duration_type utc_to_local_offset(const time_type& t)
- {
-
- time_type initial = t + utc_offset_rules::utc_to_local_base_offset();
- time_is_dst_result dst_flag =
- dst_rules::local_is_dst(initial.date(), initial.time_of_day());
- switch(dst_flag) {
- case is_in_dst: return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
- case is_not_in_dst: return utc_offset_rules::utc_to_local_base_offset();
- case invalid_time_label:return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
- case ambiguous: {
- time_type retry = initial + dst_rules::dst_offset();
- dst_flag = dst_rules::local_is_dst(retry.date(), retry.time_of_day());
-
- if (dst_flag == ambiguous) {
- return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
- }
-
- else {
- return utc_offset_rules::utc_to_local_base_offset();
- }
- }
- }
-
- boost::throw_exception(std::out_of_range("Unreachable case"));
- BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time));
- }
-
- static time_duration_type local_to_utc_offset(const time_type& t,
- date_time::dst_flags dst=date_time::calculate)
- {
- switch (dst) {
- case is_dst:
- return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
- case not_dst:
- return utc_offset_rules::local_to_utc_base_offset();
- case calculate:
- time_is_dst_result res =
- dst_rules::local_is_dst(t.date(), t.time_of_day());
- switch(res) {
- case is_in_dst: return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
- case is_not_in_dst: return utc_offset_rules::local_to_utc_base_offset();
- case ambiguous: return utc_offset_rules::local_to_utc_base_offset();
- case invalid_time_label: break;
- }
- }
- boost::throw_exception(std::out_of_range("Time label invalid"));
- BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time));
- }
- private:
- };
- void dummy_to_prevent_msvc6_ice();
-
-
- template<class time_type, short utc_offset, class dst_rule>
- class local_adjustor
- {
- public:
- typedef typename time_type::time_duration_type time_duration_type;
- typedef typename time_type::date_type date_type;
- typedef static_local_time_adjustor<time_type,
- dst_rule,
- utc_adjustment<time_duration_type,
- utc_offset> > dst_adjustor;
-
- static time_type utc_to_local(const time_type& t)
- {
- time_duration_type td = dst_adjustor::utc_to_local_offset(t);
- return t + td;
- }
-
- static time_type local_to_utc(const time_type& t,
- date_time::dst_flags dst=date_time::calculate)
- {
- time_duration_type td = dst_adjustor::local_to_utc_offset(t, dst);
- return t + td;
- }
- };
- } }
- #endif
|