1 #ifndef LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
2 #define LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
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.txt or http://www.boost.org/LICENSE_1_0.txt)
7 * Author: Jeff Garland, Bart Garst
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/string_convert.hpp"
16 //#include "boost/date_time/special_defs.hpp"
17 #include "boost/shared_ptr.hpp"
20 namespace local_time {
22 //typedef boost::date_time::time_zone_names time_zone_names;
23 typedef boost::date_time::dst_adjustment_offsets<boost::posix_time::time_duration> dst_adjustment_offsets;
24 //typedef boost::date_time::time_zone_base<boost::posix_time::ptime> time_zone;
25 typedef boost::shared_ptr<dst_calc_rule> dst_calc_rule_ptr;
29 class custom_time_zone_base : public date_time::time_zone_base<posix_time::ptime,CharT> {
31 typedef boost::posix_time::time_duration time_duration_type;
32 typedef date_time::time_zone_base<posix_time::ptime,CharT> base_type;
33 typedef typename base_type::string_type string_type;
34 typedef typename base_type::stringstream_type stringstream_type;
35 typedef date_time::time_zone_names_base<CharT> time_zone_names;
36 typedef CharT char_type;
38 custom_time_zone_base(const time_zone_names& zone_names,
39 const time_duration_type& utc_offset,
40 const dst_adjustment_offsets& dst_shift,
41 boost::shared_ptr<dst_calc_rule> calc_rule) :
42 zone_names_(zone_names),
43 base_utc_offset_(utc_offset),
44 dst_offsets_(dst_shift),
45 dst_calc_rules_(calc_rule)
47 virtual ~custom_time_zone_base() {}
48 virtual string_type dst_zone_abbrev() const
50 return zone_names_.dst_zone_abbrev();
52 virtual string_type std_zone_abbrev() const
54 return zone_names_.std_zone_abbrev();
56 virtual string_type dst_zone_name() const
58 return zone_names_.dst_zone_name();
60 virtual string_type std_zone_name() const
62 return zone_names_.std_zone_name();
64 //! True if zone uses daylight savings adjustments
65 virtual bool has_dst() const
67 return (bool) dst_calc_rules_; //if calc_rule is set the tz has dst
69 //! Local time that DST starts -- NADT if has_dst is false
70 virtual posix_time::ptime dst_local_start_time(gregorian::greg_year y) const
72 gregorian::date d(gregorian::not_a_date_time);
73 if (dst_calc_rules_) {
74 d = dst_calc_rules_->start_day(y);
76 return posix_time::ptime(d, dst_offsets_.dst_start_offset_);
78 //! Local time that DST ends -- NADT if has_dst is false
79 virtual posix_time::ptime dst_local_end_time(gregorian::greg_year y) const
81 gregorian::date d(gregorian::not_a_date_time);
82 if (dst_calc_rules_) {
83 d = dst_calc_rules_->end_day(y);
85 return posix_time::ptime(d, dst_offsets_.dst_end_offset_);
87 //! Base offset from UTC for zone (eg: -07:30:00)
88 virtual time_duration_type base_utc_offset() const
90 return base_utc_offset_;
92 //! Adjustment forward or back made while DST is in effect
93 virtual time_duration_type dst_offset() const
95 return dst_offsets_.dst_adjust_;
97 //! Returns a POSIX time_zone string for this object
98 virtual string_type to_posix_string() const
100 // std offset dst [offset],start[/time],end[/time] - w/o spaces
101 stringstream_type ss;
103 boost::shared_ptr<dst_calc_rule> no_rules;
105 ss << std_zone_abbrev();
107 if(base_utc_offset().is_negative()) {
108 // inverting the sign guarantees we get two digits
109 ss << '-' << std::setw(2) << base_utc_offset().invert_sign().hours();
112 ss << '+' << std::setw(2) << base_utc_offset().hours();
114 if(base_utc_offset().minutes() != 0 || base_utc_offset().seconds() != 0) {
115 ss << ':' << std::setw(2) << base_utc_offset().minutes();
116 if(base_utc_offset().seconds() != 0) {
117 ss << ':' << std::setw(2) << base_utc_offset().seconds();
120 if(dst_calc_rules_ != no_rules) {
122 ss << dst_zone_abbrev();
124 if(dst_offset().is_negative()) {
125 // inverting the sign guarantees we get two digits
126 ss << '-' << std::setw(2) << dst_offset().invert_sign().hours();
129 ss << '+' << std::setw(2) << dst_offset().hours();
131 if(dst_offset().minutes() != 0 || dst_offset().seconds() != 0) {
132 ss << ':' << std::setw(2) << dst_offset().minutes();
133 if(dst_offset().seconds() != 0) {
134 ss << ':' << std::setw(2) << dst_offset().seconds();
138 ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->start_rule_as_string()) << '/'
139 << std::setw(2) << dst_offsets_.dst_start_offset_.hours() << ':'
140 << std::setw(2) << dst_offsets_.dst_start_offset_.minutes();
141 if(dst_offsets_.dst_start_offset_.seconds() != 0) {
142 ss << ':' << std::setw(2) << dst_offsets_.dst_start_offset_.seconds();
145 ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->end_rule_as_string()) << '/'
146 << std::setw(2) << dst_offsets_.dst_end_offset_.hours() << ':'
147 << std::setw(2) << dst_offsets_.dst_end_offset_.minutes();
148 if(dst_offsets_.dst_end_offset_.seconds() != 0) {
149 ss << ':' << std::setw(2) << dst_offsets_.dst_end_offset_.seconds();
156 time_zone_names zone_names_;
158 time_duration_type base_utc_offset_;
159 dst_adjustment_offsets dst_offsets_;
160 boost::shared_ptr<dst_calc_rule> dst_calc_rules_;
163 typedef custom_time_zone_base<char> custom_time_zone;