Chris@16: #ifndef DATE_TIME_TIME_HPP___ Chris@16: #define DATE_TIME_TIME_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: Chris@16: /*! @file time.hpp Chris@16: This file contains the interface for the time associated classes. Chris@16: */ Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! Representation of a precise moment in time, including the date. Chris@16: /*! Chris@16: This class is a skeleton for the interface of a temporal type Chris@16: with a resolution that is higher than a day. It is intended that Chris@16: this class be the base class and that the actual time Chris@16: class be derived using the BN pattern. In this way, the derived Chris@16: class can make decisions such as 'should there be a default constructor' Chris@16: and what should it set its value to, should there be optional constructors Chris@16: say allowing only an time_durations that generate a time from a clock,etc. Chris@16: So, in fact multiple time types can be created for a time_system with Chris@16: different construction policies, and all of them can perform basic Chris@16: operations by only writing a copy constructor. Finally, compiler Chris@16: errors are also shorter. Chris@16: Chris@16: The real behavior of the time class is provided by the time_system Chris@16: template parameter. This class must provide all the logic Chris@16: for addition, subtraction, as well as define all the interface Chris@16: types. Chris@16: Chris@16: */ Chris@16: Chris@16: template Chris@16: class base_time : private Chris@16: boost::less_than_comparable > Chris@16: { Chris@16: public: Chris@101: // A tag for type categorization. Can be used to detect Boost.DateTime time points in generic code. Chris@101: typedef void _is_boost_date_time_time_point; Chris@16: typedef T time_type; Chris@16: typedef typename time_system::time_rep_type time_rep_type; Chris@16: typedef typename time_system::date_type date_type; Chris@16: typedef typename time_system::date_duration_type date_duration_type; Chris@16: typedef typename time_system::time_duration_type time_duration_type; Chris@16: //typedef typename time_system::hms_type hms_type; Chris@16: Chris@16: base_time(const date_type& day, Chris@16: const time_duration_type& td, Chris@16: dst_flags dst=not_dst) : Chris@16: time_(time_system::get_time_rep(day, td, dst)) Chris@16: {} Chris@16: base_time(special_values sv) : Chris@16: time_(time_system::get_time_rep(sv)) Chris@16: {} Chris@16: base_time(const time_rep_type& rhs) : Chris@16: time_(rhs) Chris@16: {} Chris@16: date_type date() const Chris@16: { Chris@16: return time_system::get_date(time_); Chris@16: } Chris@16: time_duration_type time_of_day() const Chris@16: { Chris@16: return time_system::get_time_of_day(time_); Chris@16: } Chris@16: /*! Optional bool parameter will return time zone as an offset Chris@16: * (ie "+07:00"). Empty string is returned for classes that do Chris@16: * not use a time_zone */ Chris@16: std::string zone_name(bool /*as_offset*/=false) const Chris@16: { Chris@16: return time_system::zone_name(time_); Chris@16: } Chris@16: /*! Optional bool parameter will return time zone as an offset Chris@16: * (ie "+07:00"). Empty string is returned for classes that do Chris@16: * not use a time_zone */ Chris@16: std::string zone_abbrev(bool /*as_offset*/=false) const Chris@16: { Chris@16: return time_system::zone_name(time_); Chris@16: } Chris@16: //! An empty string is returned for classes that do not use a time_zone Chris@16: std::string zone_as_posix_string() const Chris@16: { Chris@16: return std::string(); Chris@16: } Chris@16: Chris@16: //! check to see if date is not a value Chris@16: bool is_not_a_date_time() const Chris@16: { Chris@16: return time_.is_not_a_date_time(); Chris@16: } Chris@16: //! check to see if date is one of the infinity values Chris@16: bool is_infinity() const Chris@16: { Chris@16: return (is_pos_infinity() || is_neg_infinity()); Chris@16: } Chris@16: //! check to see if date is greater than all possible dates Chris@16: bool is_pos_infinity() const Chris@16: { Chris@16: return time_.is_pos_infinity(); Chris@16: } Chris@16: //! check to see if date is greater than all possible dates Chris@16: bool is_neg_infinity() const Chris@16: { Chris@16: return time_.is_neg_infinity(); Chris@16: } Chris@16: //! check to see if time is a special value Chris@16: bool is_special() const Chris@16: { Chris@16: return(is_not_a_date_time() || is_infinity()); Chris@16: } Chris@16: //!Equality operator -- others generated by boost::equality_comparable Chris@16: bool operator==(const time_type& rhs) const Chris@16: { Chris@16: return time_system::is_equal(time_,rhs.time_); Chris@16: } Chris@16: //!Equality operator -- others generated by boost::less_than_comparable Chris@16: bool operator<(const time_type& rhs) const Chris@16: { Chris@16: return time_system::is_less(time_,rhs.time_); Chris@16: } Chris@16: //! difference between two times Chris@16: time_duration_type operator-(const time_type& rhs) const Chris@16: { Chris@16: return time_system::subtract_times(time_, rhs.time_); Chris@16: } Chris@16: //! add date durations Chris@16: time_type operator+(const date_duration_type& dd) const Chris@16: { Chris@16: return time_system::add_days(time_, dd); Chris@16: } Chris@16: time_type operator+=(const date_duration_type& dd) Chris@16: { Chris@16: time_ = (time_system::get_time_rep(date() + dd, time_of_day())); Chris@16: return time_type(time_); Chris@16: } Chris@16: //! subtract date durations Chris@16: time_type operator-(const date_duration_type& dd) const Chris@16: { Chris@16: return time_system::subtract_days(time_, dd); Chris@16: } Chris@16: time_type operator-=(const date_duration_type& dd) Chris@16: { Chris@16: time_ = (time_system::get_time_rep(date() - dd, time_of_day())); Chris@16: return time_type(time_); Chris@16: } Chris@16: //! add time durations Chris@16: time_type operator+(const time_duration_type& td) const Chris@16: { Chris@16: return time_type(time_system::add_time_duration(time_, td)); Chris@16: } Chris@16: time_type operator+=(const time_duration_type& td) Chris@16: { Chris@16: time_ = (time_system::get_time_rep(date(), time_of_day() + td)); Chris@16: return time_type(time_); Chris@16: } Chris@16: //! subtract time durations Chris@16: time_type operator-(const time_duration_type& rhs) const Chris@16: { Chris@16: return time_system::subtract_time_duration(time_, rhs); Chris@16: } Chris@16: time_type operator-=(const time_duration_type& td) Chris@16: { Chris@16: time_ = (time_system::get_time_rep(date(), time_of_day() - td)); Chris@16: return time_type(time_); Chris@16: } Chris@16: Chris@16: protected: Chris@16: time_rep_type time_; Chris@16: }; Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: } } //namespace date_time::boost Chris@16: Chris@16: Chris@16: #endif Chris@16: