[29] | 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 or http://www.boost.org/LICENSE-1.0) |
---|
| 8 | * Author: Jeff Garland, Bart Garst |
---|
| 9 | * $Date: 2005/04/23 05:39:52 $ |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /*! @file time.hpp |
---|
| 14 | This file contains the interface for the time associated classes. |
---|
| 15 | */ |
---|
| 16 | #include "boost/date_time/time_defs.hpp" |
---|
| 17 | #include "boost/operators.hpp" |
---|
| 18 | #include <string> |
---|
| 19 | |
---|
| 20 | namespace boost { |
---|
| 21 | namespace date_time { |
---|
| 22 | |
---|
| 23 | //! Representation of a precise moment in time, including the date. |
---|
| 24 | /*! |
---|
| 25 | This class is a skeleton for the interface of a temporal type |
---|
| 26 | with a resolution that is higher than a day. It is intended that |
---|
| 27 | this class be the base class and that the actual time |
---|
| 28 | class be derived using the BN pattern. In this way, the derived |
---|
| 29 | class can make decisions such as 'should there be a default constructor' |
---|
| 30 | and what should it set its value to, should there be optional constructors |
---|
| 31 | say allowing only an time_durations that generate a time from a clock,etc. |
---|
| 32 | So, in fact multiple time types can be created for a time_system with |
---|
| 33 | different construction policies, and all of them can perform basic |
---|
| 34 | operations by only writing a copy constructor. Finally, compiler |
---|
| 35 | errors are also shorter. |
---|
| 36 | |
---|
| 37 | The real behavior of the time class is provided by the time_system |
---|
| 38 | template parameter. This class must provide all the logic |
---|
| 39 | for addition, subtraction, as well as define all the interface |
---|
| 40 | types. |
---|
| 41 | |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | template <class T, class time_system> |
---|
| 45 | class base_time : private |
---|
| 46 | boost::less_than_comparable<T |
---|
| 47 | , boost::equality_comparable<T |
---|
| 48 | > > |
---|
| 49 | { |
---|
| 50 | public: |
---|
| 51 | typedef T time_type; |
---|
| 52 | typedef typename time_system::time_rep_type time_rep_type; |
---|
| 53 | typedef typename time_system::date_type date_type; |
---|
| 54 | typedef typename time_system::date_duration_type date_duration_type; |
---|
| 55 | typedef typename time_system::time_duration_type time_duration_type; |
---|
| 56 | //typedef typename time_system::hms_type hms_type; |
---|
| 57 | |
---|
| 58 | base_time(const date_type& day, |
---|
| 59 | const time_duration_type& td, |
---|
| 60 | dst_flags dst=not_dst) : |
---|
| 61 | time_(time_system::get_time_rep(day, td, dst)) |
---|
| 62 | {} |
---|
| 63 | base_time(special_values sv) : |
---|
| 64 | time_(time_system::get_time_rep(sv)) |
---|
| 65 | {} |
---|
| 66 | base_time(const time_rep_type& rhs) : |
---|
| 67 | time_(rhs) |
---|
| 68 | {} |
---|
| 69 | date_type date() const |
---|
| 70 | { |
---|
| 71 | return time_system::get_date(time_); |
---|
| 72 | } |
---|
| 73 | time_duration_type time_of_day() const |
---|
| 74 | { |
---|
| 75 | return time_system::get_time_of_day(time_); |
---|
| 76 | } |
---|
| 77 | /*! Optional bool parameter will return time zone as an offset |
---|
| 78 | * (ie "+07:00"). Empty string is returned for classes that do |
---|
| 79 | * not use a time_zone */ |
---|
| 80 | std::string zone_name(bool as_offset=false) const |
---|
| 81 | { |
---|
| 82 | return time_system::zone_name(time_); |
---|
| 83 | } |
---|
| 84 | /*! Optional bool parameter will return time zone as an offset |
---|
| 85 | * (ie "+07:00"). Empty string is returned for classes that do |
---|
| 86 | * not use a time_zone */ |
---|
| 87 | std::string zone_abbrev(bool as_offset=false) const |
---|
| 88 | { |
---|
| 89 | return time_system::zone_name(time_); |
---|
| 90 | } |
---|
| 91 | //! An empty string is returned for classes that do not use a time_zone |
---|
| 92 | std::string zone_as_posix_string() const |
---|
| 93 | { |
---|
| 94 | return std::string(""); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | //! check to see if date is not a value |
---|
| 98 | bool is_not_a_date_time() const |
---|
| 99 | { |
---|
| 100 | return time_.is_not_a_date_time(); |
---|
| 101 | } |
---|
| 102 | //! check to see if date is one of the infinity values |
---|
| 103 | bool is_infinity() const |
---|
| 104 | { |
---|
| 105 | return (is_pos_infinity() || is_neg_infinity()); |
---|
| 106 | } |
---|
| 107 | //! check to see if date is greater than all possible dates |
---|
| 108 | bool is_pos_infinity() const |
---|
| 109 | { |
---|
| 110 | return time_.is_pos_infinity(); |
---|
| 111 | } |
---|
| 112 | //! check to see if date is greater than all possible dates |
---|
| 113 | bool is_neg_infinity() const |
---|
| 114 | { |
---|
| 115 | return time_.is_neg_infinity(); |
---|
| 116 | } |
---|
| 117 | //! check to see if time is a special value |
---|
| 118 | bool is_special() const |
---|
| 119 | { |
---|
| 120 | return(is_not_a_date_time() || is_infinity()); |
---|
| 121 | } |
---|
| 122 | //!Equality operator -- others generated by boost::equality_comparable |
---|
| 123 | bool operator==(const time_type& rhs) const |
---|
| 124 | { |
---|
| 125 | return time_system::is_equal(time_,rhs.time_); |
---|
| 126 | } |
---|
| 127 | //!Equality operator -- others generated by boost::less_than_comparable |
---|
| 128 | bool operator<(const time_type& rhs) const |
---|
| 129 | { |
---|
| 130 | return time_system::is_less(time_,rhs.time_); |
---|
| 131 | } |
---|
| 132 | //! difference between two times |
---|
| 133 | time_duration_type operator-(const time_type& rhs) const |
---|
| 134 | { |
---|
| 135 | return time_system::subtract_times(time_, rhs.time_); |
---|
| 136 | } |
---|
| 137 | //! add date durations |
---|
| 138 | time_type operator+(const date_duration_type& dd) const |
---|
| 139 | { |
---|
| 140 | return time_system::add_days(time_, dd); |
---|
| 141 | } |
---|
| 142 | time_type operator+=(const date_duration_type& dd) |
---|
| 143 | { |
---|
| 144 | time_ = (time_system::get_time_rep(date() + dd, time_of_day())); |
---|
| 145 | return time_type(time_); |
---|
| 146 | } |
---|
| 147 | //! subtract date durations |
---|
| 148 | time_type operator-(const date_duration_type& dd) const |
---|
| 149 | { |
---|
| 150 | return time_system::subtract_days(time_, dd); |
---|
| 151 | } |
---|
| 152 | time_type operator-=(const date_duration_type& dd) |
---|
| 153 | { |
---|
| 154 | time_ = (time_system::get_time_rep(date() - dd, time_of_day())); |
---|
| 155 | return time_type(time_); |
---|
| 156 | } |
---|
| 157 | //! add time durations |
---|
| 158 | time_type operator+(const time_duration_type& td) const |
---|
| 159 | { |
---|
| 160 | return time_type(time_system::add_time_duration(time_, td)); |
---|
| 161 | } |
---|
| 162 | time_type operator+=(const time_duration_type& td) |
---|
| 163 | { |
---|
| 164 | time_ = (time_system::get_time_rep(date(), time_of_day() + td)); |
---|
| 165 | return time_type(time_); |
---|
| 166 | } |
---|
| 167 | //! subtract time durations |
---|
| 168 | time_type operator-(const time_duration_type& rhs) const |
---|
| 169 | { |
---|
| 170 | return time_system::subtract_time_duration(time_, rhs); |
---|
| 171 | } |
---|
| 172 | time_type operator-=(const time_duration_type& td) |
---|
| 173 | { |
---|
| 174 | time_ = (time_system::get_time_rep(date(), time_of_day() - td)); |
---|
| 175 | return time_type(time_); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | protected: |
---|
| 179 | time_rep_type time_; |
---|
| 180 | }; |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | } } //namespace date_time::boost |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | #endif |
---|
| 190 | |
---|