10 #ifndef SC_TIMESPAN_HPP 11 #define SC_TIMESPAN_HPP 18 #include <type_traits> 21 #include "util/format.hpp" 40 using time_t = int_least64_t;
44 template<
typename Rep>
46 time(static_cast<time_t>(t))
51 constexpr timespan_t() : time( 0 )
54 constexpr
double total_minutes()
const 56 return static_cast<double>(time) * (1.0 / (60 * 1000));
58 constexpr
double total_seconds()
const 60 return static_cast<double>(time) * (1.0 / 1000);
62 constexpr time_t total_millis()
const 67 template <typename Rep, typename = std::enable_if_t<std::is_arithmetic<Rep>::value>>
68 static constexpr timespan_t from_millis(Rep millis)
70 return timespan_t(millis);
73 template <typename Rep, typename = std::enable_if_t<std::is_arithmetic<Rep>::value>>
74 static constexpr timespan_t from_seconds(Rep seconds)
76 return timespan_t(static_cast<time_t>(seconds * 1000));
79 template <typename Rep, typename = std::enable_if_t<std::is_arithmetic<Rep>::value>>
80 static constexpr timespan_t from_minutes(Rep minutes)
82 return timespan_t(static_cast<time_t>(minutes * (60 * 1000)));
85 constexpr
bool operator==(timespan_t right)
const 87 return time == right.time;
89 constexpr
bool operator!=(timespan_t right)
const 91 return time != right.time;
94 constexpr
bool operator>(timespan_t right)
const 96 return time > right.time;
98 constexpr
bool operator>=(timespan_t right)
const 100 return time >= right.time;
102 constexpr
bool operator<(timespan_t right)
const 104 return time < right.time;
106 constexpr
bool operator<=(timespan_t right)
const 108 return time <= right.time;
111 constexpr timespan_t& operator+=(timespan_t right)
113 assert(right < zero() || *
this < zero() || *
this <= max() - right);
114 assert(right > zero() || *
this >= min() - right);
118 constexpr timespan_t& operator-=(timespan_t right)
120 assert(right < zero() || *
this >= right + min());
121 assert(right > zero() || *
this <= right + max());
125 constexpr timespan_t& operator %=(timespan_t right)
131 template <typename Rep, typename = std::enable_if_t<std::is_arithmetic<Rep>::value>>
132 constexpr timespan_t& operator*=(Rep right)
134 time =
static_cast<time_t
>(time * right);
138 template <typename Rep, typename = std::enable_if_t<std::is_arithmetic<Rep>::value>>
139 constexpr timespan_t& operator/=(Rep right)
141 time =
static_cast<time_t
>(time / right);
145 friend constexpr timespan_t operator+(timespan_t right)
149 friend constexpr timespan_t operator-(timespan_t right)
151 return timespan_t(-right.time);
154 friend constexpr timespan_t operator+(timespan_t left, timespan_t right)
156 return left += right;
159 friend constexpr timespan_t operator-(timespan_t left, timespan_t right)
161 return left -= right;
164 template <
typename Rep>
165 friend constexpr
auto operator*(timespan_t left, Rep right) -> std::enable_if_t<std::is_arithmetic<Rep>::value, timespan_t>
167 return left *= right;
170 template <
typename Rep>
171 friend constexpr
auto operator*(Rep left, timespan_t right) -> std::enable_if_t<std::is_arithmetic<Rep>::value, timespan_t>
173 return right *= left;
176 template <
typename Rep>
177 friend constexpr
auto operator/(timespan_t left, Rep right) -> std::enable_if_t<std::is_arithmetic<Rep>::value, timespan_t>
179 return left /= right;
182 friend constexpr
double operator/(timespan_t left, timespan_t right)
184 return static_cast<double>(left.time) / right.time;
187 friend constexpr timespan_t operator%(timespan_t left, timespan_t right)
189 return left %= right;
192 using native_t = time_t;
196 static constexpr native_t to_native(timespan_t t)
202 template<
typename Rep>
203 static constexpr timespan_t from_native(Rep t)
205 return timespan_t(t);
208 static constexpr timespan_t zero()
212 static constexpr timespan_t max()
214 return timespan_t( std::numeric_limits<time_t>::max() );
216 static constexpr timespan_t min()
218 static_assert( !std::is_floating_point<time_t>::value );
219 return timespan_t( std::numeric_limits<time_t>::min() );
222 friend void sc_format_to( timespan_t, fmt::format_context::iterator );
229 constexpr
timespan_t operator"" _ms(
unsigned long long time )
231 return timespan_t::from_millis( time );
234 constexpr
timespan_t operator"" _s(
unsigned long long time )
236 return timespan_t::from_seconds( time );
239 constexpr
timespan_t operator"" _s(
long double time )
241 return timespan_t::from_seconds( time );
244 constexpr
timespan_t operator"" _min(
unsigned long long time )
246 return timespan_t::from_minutes( time );
249 constexpr
timespan_t operator"" _min(
long double time )
251 return timespan_t::from_minutes( time );
254 #endif // SC_TIMESPAN_HPP Definition: timespan.cpp:10
Class for representing InGame time.
Definition: timespan.hpp:37