File indexing completed on 2025-01-18 09:30:38
0001 #ifndef DATE_TIME_LOCAL_TIME_ADJUSTOR_HPP__
0002 #define DATE_TIME_LOCAL_TIME_ADJUSTOR_HPP__
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <stdexcept>
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/date_time/compiler_config.hpp>
0019 #include <boost/date_time/date_generators.hpp>
0020 #include <boost/date_time/dst_rules.hpp>
0021 #include <boost/date_time/time_defs.hpp> // boost::date_time::dst_flags
0022 #include <boost/date_time/special_defs.hpp> // not_a_date_time
0023
0024 namespace boost {
0025 namespace date_time {
0026
0027
0028
0029 template<class time_duration_type,
0030 short hours, unsigned short minutes = 0>
0031 class utc_adjustment
0032 {
0033 public:
0034 static time_duration_type local_to_utc_base_offset()
0035 {
0036 time_duration_type td(hours,minutes,0);
0037 return td.invert_sign();
0038 }
0039 static time_duration_type utc_to_local_base_offset()
0040 {
0041 return time_duration_type(hours,minutes,0);
0042 }
0043 };
0044
0045
0046
0047
0048 template<class time_type, class dst_rules>
0049 class dynamic_local_time_adjustor : public dst_rules
0050 {
0051 public:
0052 typedef typename time_type::time_duration_type time_duration_type;
0053 typedef typename time_type::date_type date_type;
0054
0055 dynamic_local_time_adjustor(time_duration_type utc_offset) :
0056 utc_offset_(utc_offset)
0057 {}
0058
0059
0060 time_duration_type utc_offset(bool is_dst)
0061 {
0062 if (is_dst) {
0063 return utc_offset_ + this->dst_offset();
0064 }
0065 else {
0066 return utc_offset_;
0067 }
0068
0069 }
0070 private:
0071 time_duration_type utc_offset_;
0072
0073 };
0074
0075
0076
0077
0078 template<class time_type, class dst_rules, class utc_offset_rules>
0079 class static_local_time_adjustor: public dst_rules, public utc_offset_rules
0080 {
0081 public:
0082 typedef typename time_type::time_duration_type time_duration_type;
0083 typedef typename time_type::date_type date_type;
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 static time_duration_type utc_to_local_offset(const time_type& t)
0110 {
0111
0112 time_type initial = t + utc_offset_rules::utc_to_local_base_offset();
0113 time_is_dst_result dst_flag =
0114 dst_rules::local_is_dst(initial.date(), initial.time_of_day());
0115 switch(dst_flag) {
0116 case is_in_dst: return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
0117 case is_not_in_dst: return utc_offset_rules::utc_to_local_base_offset();
0118 case invalid_time_label:return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
0119 case ambiguous: {
0120 time_type retry = initial + dst_rules::dst_offset();
0121 dst_flag = dst_rules::local_is_dst(retry.date(), retry.time_of_day());
0122
0123 if (dst_flag == ambiguous) {
0124 return utc_offset_rules::utc_to_local_base_offset() + dst_rules::dst_offset();
0125 }
0126
0127 else {
0128 return utc_offset_rules::utc_to_local_base_offset();
0129 }
0130 }
0131 }
0132
0133 boost::throw_exception(std::out_of_range("Unreachable case"));
0134 BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time));
0135 }
0136
0137
0138 static time_duration_type local_to_utc_offset(const time_type& t,
0139 date_time::dst_flags dst=date_time::calculate)
0140 {
0141 switch (dst) {
0142 case is_dst:
0143 return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
0144 case not_dst:
0145 return utc_offset_rules::local_to_utc_base_offset();
0146 case calculate:
0147 time_is_dst_result res =
0148 dst_rules::local_is_dst(t.date(), t.time_of_day());
0149 switch(res) {
0150 case is_in_dst: return utc_offset_rules::local_to_utc_base_offset() - dst_rules::dst_offset();
0151 case is_not_in_dst: return utc_offset_rules::local_to_utc_base_offset();
0152 case ambiguous: return utc_offset_rules::local_to_utc_base_offset();
0153 case invalid_time_label: break;
0154 }
0155 }
0156 boost::throw_exception(std::out_of_range("Time label invalid"));
0157 BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return time_duration_type(not_a_date_time));
0158 }
0159
0160
0161 private:
0162
0163 };
0164
0165 void dummy_to_prevent_msvc6_ice();
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 template<class time_type, short utc_offset, class dst_rule>
0189 class local_adjustor
0190 {
0191 public:
0192 typedef typename time_type::time_duration_type time_duration_type;
0193 typedef typename time_type::date_type date_type;
0194 typedef static_local_time_adjustor<time_type,
0195 dst_rule,
0196 utc_adjustment<time_duration_type,
0197 utc_offset> > dst_adjustor;
0198
0199 static time_type utc_to_local(const time_type& t)
0200 {
0201 time_duration_type td = dst_adjustor::utc_to_local_offset(t);
0202 return t + td;
0203 }
0204
0205 static time_type local_to_utc(const time_type& t,
0206 date_time::dst_flags dst=date_time::calculate)
0207 {
0208 time_duration_type td = dst_adjustor::local_to_utc_offset(t, dst);
0209 return t + td;
0210 }
0211 };
0212
0213
0214 } }
0215
0216
0217
0218 #endif