1 #ifndef DATE_TIME_TIME_HPP___ 2 #define DATE_TIME_TIME_HPP___ 3 4 /* Copyright (c) 2002,2003,2005 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 base_time(const date_type & day,const time_duration_type & td,dst_flags dst=not_dst)61 base_time(const date_type& day, 62 const time_duration_type& td, 63 dst_flags dst=not_dst) : 64 time_(time_system::get_time_rep(day, td, dst)) 65 {} base_time(special_values sv)66 base_time(special_values sv) : 67 time_(time_system::get_time_rep(sv)) 68 {} base_time(const time_rep_type & rhs)69 base_time(const time_rep_type& rhs) : 70 time_(rhs) 71 {} date() const72 date_type date() const 73 { 74 return time_system::get_date(time_); 75 } time_of_day() const76 time_duration_type time_of_day() const 77 { 78 return time_system::get_time_of_day(time_); 79 } 80 /*! Optional bool parameter will return time zone as an offset 81 * (ie "+07:00"). Empty string is returned for classes that do 82 * not use a time_zone */ zone_name(bool=false) const83 std::string zone_name(bool /*as_offset*/=false) const 84 { 85 return time_system::zone_name(time_); 86 } 87 /*! Optional bool parameter will return time zone as an offset 88 * (ie "+07:00"). Empty string is returned for classes that do 89 * not use a time_zone */ zone_abbrev(bool=false) const90 std::string zone_abbrev(bool /*as_offset*/=false) const 91 { 92 return time_system::zone_name(time_); 93 } 94 //! An empty string is returned for classes that do not use a time_zone zone_as_posix_string() const95 std::string zone_as_posix_string() const 96 { 97 return std::string(); 98 } 99 100 //! check to see if date is not a value is_not_a_date_time() const101 bool is_not_a_date_time() const 102 { 103 return time_.is_not_a_date_time(); 104 } 105 //! check to see if date is one of the infinity values is_infinity() const106 bool is_infinity() const 107 { 108 return (is_pos_infinity() || is_neg_infinity()); 109 } 110 //! check to see if date is greater than all possible dates is_pos_infinity() const111 bool is_pos_infinity() const 112 { 113 return time_.is_pos_infinity(); 114 } 115 //! check to see if date is greater than all possible dates is_neg_infinity() const116 bool is_neg_infinity() const 117 { 118 return time_.is_neg_infinity(); 119 } 120 //! check to see if time is a special value is_special() const121 bool is_special() const 122 { 123 return(is_not_a_date_time() || is_infinity()); 124 } 125 //!Equality operator -- others generated by boost::equality_comparable operator ==(const time_type & rhs) const126 bool operator==(const time_type& rhs) const 127 { 128 return time_system::is_equal(time_,rhs.time_); 129 } 130 //!Equality operator -- others generated by boost::less_than_comparable operator <(const time_type & rhs) const131 bool operator<(const time_type& rhs) const 132 { 133 return time_system::is_less(time_,rhs.time_); 134 } 135 //! difference between two times operator -(const time_type & rhs) const136 time_duration_type operator-(const time_type& rhs) const 137 { 138 return time_system::subtract_times(time_, rhs.time_); 139 } 140 //! add date durations operator +(const date_duration_type & dd) const141 time_type operator+(const date_duration_type& dd) const 142 { 143 return time_system::add_days(time_, dd); 144 } operator +=(const date_duration_type & dd)145 time_type operator+=(const date_duration_type& dd) 146 { 147 time_ = (time_system::get_time_rep(date() + dd, time_of_day())); 148 return time_type(time_); 149 } 150 //! subtract date durations operator -(const date_duration_type & dd) const151 time_type operator-(const date_duration_type& dd) const 152 { 153 return time_system::subtract_days(time_, dd); 154 } operator -=(const date_duration_type & dd)155 time_type operator-=(const date_duration_type& dd) 156 { 157 time_ = (time_system::get_time_rep(date() - dd, time_of_day())); 158 return time_type(time_); 159 } 160 //! add time durations operator +(const time_duration_type & td) const161 time_type operator+(const time_duration_type& td) const 162 { 163 return time_type(time_system::add_time_duration(time_, td)); 164 } operator +=(const time_duration_type & td)165 time_type operator+=(const time_duration_type& td) 166 { 167 time_ = (time_system::get_time_rep(date(), time_of_day() + td)); 168 return time_type(time_); 169 } 170 //! subtract time durations operator -(const time_duration_type & rhs) const171 time_type operator-(const time_duration_type& rhs) const 172 { 173 return time_system::subtract_time_duration(time_, rhs); 174 } operator -=(const time_duration_type & td)175 time_type operator-=(const time_duration_type& td) 176 { 177 time_ = (time_system::get_time_rep(date(), time_of_day() - td)); 178 return time_type(time_); 179 } 180 181 protected: 182 time_rep_type time_; 183 }; 184 185 186 187 188 189 } } //namespace date_time::boost 190 191 192 #endif 193 194