2 * Copyright (C) 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <wtf/MathExtras.h>
35 class TimeWithDynamicClockType;
42 explicit constexpr Seconds(double value)
47 double value() const { return m_value; }
49 double minutes() const { return m_value / 60; }
50 double seconds() const { return m_value; }
51 double milliseconds() const { return seconds() * 1000; }
52 double microseconds() const { return milliseconds() * 1000; }
53 double nanoseconds() const { return microseconds() * 1000; }
55 static constexpr Seconds fromMinutes(double minutes)
57 return Seconds(minutes * 60);
60 static constexpr Seconds fromMilliseconds(double milliseconds)
62 return Seconds(milliseconds / 1000);
65 static constexpr Seconds fromMicroseconds(double microseconds)
67 return fromMilliseconds(microseconds / 1000);
70 static constexpr Seconds fromNanoseconds(double nanoseconds)
72 return fromMicroseconds(nanoseconds / 1000);
75 static constexpr Seconds infinity()
77 return Seconds(std::numeric_limits<double>::infinity());
80 explicit operator bool() const { return !!m_value; }
82 Seconds operator+(Seconds other) const
84 return Seconds(value() + other.value());
87 Seconds operator-(Seconds other) const
89 return Seconds(value() - other.value());
92 Seconds operator-() const
94 return Seconds(-value());
97 // It makes sense to consider scaling a duration, like, "I want to wait 5 times as long as
99 Seconds operator*(double scalar) const
101 return Seconds(value() * scalar);
104 Seconds operator/(double scalar) const
106 return Seconds(value() / scalar);
109 // It's reasonable to think about ratios between Seconds.
110 double operator/(Seconds other) const
112 return value() / other.value();
115 Seconds operator%(double scalar) const
117 return Seconds(fmod(value(), scalar));
120 // This solves for r, where:
122 // floor(this / other) + r / other = this / other
124 // Therefore, if this is Seconds then r is Seconds.
125 Seconds operator%(Seconds other) const
127 return Seconds(fmod(value(), other.value()));
130 Seconds& operator+=(Seconds other)
132 return *this = *this + other;
135 Seconds& operator-=(Seconds other)
137 return *this = *this - other;
140 Seconds& operator*=(double scalar)
142 return *this = *this * scalar;
145 Seconds& operator/=(double scalar)
147 return *this = *this / scalar;
150 Seconds& operator%=(double scalar)
152 return *this = *this % scalar;
155 Seconds& operator%=(Seconds other)
157 return *this = *this % other;
160 WTF_EXPORT_PRIVATE WallTime operator+(WallTime) const;
161 WTF_EXPORT_PRIVATE MonotonicTime operator+(MonotonicTime) const;
162 WTF_EXPORT_PRIVATE TimeWithDynamicClockType operator+(const TimeWithDynamicClockType&) const;
164 WTF_EXPORT_PRIVATE WallTime operator-(WallTime) const;
165 WTF_EXPORT_PRIVATE MonotonicTime operator-(MonotonicTime) const;
166 WTF_EXPORT_PRIVATE TimeWithDynamicClockType operator-(const TimeWithDynamicClockType&) const;
168 bool operator==(Seconds other) const
170 return m_value == other.m_value;
173 bool operator!=(Seconds other) const
175 return m_value != other.m_value;
178 bool operator<(Seconds other) const
180 return m_value < other.m_value;
183 bool operator>(Seconds other) const
185 return m_value > other.m_value;
188 bool operator<=(Seconds other) const
190 return m_value <= other.m_value;
193 bool operator>=(Seconds other) const
195 return m_value >= other.m_value;
198 WTF_EXPORT_PRIVATE void dump(PrintStream&) const;
201 double m_value { 0 };
204 constexpr Seconds operator"" _min(long double minutes)
206 return Seconds::fromMinutes(minutes);
209 constexpr Seconds operator"" _s(long double seconds)
211 return Seconds(seconds);
214 constexpr Seconds operator"" _ms(long double milliseconds)
216 return Seconds::fromMilliseconds(milliseconds);
219 constexpr Seconds operator"" _us(long double microseconds)
221 return Seconds::fromMicroseconds(microseconds);
224 constexpr Seconds operator"" _ns(long double nanoseconds)
226 return Seconds::fromNanoseconds(nanoseconds);
229 constexpr Seconds operator"" _min(unsigned long long minutes)
231 return Seconds::fromMinutes(minutes);
234 constexpr Seconds operator"" _s(unsigned long long seconds)
236 return Seconds(seconds);
239 constexpr Seconds operator"" _ms(unsigned long long milliseconds)
241 return Seconds::fromMilliseconds(milliseconds);
244 constexpr Seconds operator"" _us(unsigned long long microseconds)
246 return Seconds::fromMicroseconds(microseconds);
249 constexpr Seconds operator"" _ns(unsigned long long nanoseconds)
251 return Seconds::fromNanoseconds(nanoseconds);
254 WTF_EXPORT_PRIVATE void sleep(Seconds);
260 #endif // WTF_Seconds_h