1 #if !defined(sol_date_time_duration_hpp_included)
2 #define sol_date_time_duration_hpp_included
3 /// \addtogroup sol_date_time
8 * \file sol/date_time/duration.hpp
9 * \brief Length of time unattached to a any time point.
10 * \author Sven Suursoho
14 namespace sol
{ namespace date_time
{
17 /// Common duration type, ancestor class for more specific duration
28 /// Count type at the resolution of duration type
29 typedef TickType tick_type
;
32 /// Number of ticks per second
33 static tick_type
const ticks_per_second
= TicksPerSecond
;
36 /// Number of seconds per tick
37 static tick_type
const seconds_per_tick
= SecondsPerTick
;
40 /// True if resolution is less than 1 second
41 static bool const is_subsecond
= TicksPerSecond
> SecondsPerTick
;
44 /// \internal Helper type definition
45 typedef duration_type
<
46 FinalType
, TickType
, TicksPerSecond
, SecondsPerTick
50 /// Construct duration object (defaulting to count of 0)
51 duration_type (tick_type d
= 0)
56 /// Return the count at the resolution of the time duration type
64 /// Return this + that
66 operator+ (FinalType
const &that
) const
68 return FinalType(count_
+ that
.count_
);
72 /// Return this += that
74 operator+= (FinalType
const &that
)
76 count_
+= that
.count_
;
77 return *static_cast<FinalType
*>(this);
81 /// Return this - that
83 operator- (FinalType
const &that
) const
85 return FinalType(count_
- that
.count_
);
89 /// Return this -= that
91 operator-= (FinalType
const &that
)
93 count_
-= that
.count_
;
94 return *static_cast<FinalType
*>(this);
98 /// Return this/divisor
100 operator/ (int divisor
) const
102 return FinalType(count_
/divisor
);
106 /// Return this/=divisor
108 operator/= (int divisor
)
111 return *static_cast<FinalType
*>(this);
115 /// Return this*divisor
117 operator* (int multiplier
) const
119 return FinalType(count_
*multiplier
);
123 /// Return this*=divisor
125 operator*= (int multiplier
)
127 count_
*= multiplier
;
128 return *static_cast<FinalType
*>(this);
132 /// Return negate of this
136 return FinalType(-count_
);
140 /// Return true if this == that
142 operator== (this_type
const &that
) const
144 return count_
== that
.count_
;
148 /// Return true if this != that
150 operator!= (this_type
const &that
) const
152 return count_
!= that
.count_
;
156 /// Return true if this > that
158 operator> (this_type
const &that
) const
160 return count_
> that
.count_
;
164 /// Return true if this >= that
166 operator>= (this_type
const &that
) const
168 return count_
>= that
.count_
;
172 /// Return true if this < that
174 operator< (this_type
const &that
) const
176 return count_
< that
.count_
;
180 /// Return true if this <= that
182 operator<= (this_type
const &that
) const
184 return count_
<= that
.count_
;
194 /// Duration in nanoseconds
196 : public duration_type
<nanoseconds
, long long, 1000000000, 0>
200 nanoseconds (tick_type d
= 0)
206 /// Duration in microseconds
208 : public duration_type
<microseconds
, long long, 1000000, 0>
212 microseconds (tick_type d
= 0)
217 operator nanoseconds () const
219 return count_
*(nanoseconds::ticks_per_second
/ticks_per_second
);
224 /// Duration in milliseconds
226 : public duration_type
<milliseconds
, long long, 1000, 0>
230 milliseconds (tick_type d
= 0)
235 operator nanoseconds () const
237 return count_
*(nanoseconds::ticks_per_second
/ticks_per_second
);
241 operator microseconds () const
243 return count_
*(microseconds::ticks_per_second
/ticks_per_second
);
248 /// Duration in seconds
250 : public duration_type
<seconds
, long long, 1, 1>
254 seconds (tick_type d
= 0)
259 operator nanoseconds () const
261 return count_
*(nanoseconds::ticks_per_second
/ticks_per_second
);
265 operator microseconds () const
267 return count_
*(microseconds::ticks_per_second
/ticks_per_second
);
271 operator milliseconds () const
273 return count_
*(milliseconds::ticks_per_second
/ticks_per_second
);
278 /// Duration in minutes
280 : public duration_type
<minutes
, long long, 0, 60>
284 minutes (tick_type d
= 0): this_type(d
)
288 operator nanoseconds () const
290 return count_
*seconds_per_tick
*nanoseconds::ticks_per_second
;
294 operator microseconds () const
296 return count_
*seconds_per_tick
*microseconds::ticks_per_second
;
300 operator milliseconds () const
302 return count_
*seconds_per_tick
*milliseconds::ticks_per_second
;
306 operator seconds () const
308 return count_
*seconds_per_tick
;
313 /// Duration in hours
315 : public duration_type
<hours
, long long, 0, 3600>
319 hours (tick_type d
= 0)
324 operator nanoseconds () const
326 return count_
*seconds_per_tick
*nanoseconds::ticks_per_second
;
330 operator microseconds () const
332 return count_
*seconds_per_tick
*microseconds::ticks_per_second
;
336 operator milliseconds () const
338 return count_
*seconds_per_tick
*milliseconds::ticks_per_second
;
342 operator seconds () const
344 return count_
*seconds_per_tick
;
348 operator minutes () const
350 return count_
*(seconds_per_tick
/minutes::seconds_per_tick
);
355 }} // namespace sol::date_time
359 #endif // sol_date_time_duration_hpp_included