123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- #ifndef LOCAL_TIME_LOCAL_DATE_TIME_HPP__
- #define LOCAL_TIME_LOCAL_DATE_TIME_HPP__
- #include <string>
- #include <iomanip>
- #include <sstream>
- #include <stdexcept>
- #include <boost/shared_ptr.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/date_time/time.hpp>
- #include <boost/date_time/posix_time/posix_time.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/dst_rules.hpp>
- #include <boost/date_time/time_zone_base.hpp>
- #include <boost/date_time/special_defs.hpp>
- #include <boost/date_time/time_resolution_traits.hpp>
- namespace boost {
- namespace local_time {
-
- struct BOOST_SYMBOL_VISIBLE ambiguous_result : public std::logic_error
- {
- ambiguous_result (std::string const& msg = std::string()) :
- std::logic_error(std::string("Daylight Savings Results are ambiguous: " + msg)) {}
- };
-
- struct BOOST_SYMBOL_VISIBLE time_label_invalid : public std::logic_error
- {
- time_label_invalid (std::string const& msg = std::string()) :
- std::logic_error(std::string("Time label given is invalid: " + msg)) {}
- };
- struct BOOST_SYMBOL_VISIBLE dst_not_valid: public std::logic_error
- {
- dst_not_valid(std::string const& msg = std::string()) :
- std::logic_error(std::string("is_dst flag does not match resulting dst for time label given: " + msg)) {}
- };
-
-
- using date_time::time_is_dst_result;
- using date_time::is_in_dst;
- using date_time::is_not_in_dst;
- using date_time::ambiguous;
- using date_time::invalid_time_label;
-
-
- template<class utc_time_=posix_time::ptime,
- class tz_type=date_time::time_zone_base<utc_time_,char> >
- class BOOST_SYMBOL_VISIBLE local_date_time_base : public date_time::base_time<utc_time_,
- boost::posix_time::posix_time_system> {
- public:
- typedef utc_time_ utc_time_type;
- typedef typename utc_time_type::time_duration_type time_duration_type;
- typedef typename utc_time_type::date_type date_type;
- typedef typename date_type::duration_type date_duration_type;
- typedef typename utc_time_type::time_system_type time_system_type;
-
- local_date_time_base(utc_time_type t,
- boost::shared_ptr<tz_type> tz) :
- date_time::base_time<utc_time_type, time_system_type>(t),
- zone_(tz)
- {
-
- }
-
- local_date_time_base(date_type d,
- time_duration_type td,
- boost::shared_ptr<tz_type> tz,
- bool dst_flag) :
- date_time::base_time<utc_time_type,time_system_type>(construction_adjustment(utc_time_type(d, td), tz, dst_flag)),
- zone_(tz)
- {
- if(tz != boost::shared_ptr<tz_type>() && tz->has_dst()){
-
- time_is_dst_result result = check_dst(d, td, tz);
- bool in_dst = (result == is_in_dst);
-
- if(result == invalid_time_label){
-
- std::ostringstream ss;
- ss << "time given: " << d << ' ' << td;
- boost::throw_exception(time_label_invalid(ss.str()));
- }
- else if(result != ambiguous && in_dst != dst_flag){
-
-
- std::ostringstream ss;
- ss.setf(std::ios_base::boolalpha);
- ss << "flag given: dst=" << dst_flag << ", dst calculated: dst=" << in_dst;
- boost::throw_exception(dst_not_valid(ss.str()));
- }
-
- }
- }
-
- enum DST_CALC_OPTIONS { EXCEPTION_ON_ERROR, NOT_DATE_TIME_ON_ERROR };
-
-
- local_date_time_base(date_type d,
- time_duration_type td,
- boost::shared_ptr<tz_type> tz,
- DST_CALC_OPTIONS calc_option) :
-
- date_time::base_time<utc_time_type,time_system_type>(utc_time_type(d,td)),
- zone_(tz)
- {
- time_is_dst_result result = check_dst(d, td, tz);
- if(result == ambiguous) {
- if(calc_option == EXCEPTION_ON_ERROR){
- std::ostringstream ss;
- ss << "time given: " << d << ' ' << td;
- boost::throw_exception(ambiguous_result(ss.str()));
- }
- else{
- this->time_ = posix_time::posix_time_system::get_time_rep(date_type(date_time::not_a_date_time), time_duration_type(date_time::not_a_date_time));
- }
- }
- else if(result == invalid_time_label){
- if(calc_option == EXCEPTION_ON_ERROR){
- std::ostringstream ss;
- ss << "time given: " << d << ' ' << td;
- boost::throw_exception(time_label_invalid(ss.str()));
- }
- else{
- this->time_ = posix_time::posix_time_system::get_time_rep(date_type(date_time::not_a_date_time), time_duration_type(date_time::not_a_date_time));
- }
- }
- else if(result == is_in_dst){
- utc_time_type t =
- construction_adjustment(utc_time_type(d, td), tz, true);
- this->time_ = posix_time::posix_time_system::get_time_rep(t.date(),
- t.time_of_day());
- }
- else{
- utc_time_type t =
- construction_adjustment(utc_time_type(d, td), tz, false);
- this->time_ = posix_time::posix_time_system::get_time_rep(t.date(),
- t.time_of_day());
- }
- }
-
-
- static time_is_dst_result check_dst(date_type d,
- time_duration_type td,
- boost::shared_ptr<tz_type> tz)
- {
- if(tz != boost::shared_ptr<tz_type>() && tz->has_dst()) {
- typedef typename date_time::dst_calculator<date_type, time_duration_type> dst_calculator;
- return dst_calculator::local_is_dst(
- d, td,
- tz->dst_local_start_time(d.year()).date(),
- tz->dst_local_start_time(d.year()).time_of_day(),
- tz->dst_local_end_time(d.year()).date(),
- tz->dst_local_end_time(d.year()).time_of_day(),
- tz->dst_offset()
- );
- }
- else{
- return is_not_in_dst;
- }
- }
-
- ~local_date_time_base() {}
-
- local_date_time_base(const local_date_time_base& rhs) :
- date_time::base_time<utc_time_type, time_system_type>(rhs),
- zone_(rhs.zone_)
- {}
-
- explicit local_date_time_base(const boost::date_time::special_values sv,
- boost::shared_ptr<tz_type> tz = boost::shared_ptr<tz_type>()) :
- date_time::base_time<utc_time_type, time_system_type>(utc_time_type(sv)),
- zone_(tz)
- {}
-
- boost::shared_ptr<tz_type> zone() const
- {
- return zone_;
- }
-
- bool is_dst() const
- {
- if(zone_ != boost::shared_ptr<tz_type>() && zone_->has_dst() && !this->is_special()) {
-
- utc_time_type lt(this->time_);
- lt += zone_->base_utc_offset();
-
-
- switch(check_dst(lt.date(), lt.time_of_day(), zone_)){
- case is_not_in_dst:
- return false;
- case is_in_dst:
- return true;
- case ambiguous:
- if(lt + zone_->dst_offset() < zone_->dst_local_end_time(lt.date().year())) {
- return true;
- }
- break;
- case invalid_time_label:
- if(lt >= zone_->dst_local_start_time(lt.date().year())) {
- return true;
- }
- break;
- }
- }
- return false;
- }
-
- utc_time_type utc_time() const
- {
- return utc_time_type(this->time_);
- }
-
- utc_time_type local_time() const
- {
- if(zone_ != boost::shared_ptr<tz_type>()){
- utc_time_type lt = this->utc_time() + zone_->base_utc_offset();
- if (is_dst()) {
- lt += zone_->dst_offset();
- }
- return lt;
- }
- return utc_time_type(this->time_);
- }
-
-
- std::string to_string() const
- {
-
- std::ostringstream ss;
- if(this->is_special()){
- ss << utc_time();
- return ss.str();
- }
- if(zone_ == boost::shared_ptr<tz_type>()) {
- ss << utc_time() << " UTC";
- return ss.str();
- }
- bool is_dst_ = is_dst();
- utc_time_type lt = this->utc_time() + zone_->base_utc_offset();
- if (is_dst_) {
- lt += zone_->dst_offset();
- }
- ss << local_time() << " ";
- if (is_dst()) {
- ss << zone_->dst_zone_abbrev();
- }
- else {
- ss << zone_->std_zone_abbrev();
- }
- return ss.str();
- }
-
- local_date_time_base local_time_in(boost::shared_ptr<tz_type> new_tz,
- time_duration_type td=time_duration_type(0,0,0)) const
- {
- return local_date_time_base(utc_time_type(this->time_) + td, new_tz);
- }
-
-
- std::string zone_name(bool as_offset=false) const
- {
- if(zone_ == boost::shared_ptr<tz_type>()) {
- if(as_offset) {
- return std::string("Z");
- }
- else {
- return std::string("Coordinated Universal Time");
- }
- }
- if (is_dst()) {
- if(as_offset) {
- time_duration_type td = zone_->base_utc_offset();
- td += zone_->dst_offset();
- return zone_as_offset(td, ":");
- }
- else {
- return zone_->dst_zone_name();
- }
- }
- else {
- if(as_offset) {
- time_duration_type td = zone_->base_utc_offset();
- return zone_as_offset(td, ":");
- }
- else {
- return zone_->std_zone_name();
- }
- }
- }
-
-
- std::string zone_abbrev(bool as_offset=false) const
- {
- if(zone_ == boost::shared_ptr<tz_type>()) {
- if(as_offset) {
- return std::string("Z");
- }
- else {
- return std::string("UTC");
- }
- }
- if (is_dst()) {
- if(as_offset) {
- time_duration_type td = zone_->base_utc_offset();
- td += zone_->dst_offset();
- return zone_as_offset(td, "");
- }
- else {
- return zone_->dst_zone_abbrev();
- }
- }
- else {
- if(as_offset) {
- time_duration_type td = zone_->base_utc_offset();
- return zone_as_offset(td, "");
- }
- else {
- return zone_->std_zone_abbrev();
- }
- }
- }
-
- std::string zone_as_posix_string() const
- {
- if(zone_ == shared_ptr<tz_type>()) {
- return std::string("UTC+00");
- }
- return zone_->to_posix_string();
- }
-
-
-
- bool operator==(const local_date_time_base& rhs) const
- {
- return time_system_type::is_equal(this->time_, rhs.time_);
- }
-
- bool operator!=(const local_date_time_base& rhs) const
- {
- return !(*this == rhs);
- }
-
- bool operator<(const local_date_time_base& rhs) const
- {
- return time_system_type::is_less(this->time_, rhs.time_);
- }
-
- bool operator<=(const local_date_time_base& rhs) const
- {
- return (*this < rhs || *this == rhs);
- }
-
- bool operator>(const local_date_time_base& rhs) const
- {
- return !(*this <= rhs);
- }
-
- bool operator>=(const local_date_time_base& rhs) const
- {
- return (*this > rhs || *this == rhs);
- }
-
- local_date_time_base operator+(const date_duration_type& dd) const
- {
- return local_date_time_base(time_system_type::add_days(this->time_,dd), zone_);
- }
-
- local_date_time_base operator+=(const date_duration_type& dd)
- {
- this->time_ = time_system_type::add_days(this->time_,dd);
- return *this;
- }
-
- local_date_time_base operator-(const date_duration_type& dd) const
- {
- return local_date_time_base(time_system_type::subtract_days(this->time_,dd), zone_);
- }
-
- local_date_time_base operator-=(const date_duration_type& dd)
- {
- this->time_ = time_system_type::subtract_days(this->time_,dd);
- return *this;
- }
-
- local_date_time_base operator+(const time_duration_type& td) const
- {
- return local_date_time_base(time_system_type::add_time_duration(this->time_,td), zone_);
- }
-
- local_date_time_base operator+=(const time_duration_type& td)
- {
- this->time_ = time_system_type::add_time_duration(this->time_,td);
- return *this;
- }
-
- local_date_time_base operator-(const time_duration_type& td) const
- {
- return local_date_time_base(time_system_type::subtract_time_duration(this->time_,td), zone_);
- }
-
- local_date_time_base operator-=(const time_duration_type& td)
- {
- this->time_ = time_system_type::subtract_time_duration(this->time_,td);
- return *this;
- }
-
- time_duration_type operator-(const local_date_time_base& rhs) const
- {
- return utc_time_type(this->time_) - utc_time_type(rhs.time_);
- }
- private:
- boost::shared_ptr<tz_type> zone_;
-
-
- utc_time_type construction_adjustment(utc_time_type t,
- boost::shared_ptr<tz_type> z,
- bool dst_flag)
- {
- if(z != boost::shared_ptr<tz_type>()) {
- if(dst_flag && z->has_dst()) {
- t -= z->dst_offset();
- }
- t -= z->base_utc_offset();
- }
- return t;
- }
-
- std::string zone_as_offset(const time_duration_type& td,
- const std::string& separator) const
- {
- std::ostringstream ss;
- if(td.is_negative()) {
-
-
-
- ss << "-";
- }
- else {
- ss << "+";
- }
- ss << std::setw(2) << std::setfill('0')
- << date_time::absolute_value(td.hours())
- << separator
- << std::setw(2) << std::setfill('0')
- << date_time::absolute_value(td.minutes());
- return ss.str();
- }
- };
-
- typedef local_date_time_base<> local_date_time;
- } }
- #endif
|