local_date_time.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 526 行 · 第 1/2 页
HPP
526 行
{ 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_); } //! Returns string in the form "2003-Aug-20 05:00:00 EDT" /*! Returns string in the form "2003-Aug-20 05:00:00 EDT". If * time_zone is NULL the time zone abbreviation will be "UTC". The time * zone abbrev will not be included if calling object is a special_value*/ std::string to_string() const { //TODO is this a temporary function ??? std::stringstream 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(); } /*! returns a local_date_time_base in the given time zone with the * optional time_duration added. */ 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); } //! Returns name of associated time zone or "Coordinated Universal Time". /*! Optional bool parameter will return time zone as an offset * (ie "+07:00" extended iso format). Empty string is returned for * classes that do not use a time_zone */ 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(); } } } //! Returns abbreviation of associated time zone or "UTC". /*! Optional bool parameter will return time zone as an offset * (ie "+0700" iso format). Empty string is returned for classes * that do not use a time_zone */ 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(); } } } //! returns a posix_time_zone string for the associated time_zone. If no time_zone, "UTC+00" is returned. std::string zone_as_posix_string() const { if(zone_ == shared_ptr<tz_type>()) { return std::string("UTC+00"); } return zone_->to_posix_string(); } //! Equality comparison operator /*bool operator==(const date_time::base_time<boost::posix_time::ptime,boost::posix_time::posix_time_system>& rhs) const { // fails due to rhs.time_ being protected return date_time::base_time<boost::posix_time::ptime,boost::posix_time::posix_time_system>::operator==(rhs); //return this->time_ == rhs.time_; }*/ //! Equality comparison operator bool operator==(const local_date_time_base& rhs) const { return time_system_type::is_equal(this->time_, rhs.time_); } //! Non-Equality comparison operator bool operator!=(const local_date_time_base& rhs) const { return !(*this == rhs); } //! Less than comparison operator bool operator<(const local_date_time_base& rhs) const { return time_system_type::is_less(this->time_, rhs.time_); } //! Less than or equal to comparison operator bool operator<=(const local_date_time_base& rhs) const { return (*this < rhs || *this == rhs); } //! Greater than comparison operator bool operator>(const local_date_time_base& rhs) const { return !(*this <= rhs); } //! Greater than or equal to comparison operator bool operator>=(const local_date_time_base& rhs) const { return (*this > rhs || *this == rhs); } //! Local_date_time + date_duration 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 += date_duration 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 - date_duration 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 -= date_duration 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 + time_duration 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 += time_duration 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 - time_duration 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 -= time_duration local_date_time_base operator-=(const time_duration_type& td) { this->time_ = time_system_type::subtract_time_duration(this->time_,td); return *this; } //! local_date_time -= local_date_time --> time_duration_type 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_; //bool is_dst_; /*! Adjust the passed in time to UTC? */ 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(); } // else no adjust t -= z->base_utc_offset(); } return t; } /*! Simple formatting code -- todo remove this? */ std::string zone_as_offset(const time_duration_type& td, const std::string& separator) const { std::stringstream ss; if(td.is_negative()) { // a negative duration is represented as "-[h]h:mm" // we require two digits for the hour. A positive duration // with the %H flag will always give two digits 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(); } }; //!Use the default parameters to define local_date_time typedef local_date_time_base<> local_date_time;} }#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?