123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #ifndef LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
- #define LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
- #include "boost/date_time/time_zone_base.hpp"
- #include "boost/date_time/time_zone_names.hpp"
- #include "boost/date_time/posix_time/posix_time.hpp"
- #include "boost/date_time/local_time/dst_transition_day_rules.hpp"
- #include "boost/date_time/string_convert.hpp"
- #include "boost/shared_ptr.hpp"
- namespace boost {
- namespace local_time {
-
- typedef boost::date_time::dst_adjustment_offsets<boost::posix_time::time_duration> dst_adjustment_offsets;
-
- typedef boost::shared_ptr<dst_calc_rule> dst_calc_rule_ptr;
-
- template<class CharT>
- class custom_time_zone_base : public date_time::time_zone_base<posix_time::ptime,CharT> {
- public:
- typedef boost::posix_time::time_duration time_duration_type;
- typedef date_time::time_zone_base<posix_time::ptime,CharT> base_type;
- typedef typename base_type::string_type string_type;
- typedef typename base_type::stringstream_type stringstream_type;
- typedef date_time::time_zone_names_base<CharT> time_zone_names;
- typedef CharT char_type;
-
- custom_time_zone_base(const time_zone_names& zone_names,
- const time_duration_type& utc_offset,
- const dst_adjustment_offsets& dst_shift,
- boost::shared_ptr<dst_calc_rule> calc_rule) :
- zone_names_(zone_names),
- base_utc_offset_(utc_offset),
- dst_offsets_(dst_shift),
- dst_calc_rules_(calc_rule)
- {};
- virtual ~custom_time_zone_base() {};
- virtual string_type dst_zone_abbrev() const
- {
- return zone_names_.dst_zone_abbrev();
- }
- virtual string_type std_zone_abbrev() const
- {
- return zone_names_.std_zone_abbrev();
- }
- virtual string_type dst_zone_name() const
- {
- return zone_names_.dst_zone_name();
- }
- virtual string_type std_zone_name() const
- {
- return zone_names_.std_zone_name();
- }
-
- virtual bool has_dst() const
- {
- return (dst_calc_rules_);
- }
-
- virtual posix_time::ptime dst_local_start_time(gregorian::greg_year y) const
- {
- gregorian::date d(gregorian::not_a_date_time);
- if (dst_calc_rules_) {
- d = dst_calc_rules_->start_day(y);
- }
- return posix_time::ptime(d, dst_offsets_.dst_start_offset_);
- }
-
- virtual posix_time::ptime dst_local_end_time(gregorian::greg_year y) const
- {
- gregorian::date d(gregorian::not_a_date_time);
- if (dst_calc_rules_) {
- d = dst_calc_rules_->end_day(y);
- }
- return posix_time::ptime(d, dst_offsets_.dst_end_offset_);
- }
-
- virtual time_duration_type base_utc_offset() const
- {
- return base_utc_offset_;
- }
-
- virtual time_duration_type dst_offset() const
- {
- return dst_offsets_.dst_adjust_;
- }
-
- virtual string_type to_posix_string() const
- {
-
- stringstream_type ss;
- ss.fill('0');
- boost::shared_ptr<dst_calc_rule> no_rules;
-
- ss << std_zone_abbrev();
-
- if(base_utc_offset().is_negative()) {
-
- ss << '-' << std::setw(2) << base_utc_offset().invert_sign().hours();
- }
- else {
- ss << '+' << std::setw(2) << base_utc_offset().hours();
- }
- if(base_utc_offset().minutes() != 0 || base_utc_offset().seconds() != 0) {
- ss << ':' << std::setw(2) << base_utc_offset().minutes();
- if(base_utc_offset().seconds() != 0) {
- ss << ':' << std::setw(2) << base_utc_offset().seconds();
- }
- }
- if(dst_calc_rules_ != no_rules) {
-
- ss << dst_zone_abbrev();
-
- if(dst_offset().is_negative()) {
-
- ss << '-' << std::setw(2) << dst_offset().invert_sign().hours();
- }
- else {
- ss << '+' << std::setw(2) << dst_offset().hours();
- }
- if(dst_offset().minutes() != 0 || dst_offset().seconds() != 0) {
- ss << ':' << std::setw(2) << dst_offset().minutes();
- if(dst_offset().seconds() != 0) {
- ss << ':' << std::setw(2) << dst_offset().seconds();
- }
- }
-
- ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->start_rule_as_string()) << '/'
- << std::setw(2) << dst_offsets_.dst_start_offset_.hours() << ':'
- << std::setw(2) << dst_offsets_.dst_start_offset_.minutes();
- if(dst_offsets_.dst_start_offset_.seconds() != 0) {
- ss << ':' << std::setw(2) << dst_offsets_.dst_start_offset_.seconds();
- }
-
- ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->end_rule_as_string()) << '/'
- << std::setw(2) << dst_offsets_.dst_end_offset_.hours() << ':'
- << std::setw(2) << dst_offsets_.dst_end_offset_.minutes();
- if(dst_offsets_.dst_end_offset_.seconds() != 0) {
- ss << ':' << std::setw(2) << dst_offsets_.dst_end_offset_.seconds();
- }
- }
- return ss.str();
- }
- private:
- time_zone_names zone_names_;
- bool has_dst_;
- time_duration_type base_utc_offset_;
- dst_adjustment_offsets dst_offsets_;
- boost::shared_ptr<dst_calc_rule> dst_calc_rules_;
- };
- typedef custom_time_zone_base<char> custom_time_zone;
- } }
- #endif
|