1 | #ifndef LOCAL_TIME_TIME_ZONE_HPP__
|
---|
2 | #define LOCAL_TIME_TIME_ZONE_HPP__
|
---|
3 |
|
---|
4 | /* Copyright (c) 2003-2005 CrystalClear Software, Inc.
|
---|
5 | * Subject to the Boost Software License, Version 1.0.
|
---|
6 | * (See accompanying file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
---|
7 | * Author: Jeff Garland, Bart Garst
|
---|
8 | * $Date: 2005/04/17 21:48:19 $
|
---|
9 | */
|
---|
10 |
|
---|
11 | #include "boost/date_time/time_zone_base.hpp"
|
---|
12 | #include "boost/date_time/time_zone_names.hpp"
|
---|
13 | #include "boost/date_time/posix_time/posix_time.hpp"
|
---|
14 | #include "boost/date_time/local_time/dst_transition_day_rules.hpp"
|
---|
15 | //#include "boost/date_time/special_defs.hpp"
|
---|
16 | #include "boost/shared_ptr.hpp"
|
---|
17 |
|
---|
18 | namespace boost {
|
---|
19 | namespace local_time {
|
---|
20 |
|
---|
21 | typedef boost::date_time::time_zone_names time_zone_names;
|
---|
22 | typedef boost::date_time::dst_adjustment_offsets<boost::posix_time::time_duration> dst_adjustment_offsets;
|
---|
23 | typedef boost::date_time::time_zone_base<boost::posix_time::ptime> time_zone_base;
|
---|
24 | typedef boost::shared_ptr<dst_calc_rule> dst_calc_rule_ptr;
|
---|
25 |
|
---|
26 | //! A real time zone
|
---|
27 | class time_zone : public time_zone_base {
|
---|
28 | public:
|
---|
29 | typedef boost::posix_time::time_duration time_duration_type;
|
---|
30 | typedef time_zone_base base_type;
|
---|
31 | typedef base_type::string_type string_type;
|
---|
32 | typedef base_type::stringstream_type stringstream_type;
|
---|
33 | time_zone(const time_zone_names& zone_names,
|
---|
34 | const time_duration_type& base_utc_offset,
|
---|
35 | const dst_adjustment_offsets& dst_offset,
|
---|
36 | boost::shared_ptr<dst_calc_rule> calc_rule) :
|
---|
37 | zone_names_(zone_names),
|
---|
38 | base_utc_offset_(base_utc_offset),
|
---|
39 | dst_offsets_(dst_offset),
|
---|
40 | dst_calc_rules_(calc_rule)
|
---|
41 | {};
|
---|
42 | virtual ~time_zone() {};
|
---|
43 | virtual std::string dst_zone_abbrev() const
|
---|
44 | {
|
---|
45 | return zone_names_.dst_zone_abbrev();
|
---|
46 | }
|
---|
47 | virtual std::string std_zone_abbrev() const
|
---|
48 | {
|
---|
49 | return zone_names_.std_zone_abbrev();
|
---|
50 | }
|
---|
51 | virtual std::string dst_zone_name() const
|
---|
52 | {
|
---|
53 | return zone_names_.dst_zone_name();
|
---|
54 | }
|
---|
55 | virtual std::string std_zone_name() const
|
---|
56 | {
|
---|
57 | return zone_names_.std_zone_name();
|
---|
58 | }
|
---|
59 | //! True if zone uses daylight savings adjustments
|
---|
60 | virtual bool has_dst() const
|
---|
61 | {
|
---|
62 | return (dst_calc_rules_); //if calc_rule is set the tz has dst
|
---|
63 | }
|
---|
64 | //! Local time that DST starts -- NADT if has_dst is false
|
---|
65 | virtual posix_time::ptime dst_local_start_time(gregorian::greg_year y) const
|
---|
66 | {
|
---|
67 | gregorian::date d(gregorian::not_a_date_time);
|
---|
68 | if (dst_calc_rules_) {
|
---|
69 | d = dst_calc_rules_->start_day(y);
|
---|
70 | }
|
---|
71 | return posix_time::ptime(d, dst_offsets_.dst_start_offset_);
|
---|
72 | }
|
---|
73 | //! Local time that DST ends -- NADT if has_dst is false
|
---|
74 | virtual posix_time::ptime dst_local_end_time(gregorian::greg_year y) const
|
---|
75 | {
|
---|
76 | gregorian::date d(gregorian::not_a_date_time);
|
---|
77 | if (dst_calc_rules_) {
|
---|
78 | d = dst_calc_rules_->end_day(y);
|
---|
79 | }
|
---|
80 | return posix_time::ptime(d, dst_offsets_.dst_end_offset_);
|
---|
81 | }
|
---|
82 | //! Base offset from UTC for zone (eg: -07:30:00)
|
---|
83 | virtual time_duration_type base_utc_offset() const
|
---|
84 | {
|
---|
85 | return base_utc_offset_;
|
---|
86 | }
|
---|
87 | //! Adjustment forward or back made while DST is in effect
|
---|
88 | virtual time_duration_type dst_offset() const
|
---|
89 | {
|
---|
90 | return dst_offsets_.dst_adjust_;
|
---|
91 | }
|
---|
92 | //! Returns a POSIX time_zone string for this object
|
---|
93 | virtual string_type to_posix_string() const
|
---|
94 | {
|
---|
95 | // std offset dst [offset],start[/time],end[/time] - w/o spaces
|
---|
96 | stringstream_type ss;
|
---|
97 | ss.fill('0');
|
---|
98 | boost::shared_ptr<dst_calc_rule> no_rules;
|
---|
99 | // std
|
---|
100 | ss << std_zone_abbrev();
|
---|
101 | // offset
|
---|
102 | if(base_utc_offset().is_negative()) {
|
---|
103 | // inverting the sign guarantees we get two digits
|
---|
104 | ss << '-' << std::setw(2) << base_utc_offset().invert_sign().hours();
|
---|
105 | }
|
---|
106 | else {
|
---|
107 | ss << '+' << std::setw(2) << base_utc_offset().hours();
|
---|
108 | }
|
---|
109 | if(base_utc_offset().minutes() != 0 || base_utc_offset().seconds() != 0) {
|
---|
110 | ss << ':' << std::setw(2) << base_utc_offset().minutes();
|
---|
111 | if(base_utc_offset().seconds() != 0) {
|
---|
112 | ss << ':' << std::setw(2) << base_utc_offset().seconds();
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if(dst_calc_rules_ != no_rules) {
|
---|
116 | // dst
|
---|
117 | ss << dst_zone_abbrev();
|
---|
118 | // dst offset
|
---|
119 | if(dst_offset().is_negative()) {
|
---|
120 | // inverting the sign guarantees we get two digits
|
---|
121 | ss << '-' << std::setw(2) << dst_offset().invert_sign().hours();
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | ss << '+' << std::setw(2) << dst_offset().hours();
|
---|
125 | }
|
---|
126 | if(dst_offset().minutes() != 0 || dst_offset().seconds() != 0) {
|
---|
127 | ss << ':' << std::setw(2) << dst_offset().minutes();
|
---|
128 | if(dst_offset().seconds() != 0) {
|
---|
129 | ss << ':' << std::setw(2) << dst_offset().seconds();
|
---|
130 | }
|
---|
131 | }
|
---|
132 | // start/time
|
---|
133 | ss << ',' << dst_calc_rules_->start_rule_as_string() << '/'
|
---|
134 | << std::setw(2) << dst_offsets_.dst_start_offset_.hours() << ':'
|
---|
135 | << std::setw(2) << dst_offsets_.dst_start_offset_.minutes();
|
---|
136 | if(dst_offsets_.dst_start_offset_.seconds() != 0) {
|
---|
137 | ss << ':' << std::setw(2) << dst_offsets_.dst_start_offset_.seconds();
|
---|
138 | }
|
---|
139 | // end/time
|
---|
140 | ss << ',' << dst_calc_rules_->end_rule_as_string() << '/'
|
---|
141 | << std::setw(2) << dst_offsets_.dst_end_offset_.hours() << ':'
|
---|
142 | << std::setw(2) << dst_offsets_.dst_end_offset_.minutes();
|
---|
143 | if(dst_offsets_.dst_end_offset_.seconds() != 0) {
|
---|
144 | ss << ':' << std::setw(2) << dst_offsets_.dst_end_offset_.seconds();
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | return ss.str();
|
---|
149 | }
|
---|
150 | private:
|
---|
151 | time_zone_names zone_names_;
|
---|
152 | bool has_dst_;
|
---|
153 | time_duration_type base_utc_offset_;
|
---|
154 | dst_adjustment_offsets dst_offsets_;
|
---|
155 | boost::shared_ptr<dst_calc_rule> dst_calc_rules_;
|
---|
156 | };
|
---|
157 |
|
---|
158 | } }//namespace
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | #endif
|
---|