Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:19

0001 #ifndef LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
0002 #define LOCAL_TIME_CUSTOM_TIME_ZONE_HPP__
0003 
0004 /* Copyright (c) 2003-2005 CrystalClear Software, Inc.
0005  * Subject to the Boost Software License, Version 1.0.
0006  * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0007  * Author: Jeff Garland, Bart Garst
0008  * $Date$
0009  */
0010 
0011 #include "boost/date_time/time_zone_base.hpp"
0012 #include "boost/date_time/time_zone_names.hpp"
0013 #include "boost/date_time/posix_time/posix_time.hpp"
0014 #include "boost/date_time/local_time/dst_transition_day_rules.hpp"
0015 #include "boost/date_time/string_convert.hpp"
0016 //#include "boost/date_time/special_defs.hpp"
0017 #include "boost/shared_ptr.hpp"
0018 
0019 namespace boost {
0020 namespace local_time {
0021 
0022   //typedef boost::date_time::time_zone_names time_zone_names;
0023   typedef boost::date_time::dst_adjustment_offsets<boost::posix_time::time_duration> dst_adjustment_offsets;
0024   //typedef boost::date_time::time_zone_base<boost::posix_time::ptime> time_zone;
0025   typedef boost::shared_ptr<dst_calc_rule> dst_calc_rule_ptr;
0026 
0027   //! A real time zone
0028   template<class CharT>
0029   class custom_time_zone_base : public date_time::time_zone_base<posix_time::ptime,CharT> {
0030   public:
0031     typedef boost::posix_time::time_duration time_duration_type;
0032     typedef date_time::time_zone_base<posix_time::ptime,CharT> base_type;
0033     typedef typename base_type::string_type string_type;
0034     typedef typename base_type::stringstream_type stringstream_type;
0035     typedef date_time::time_zone_names_base<CharT> time_zone_names;
0036     typedef CharT char_type;
0037 
0038     custom_time_zone_base(const time_zone_names& zone_names,
0039                      const time_duration_type& utc_offset,
0040                      const dst_adjustment_offsets& dst_shift,
0041                      boost::shared_ptr<dst_calc_rule> calc_rule) :
0042       zone_names_(zone_names),
0043       base_utc_offset_(utc_offset),
0044       dst_offsets_(dst_shift),
0045       dst_calc_rules_(calc_rule)
0046     {}
0047     virtual ~custom_time_zone_base() {}
0048     virtual string_type dst_zone_abbrev() const
0049     {
0050       return zone_names_.dst_zone_abbrev();
0051     }
0052     virtual string_type std_zone_abbrev() const
0053     {
0054       return zone_names_.std_zone_abbrev();
0055     }
0056     virtual string_type dst_zone_name() const
0057     {
0058       return zone_names_.dst_zone_name();
0059     }
0060     virtual string_type std_zone_name() const
0061     {
0062       return zone_names_.std_zone_name();
0063     }
0064     //! True if zone uses daylight savings adjustments
0065     virtual bool has_dst() const
0066     {
0067       return (bool) dst_calc_rules_; //if calc_rule is set the tz has dst
0068     }
0069     //! Local time that DST starts -- NADT if has_dst is false
0070     virtual posix_time::ptime dst_local_start_time(gregorian::greg_year y) const
0071     {
0072       gregorian::date d(gregorian::not_a_date_time);
0073       if (dst_calc_rules_) {
0074         d = dst_calc_rules_->start_day(y);
0075       }
0076       return posix_time::ptime(d, dst_offsets_.dst_start_offset_);
0077     }
0078     //! Local time that DST ends -- NADT if has_dst is false
0079     virtual posix_time::ptime dst_local_end_time(gregorian::greg_year y) const
0080     {
0081       gregorian::date d(gregorian::not_a_date_time);
0082       if (dst_calc_rules_) {
0083         d = dst_calc_rules_->end_day(y);
0084       }
0085       return posix_time::ptime(d, dst_offsets_.dst_end_offset_);
0086     }
0087     //! Base offset from UTC for zone (eg: -07:30:00)
0088     virtual time_duration_type base_utc_offset() const
0089     {
0090       return base_utc_offset_;
0091     }
0092     //! Adjustment forward or back made while DST is in effect
0093     virtual time_duration_type dst_offset() const
0094     {
0095       return dst_offsets_.dst_adjust_;
0096     }
0097     //! Returns a POSIX time_zone string for this object
0098     virtual string_type to_posix_string() const
0099     {
0100       // std offset dst [offset],start[/time],end[/time] - w/o spaces
0101       stringstream_type ss;
0102       ss.fill('0');
0103       boost::shared_ptr<dst_calc_rule> no_rules;
0104       // std
0105       ss << std_zone_abbrev();
0106       // offset
0107       if(base_utc_offset().is_negative()) {
0108         // inverting the sign guarantees we get two digits
0109         ss << '-' << std::setw(2) << base_utc_offset().invert_sign().hours();
0110       }
0111       else {
0112         ss << '+' << std::setw(2) << base_utc_offset().hours();
0113       }
0114       if(base_utc_offset().minutes() != 0 || base_utc_offset().seconds() != 0) {
0115         ss << ':' << std::setw(2) << base_utc_offset().minutes();
0116         if(base_utc_offset().seconds() != 0) {
0117           ss << ':' << std::setw(2) << base_utc_offset().seconds();
0118         }
0119       }
0120       if(dst_calc_rules_ != no_rules) {
0121         // dst
0122         ss << dst_zone_abbrev();
0123         // dst offset
0124         if(dst_offset().is_negative()) {
0125           // inverting the sign guarantees we get two digits
0126           ss << '-' << std::setw(2) << dst_offset().invert_sign().hours();
0127         }
0128         else {
0129           ss << '+' << std::setw(2) << dst_offset().hours();
0130         }
0131         if(dst_offset().minutes() != 0 || dst_offset().seconds() != 0) {
0132           ss << ':' << std::setw(2) << dst_offset().minutes();
0133           if(dst_offset().seconds() != 0) {
0134             ss << ':' << std::setw(2) << dst_offset().seconds();
0135           }
0136         }
0137         // start/time
0138         ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->start_rule_as_string()) << '/'
0139            << std::setw(2) << dst_offsets_.dst_start_offset_.hours() << ':'
0140            << std::setw(2) << dst_offsets_.dst_start_offset_.minutes();
0141         if(dst_offsets_.dst_start_offset_.seconds() != 0) {
0142           ss << ':' << std::setw(2) << dst_offsets_.dst_start_offset_.seconds();
0143         }
0144         // end/time
0145         ss << ',' << date_time::convert_string_type<char, char_type>(dst_calc_rules_->end_rule_as_string()) << '/'
0146            << std::setw(2) << dst_offsets_.dst_end_offset_.hours() << ':'
0147            << std::setw(2) << dst_offsets_.dst_end_offset_.minutes();
0148         if(dst_offsets_.dst_end_offset_.seconds() != 0) {
0149           ss << ':' << std::setw(2) << dst_offsets_.dst_end_offset_.seconds();
0150         }
0151       }
0152 
0153       return ss.str();
0154     }
0155   private:
0156     time_zone_names zone_names_;
0157     time_duration_type base_utc_offset_;
0158     dst_adjustment_offsets dst_offsets_;
0159     boost::shared_ptr<dst_calc_rule> dst_calc_rules_;
0160   };
0161 
0162   typedef custom_time_zone_base<char> custom_time_zone;
0163 
0164 } }//namespace
0165 
0166 
0167 
0168 #endif