1 #ifndef DATE_TIME_TIME_HPP___ 2 #define DATE_TIME_TIME_HPP___ 3 4 /* Copyright (c) 2002,2003,2005,2020 CrystalClear Software, Inc. 5 * Use, modification and distribution is subject to the 6 * Boost Software License, Version 1.0. (See accompanying 7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 8 * Author: Jeff Garland, Bart Garst 9 * $Date$ 10 */ 11 12 13 /*! @file time.hpp 14 This file contains the interface for the time associated classes. 15 */ 16 #include <string> 17 #include <boost/operators.hpp> 18 #include <boost/date_time/time_defs.hpp> 19 #include <boost/date_time/special_defs.hpp> 20 21 namespace boost { 22 namespace date_time { 23 24 //! Representation of a precise moment in time, including the date. 25 /*! 26 This class is a skeleton for the interface of a temporal type 27 with a resolution that is higher than a day. It is intended that 28 this class be the base class and that the actual time 29 class be derived using the BN pattern. In this way, the derived 30 class can make decisions such as 'should there be a default constructor' 31 and what should it set its value to, should there be optional constructors 32 say allowing only an time_durations that generate a time from a clock,etc. 33 So, in fact multiple time types can be created for a time_system with 34 different construction policies, and all of them can perform basic 35 operations by only writing a copy constructor. Finally, compiler 36 errors are also shorter. 37 38 The real behavior of the time class is provided by the time_system 39 template parameter. This class must provide all the logic 40 for addition, subtraction, as well as define all the interface 41 types. 42 43 */ 44 45 template <class T, class time_system> 46 class base_time : private 47 boost::less_than_comparable<T 48 , boost::equality_comparable<T 49 > > 50 { 51 public: 52 // A tag for type categorization. Can be used to detect Boost.DateTime time points in generic code. 53 typedef void _is_boost_date_time_time_point; 54 typedef T time_type; 55 typedef typename time_system::time_rep_type time_rep_type; 56 typedef typename time_system::date_type date_type; 57 typedef typename time_system::date_duration_type date_duration_type; 58 typedef typename time_system::time_duration_type time_duration_type; 59 //typedef typename time_system::hms_type hms_type; 60 61 BOOST_CXX14_CONSTEXPR base_time(const date_type & day,const time_duration_type & td,dst_flags dst=not_dst)62 base_time(const date_type& day, 63 const time_duration_type& td, 64 dst_flags dst=not_dst) : 65 time_(time_system::get_time_rep(day, td, dst)) 66 {} 67 BOOST_CXX14_CONSTEXPR base_time(special_values sv)68 base_time(special_values sv) : 69 time_(time_system::get_time_rep(sv)) 70 {} 71 BOOST_CXX14_CONSTEXPR base_time(const time_rep_type & rhs)72 base_time(const time_rep_type& rhs) : 73 time_(rhs) 74 {} 75 BOOST_CXX14_CONSTEXPR date() const76 date_type date() const 77 { 78 return time_system::get_date(time_); 79 } 80 BOOST_CXX14_CONSTEXPR time_of_day() const81 time_duration_type time_of_day() const 82 { 83 return time_system::get_time_of_day(time_); 84 } 85 /*! Optional bool parameter will return time zone as an offset 86 * (ie "+07:00"). Empty string is returned for classes that do 87 * not use a time_zone */ zone_name(bool=false) const88 std::string zone_name(bool /*as_offset*/=false) const 89 { 90 return time_system::zone_name(time_); 91 } 92 /*! Optional bool parameter will return time zone as an offset 93 * (ie "+07:00"). Empty string is returned for classes that do 94 * not use a time_zone */ zone_abbrev(bool=false) const95 std::string zone_abbrev(bool /*as_offset*/=false) const 96 { 97 return time_system::zone_name(time_); 98 } 99 //! An empty string is returned for classes that do not use a time_zone zone_as_posix_string() const100 std::string zone_as_posix_string() const 101 { 102 return std::string(); 103 } 104 105 //! check to see if date is not a value 106 BOOST_CXX14_CONSTEXPR is_not_a_date_time() const107 bool is_not_a_date_time() const 108 { 109 return time_.is_not_a_date_time(); 110 } 111 //! check to see if date is one of the infinity values 112 BOOST_CXX14_CONSTEXPR is_infinity() const113 bool is_infinity() const 114 { 115 return (is_pos_infinity() || is_neg_infinity()); 116 } 117 //! check to see if date is greater than all possible dates 118 BOOST_CXX14_CONSTEXPR is_pos_infinity() const119 bool is_pos_infinity() const 120 { 121 return time_.is_pos_infinity(); 122 } 123 //! check to see if date is greater than all possible dates 124 BOOST_CXX14_CONSTEXPR is_neg_infinity() const125 bool is_neg_infinity() const 126 { 127 return time_.is_neg_infinity(); 128 } 129 //! check to see if time is a special value 130 BOOST_CXX14_CONSTEXPR is_special() const131 bool is_special() const 132 { 133 return(is_not_a_date_time() || is_infinity()); 134 } 135 //!Equality operator -- others generated by boost::equality_comparable 136 BOOST_CXX14_CONSTEXPR operator ==(const time_type & rhs) const137 bool operator==(const time_type& rhs) const 138 { 139 return time_system::is_equal(time_,rhs.time_); 140 } 141 //!Equality operator -- others generated by boost::less_than_comparable 142 BOOST_CXX14_CONSTEXPR operator <(const time_type & rhs) const143 bool operator<(const time_type& rhs) const 144 { 145 return time_system::is_less(time_,rhs.time_); 146 } 147 //! difference between two times 148 BOOST_CXX14_CONSTEXPR operator -(const time_type & rhs) const149 time_duration_type operator-(const time_type& rhs) const 150 { 151 return time_system::subtract_times(time_, rhs.time_); 152 } 153 //! add date durations 154 BOOST_CXX14_CONSTEXPR operator +(const date_duration_type & dd) const155 time_type operator+(const date_duration_type& dd) const 156 { 157 return time_system::add_days(time_, dd); 158 } 159 BOOST_CXX14_CONSTEXPR operator +=(const date_duration_type & dd)160 time_type operator+=(const date_duration_type& dd) 161 { 162 time_ = (time_system::get_time_rep(date() + dd, time_of_day())); 163 return time_type(time_); 164 } 165 //! subtract date durations 166 BOOST_CXX14_CONSTEXPR operator -(const date_duration_type & dd) const167 time_type operator-(const date_duration_type& dd) const 168 { 169 return time_system::subtract_days(time_, dd); 170 } 171 BOOST_CXX14_CONSTEXPR operator -=(const date_duration_type & dd)172 time_type operator-=(const date_duration_type& dd) 173 { 174 time_ = (time_system::get_time_rep(date() - dd, time_of_day())); 175 return time_type(time_); 176 } 177 //! add time durations 178 BOOST_CXX14_CONSTEXPR operator +(const time_duration_type & td) const179 time_type operator+(const time_duration_type& td) const 180 { 181 return time_type(time_system::add_time_duration(time_, td)); 182 } 183 BOOST_CXX14_CONSTEXPR operator +=(const time_duration_type & td)184 time_type operator+=(const time_duration_type& td) 185 { 186 time_ = time_system::add_time_duration(time_,td); 187 return time_type(time_); 188 } 189 //! subtract time durations 190 BOOST_CXX14_CONSTEXPR operator -(const time_duration_type & rhs) const191 time_type operator-(const time_duration_type& rhs) const 192 { 193 return time_system::subtract_time_duration(time_, rhs); 194 } 195 BOOST_CXX14_CONSTEXPR operator -=(const time_duration_type & td)196 time_type operator-=(const time_duration_type& td) 197 { 198 time_ = time_system::subtract_time_duration(time_, td); 199 return time_type(time_); 200 } 201 202 protected: 203 time_rep_type time_; 204 }; 205 206 207 208 209 210 } } //namespace date_time::boost 211 212 213 #endif 214 215