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