1 #ifndef DATE_TIME_TIME_DURATION_HPP___
2 #define DATE_TIME_TIME_DURATION_HPP___
3 
4 /* Copyright (c) 2002,2003 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: 2009-06-04 04:24:49 -0400 (Thu, 04 Jun 2009) $
10  */
11 
12 #include <boost/cstdint.hpp>
13 #include <boost/operators.hpp>
14 #include <boost/date_time/time_defs.hpp>
15 #include <boost/date_time/special_defs.hpp>
16 #include <boost/date_time/compiler_config.hpp>
17 
18 namespace boost {
19 namespace date_time {
20 
21 
22   //! Represents some amount of elapsed time measure to a given resolution
23   /*! This class represents a standard set of capabilities for all
24       counted time durations.  Time duration implementations should derive
25       from this class passing their type as the first template parameter.
26       This design allows the subclass duration types to provide custom
27       construction policies or other custom features not provided here.
28 
29       @param T The subclass type
30       @param rep_type The time resolution traits for this duration type.
31   */
32   template<class T, typename rep_type>
33   class time_duration : private
34       boost::less_than_comparable<T
35     , boost::equality_comparable<T
36     > >
37   /* dividable, addable, and subtractable operator templates
38    * won't work with this class (MSVC++ 6.0). return type
39    * from '+=' is different than expected return type
40    * from '+'. multipliable probably wont work
41    * either (haven't tried) */
42   {
43   public:
44     typedef T duration_type;  //the subclass
45     typedef rep_type traits_type;
46     typedef typename rep_type::day_type  day_type;
47     typedef typename rep_type::hour_type hour_type;
48     typedef typename rep_type::min_type  min_type;
49     typedef typename rep_type::sec_type  sec_type;
50     typedef typename rep_type::fractional_seconds_type fractional_seconds_type;
51     typedef typename rep_type::tick_type tick_type;
52     typedef typename rep_type::impl_type impl_type;
53 
time_duration()54     time_duration() : ticks_(0) {}
time_duration(hour_type hours_in,min_type minutes_in,sec_type seconds_in=0,fractional_seconds_type frac_sec_in=0)55     time_duration(hour_type hours_in,
56                   min_type minutes_in,
57                   sec_type seconds_in=0,
58                   fractional_seconds_type frac_sec_in = 0) :
59       ticks_(rep_type::to_tick_count(hours_in,minutes_in,seconds_in,frac_sec_in))
60     {}
61     // copy constructor required for dividable<>
62     //! Construct from another time_duration (Copy constructor)
time_duration(const time_duration<T,rep_type> & other)63     time_duration(const time_duration<T, rep_type>& other)
64       : ticks_(other.ticks_)
65     {}
66     //! Construct from special_values
time_duration(special_values sv)67     time_duration(special_values sv) : ticks_(impl_type::from_special(sv))
68     {}
69     //! Returns smallest representable duration
unit()70     static duration_type unit()
71     {
72       return duration_type(0,0,0,1);
73     }
74     //! Return the number of ticks in a second
ticks_per_second()75     static tick_type ticks_per_second()
76     {
77       return rep_type::res_adjust();
78     }
79     //! Provide the resolution of this duration type
resolution()80     static time_resolutions resolution()
81     {
82       return rep_type::resolution();
83     }
84     //! Returns number of hours in the duration
hours() const85     hour_type hours()   const
86     {
87       return static_cast<hour_type>(ticks() / (3600*ticks_per_second()));
88     }
89     //! Returns normalized number of minutes
minutes() const90     min_type minutes() const
91     {
92       return static_cast<min_type>((ticks() / (60*ticks_per_second())) % 60);
93     }
94     //! Returns normalized number of seconds (0..60)
seconds() const95     sec_type seconds() const
96     {
97       return static_cast<sec_type>((ticks()/ticks_per_second()) % 60);
98     }
99     //! Returns total number of seconds truncating any fractional seconds
total_seconds() const100     sec_type total_seconds() const
101     {
102       return static_cast<sec_type>(ticks() / ticks_per_second());
103     }
104     //! Returns total number of milliseconds truncating any fractional seconds
total_milliseconds() const105     tick_type total_milliseconds() const
106     {
107       if (ticks_per_second() < 1000) {
108         return ticks() * (static_cast<tick_type>(1000) / ticks_per_second());
109       }
110       return ticks() / (ticks_per_second() / static_cast<tick_type>(1000)) ;
111     }
112     //! Returns total number of nanoseconds truncating any sub millisecond values
total_nanoseconds() const113     tick_type total_nanoseconds() const
114     {
115       if (ticks_per_second() < 1000000000) {
116         return ticks() * (static_cast<tick_type>(1000000000) / ticks_per_second());
117       }
118       return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000000)) ;
119     }
120     //! Returns total number of microseconds truncating any sub microsecond values
total_microseconds() const121     tick_type total_microseconds() const
122     {
123       if (ticks_per_second() < 1000000) {
124         return ticks() * (static_cast<tick_type>(1000000) / ticks_per_second());
125       }
126       return ticks() / (ticks_per_second() / static_cast<tick_type>(1000000)) ;
127     }
128     //! Returns count of fractional seconds at given resolution
fractional_seconds() const129     fractional_seconds_type fractional_seconds() const
130     {
131       return (ticks() % ticks_per_second());
132     }
133     //! Returns number of possible digits in fractional seconds
num_fractional_digits()134     static unsigned short num_fractional_digits()
135     {
136       return rep_type::num_fractional_digits();
137     }
invert_sign() const138     duration_type invert_sign() const
139     {
140       return duration_type(ticks_ * (-1));
141     }
is_negative() const142     bool is_negative() const
143     {
144       return ticks_ < 0;
145     }
operator <(const time_duration & rhs) const146     bool operator<(const time_duration& rhs)  const
147     {
148       return ticks_ <  rhs.ticks_;
149     }
operator ==(const time_duration & rhs) const150     bool operator==(const time_duration& rhs)  const
151     {
152       return ticks_ ==  rhs.ticks_;
153     }
154     //! unary- Allows for time_duration td = -td1
operator -() const155     duration_type operator-()const
156     {
157       return duration_type(ticks_ * (-1));
158     }
operator -(const duration_type & d) const159     duration_type operator-(const duration_type& d) const
160     {
161       return duration_type(ticks_ - d.ticks_);
162     }
operator +(const duration_type & d) const163     duration_type operator+(const duration_type& d) const
164     {
165       return duration_type(ticks_ + d.ticks_);
166     }
operator /(int divisor) const167     duration_type operator/(int divisor) const
168     {
169       return duration_type(ticks_ / divisor);
170     }
operator -=(const duration_type & d)171     duration_type operator-=(const duration_type& d)
172     {
173       ticks_ = ticks_ - d.ticks_;
174       return duration_type(ticks_);
175     }
operator +=(const duration_type & d)176     duration_type operator+=(const duration_type& d)
177     {
178       ticks_ = ticks_ + d.ticks_;
179       return duration_type(ticks_);
180     }
181     //! Division operations on a duration with an integer.
operator /=(int divisor)182     duration_type operator/=(int divisor)
183     {
184       ticks_ = ticks_ / divisor;
185       return duration_type(ticks_);
186     }
187     //! Multiplication operations an a duration with an integer
operator *(int rhs) const188     duration_type operator*(int rhs) const
189     {
190       return duration_type(ticks_ * rhs);
191     }
operator *=(int divisor)192     duration_type operator*=(int divisor)
193     {
194       ticks_ = ticks_ * divisor;
195       return duration_type(ticks_);
196     }
ticks() const197     tick_type ticks() const
198     {
199       return traits_type::as_number(ticks_);
200     }
201 
202     //! Is ticks_ a special value?
is_special() const203     bool is_special()const
204     {
205       if(traits_type::is_adapted())
206       {
207         return ticks_.is_special();
208       }
209       else{
210         return false;
211       }
212     }
213     //! Is duration pos-infinity
is_pos_infinity() const214     bool is_pos_infinity()const
215     {
216       if(traits_type::is_adapted())
217       {
218         return ticks_.is_pos_infinity();
219       }
220       else{
221         return false;
222       }
223     }
224     //! Is duration neg-infinity
is_neg_infinity() const225     bool is_neg_infinity()const
226     {
227       if(traits_type::is_adapted())
228       {
229         return ticks_.is_neg_infinity();
230       }
231       else{
232         return false;
233       }
234     }
235     //! Is duration not-a-date-time
is_not_a_date_time() const236     bool is_not_a_date_time()const
237     {
238       if(traits_type::is_adapted())
239       {
240         return ticks_.is_nan();
241       }
242       else{
243         return false;
244       }
245     }
246 
247     //! Used for special_values output
get_rep() const248     impl_type get_rep()const
249     {
250       return ticks_;
251     }
252 
253   protected:
time_duration(impl_type in)254     explicit time_duration(impl_type in) : ticks_(in) {};
255     impl_type ticks_;
256   };
257 
258 
259 
260   //! Template for instantiating derived adjusting durations
261   /* These templates are designed to work with multiples of
262    * 10 for frac_of_second and resoultion adjustment
263    */
264   template<class base_duration, boost::int64_t frac_of_second>
265   class subsecond_duration : public base_duration
266   {
267   public:
268     typedef typename base_duration::traits_type traits_type;
subsecond_duration(boost::int64_t ss)269     explicit subsecond_duration(boost::int64_t ss) :
270       base_duration(0,0,0,ss*traits_type::res_adjust()/frac_of_second)
271     {}
272   };
273 
274 
275 
276 } } //namespace date_time
277 
278 
279 
280 
281 #endif
282 
283