1 /* Copyright (c) 2002,2003, 2007 CrystalClear Software, Inc.
2 * Use, modification and distribution is subject to the
3 * Boost Software License, Version 1.0. (See accompanying
4 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
5 * Author: Jeff Garland
6 */
7
8 #include "boost/date_time/posix_time/posix_time.hpp"
9 #include "boost/date_time/local_time_adjustor.hpp"
10 #include "boost/date_time/local_timezone_defs.hpp"
11 #include "../testfrmwk.hpp"
12
13 int
main()14 main()
15 {
16 using namespace boost::posix_time;
17 using namespace boost::gregorian;
18
19 date dst_start(2002,Apr, 7);
20 date dst_end_day(2002,Oct, 27);
21
22 typedef boost::date_time::utc_adjustment<time_duration,-5> us_eastern_offset_adj;
23 //Type that embeds rules for UTC-5 plus DST offset
24 typedef boost::date_time::static_local_time_adjustor<ptime,
25 us_dst,
26 us_eastern_offset_adj> us_eastern;
27
28 //test some times clearly not in DST
29 date d3(2002,Feb,1);
30 ptime t10(d3, hours(4));
31 ptime t10_check(d3, hours(9)); //utc is 5 hours ahead
32 time_duration td = us_eastern::local_to_utc_offset(t10);//dst flag is defaulted
33 check("check local calculation", td == hours(5));
34 ptime t10_local = t10 + td;
35 std::cout << t10_local << std::endl;
36 check("check local calculation", t10_local == t10_check);
37 check("check utc is dst",
38 us_eastern::utc_to_local_offset(t10) == hours(-5));
39
40
41 //something clearly IN dst
42 date d4(2002,May,1);
43 ptime t11(d4, hours(3));
44 check("check local offset",us_eastern::local_to_utc_offset(t11) == hours(4));
45 std::cout << us_eastern::local_to_utc_offset(t11) << std::endl;
46 ptime t11_check(d4, hours(7));//now utc offset is only 4 hours
47 ptime t11_local = t11 + us_eastern::local_to_utc_offset(t11);
48 std::cout << t11_local << " " << t11_check << std::endl;
49 check("check local calculation", t11_local == t11_check);
50 //should get same offset with DST flag set
51 check("check local offset-dst flag on",
52 us_eastern::local_to_utc_offset(t11, boost::date_time::is_dst) == hours(4));
53 check("check local offset-dst flag override",
54 us_eastern::local_to_utc_offset(t11, boost::date_time::not_dst) == hours(5));
55
56
57 //Check the start of dst boundary
58 ptime l_not_dst(dst_start, time_duration(1,59,59)); //2002-Apr-07 01:59:59
59 check("check local dst start boundary case",
60 us_eastern::local_to_utc_offset(l_not_dst) == hours(5));
61 ptime u_not_dst(dst_start, time_duration(6,59,59));
62 check("check utc dst start boundary case",
63 us_eastern::utc_to_local_offset(u_not_dst) == hours(-5));
64 ptime l_in_dst(dst_start, hours(3)); //2002-Apr-07 03:00:00 1st sec of dst
65 check("check local dst start boundary case",
66 us_eastern::local_to_utc_offset(l_in_dst, boost::date_time::is_dst) == hours(4));
67 ptime u_in_dst(dst_start, hours(7));
68 check("check utc dst start boundary case",
69 us_eastern::utc_to_local_offset(u_in_dst) == hours(-4));
70
71
72 //Check the end of dst boundary
73 ptime dst_end(dst_end_day, time_duration(1,59,59)); //2002-Oct-27 01:00:00 DST
74 check("check local dst end boundary case - still dst",
75 us_eastern::local_to_utc_offset(dst_end, boost::date_time::is_dst) == hours(4));
76 check("check local dst end boundary case - still dst",
77 us_eastern::local_to_utc_offset(dst_end, boost::date_time::not_dst) == hours(5));
78 ptime u_dst_end1(dst_end_day, time_duration(5,59,59));
79 check("check utc dst end boundary case",
80 us_eastern::utc_to_local_offset(u_dst_end1) == hours(-4));
81 ptime u_dst_end2(dst_end_day, time_duration(6,0,0));
82 check("check utc dst end boundary case",
83 us_eastern::utc_to_local_offset(u_dst_end2) == hours(-5));
84 ptime u_dst_end3(dst_end_day, time_duration(6,59,59));
85 check("check utc dst end boundary case",
86 us_eastern::utc_to_local_offset(u_dst_end3) == hours(-5));
87 ptime u_dst_end4(dst_end_day, time_duration(7,0,0));
88 check("check utc dst end boundary case",
89 us_eastern::utc_to_local_offset(u_dst_end4) == hours(-5));
90
91
92 //Now try a local adjustments without dst
93 typedef boost::date_time::utc_adjustment<time_duration,-7> us_az_offset_adj;
94 typedef boost::date_time::null_dst_rules<date, time_duration> us_az_dst_adj;
95 //Type that embeds rules for UTC-7 with no dst
96 typedef boost::date_time::static_local_time_adjustor<ptime,
97 us_az_dst_adj,
98 us_az_offset_adj> us_az;
99
100 check("check local offset - no dst",
101 us_az::local_to_utc_offset(t10) == hours(7));
102 check("check local offset - no dst",
103 us_az::local_to_utc_offset(t11) == hours(7));
104 check("check local offset - no dst",
105 us_az::utc_to_local_offset(t10) == hours(-7));
106 check("check local offset - no dst",
107 us_az::utc_to_local_offset(t11) == hours(-7));
108
109
110 //Arizona timezone is utc-7 with no dst
111 typedef boost::date_time::local_adjustor<ptime, -7, no_dst> us_arizona;
112
113 ptime t7(date(2002,May,31), hours(17));
114 ptime t8 = us_arizona::local_to_utc(t7);
115 ptime t9 = us_arizona::utc_to_local(t8);
116 //converted to local then back ot utc
117 check("check us_local_adjustor", t9 == t7);
118
119 typedef boost::date_time::local_adjustor<ptime, -5, us_dst> us_eastern2;
120
121 {
122 ptime t7a(date(2002,May,31), hours(17));
123 ptime t7b = us_eastern2::local_to_utc(t7a);
124 ptime t7c = us_eastern2::utc_to_local(t7b);
125 //converted to local then back ot utc
126 check("check us_local_adjustor", t7c == t7a);
127 }
128
129 typedef boost::date_time::us_dst_trait<date> us_dst_traits;
130 typedef boost::date_time::dst_calc_engine<date, time_duration, us_dst_traits>
131 us_dst_calc2;
132
133 typedef boost::date_time::local_adjustor<ptime, -5, us_dst_calc2> us_eastern3;
134 {
135 ptime t7a(date(2002,May,31), hours(17));
136 ptime t7b = us_eastern3::local_to_utc(t7a);
137 ptime t7c = us_eastern3::utc_to_local(t7b);
138 //converted to local then back ot utc
139 check("check us_local_adjustor3", t7c == t7a);
140 }
141
142 {
143 ptime t7a(date(2007,Mar,11), hours(4));
144 ptime t7b = us_eastern3::local_to_utc(t7a);
145 ptime t7c = us_eastern3::utc_to_local(t7b);
146 //converted to local then back ot utc
147 check("check us_local_adjustor3 2007", t7c == t7a);
148 }
149
150 {
151 ptime t7a(date(2007,Mar,11), hours(3));
152 ptime t7b = us_eastern3::local_to_utc(t7a);
153 ptime t7c = us_eastern3::utc_to_local(t7b);
154 //converted to local then back ot utc
155 check("check us_local_adjustor3 2007 a", t7c == t7a);
156 }
157
158 //still experimental
159 typedef boost::date_time::dynamic_local_time_adjustor<ptime, us_dst> lta;
160 // lta adjustor(hours(-7));
161 check("dst start", lta::local_dst_start_day(2002) == dst_start);
162 check("dst end", lta::local_dst_end_day(2002) == dst_end_day);
163 check("dst boundary", lta::is_dst_boundary_day(dst_start));
164 check("dst boundary", lta::is_dst_boundary_day(dst_end_day));
165 // check("check non-dst offset", adjustor.utc_offset(false)==hours(-7));
166 // check("check dst offset", adjustor.utc_offset(true)==hours(-6));
167
168
169 check("dst start", lta::local_dst_start_day(2007) == date(2007,Mar,11));
170 check("dst end", lta::local_dst_end_day(2007) == date(2007,Nov,4));
171
172 return printTestStats();
173
174 }
175
176