1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-30 19:47:48 +00:00

AK: Rename Time to Duration

That's what this class really is; in fact that's what the first line of
the comment says it is.

This commit does not rename the main files, since those will contain
other time-related classes in a little bit.
This commit is contained in:
kleines Filmröllchen 2023-03-13 16:30:34 +01:00 committed by Jelle Raaijmakers
parent 82ddc813d5
commit 213025f210
140 changed files with 634 additions and 628 deletions

View file

@ -8,12 +8,12 @@
namespace AK {
Time time_from_packed_dos(DOSPackedDate date, DOSPackedTime time)
Duration time_from_packed_dos(DOSPackedDate date, DOSPackedTime time)
{
if (date.value == 0)
return Time();
return Duration();
return Time::from_timestamp(first_dos_year + date.year, date.month, date.day, time.hour, time.minute, time.second * 2, 0);
return Duration::from_timestamp(first_dos_year + date.year, date.month, date.day, time.hour, time.minute, time.second * 2, 0);
}
DOSPackedDate to_packed_dos_date(unsigned year, unsigned month, unsigned day)

View file

@ -33,7 +33,7 @@ static_assert(sizeof(DOSPackedDate) == 2);
inline constexpr u16 first_dos_year = 1980;
Time time_from_packed_dos(DOSPackedDate, DOSPackedTime);
Duration time_from_packed_dos(DOSPackedDate, DOSPackedTime);
DOSPackedDate to_packed_dos_date(unsigned year, unsigned month, unsigned day);
DOSPackedTime to_packed_dos_time(unsigned hour, unsigned minute, unsigned second);

View file

@ -27,6 +27,7 @@ class CountingStream;
class DeprecatedFlyString;
class DeprecatedString;
class DeprecatedStringCodePointIterator;
class Duration;
class Error;
class FlyString;
class GenericLexer;
@ -44,7 +45,6 @@ class String;
class StringBuilder;
class StringImpl;
class StringView;
class Time;
class URL;
class Utf16View;
class Utf32CodePointIterator;
@ -163,6 +163,7 @@ using AK::DeprecatedFlyString;
using AK::DeprecatedString;
using AK::DeprecatedStringCodePointIterator;
using AK::DoublyLinkedList;
using AK::Duration;
using AK::Error;
using AK::ErrorOr;
using AK::FixedArray;
@ -194,7 +195,6 @@ using AK::String;
using AK::StringBuilder;
using AK::StringImpl;
using AK::StringView;
using AK::Time;
using AK::Traits;
using AK::URL;
using AK::Utf16View;

View file

@ -38,31 +38,31 @@ unsigned day_of_week(int year, unsigned month, int day)
return (year + year / 4 - year / 100 + year / 400 + seek_table[month - 1] + day) % 7;
}
Time Time::from_ticks(clock_t ticks, time_t ticks_per_second)
Duration Duration::from_ticks(clock_t ticks, time_t ticks_per_second)
{
auto secs = ticks % ticks_per_second;
i32 nsecs = 1'000'000'000 * (ticks - (ticks_per_second * secs)) / ticks_per_second;
i32 extra_secs = sane_mod(nsecs, 1'000'000'000);
return Time::from_half_sanitized(secs, extra_secs, nsecs);
return Duration::from_half_sanitized(secs, extra_secs, nsecs);
}
Time Time::from_timespec(const struct timespec& ts)
Duration Duration::from_timespec(const struct timespec& ts)
{
i32 nsecs = ts.tv_nsec;
i32 extra_secs = sane_mod(nsecs, 1'000'000'000);
return Time::from_half_sanitized(ts.tv_sec, extra_secs, nsecs);
return Duration::from_half_sanitized(ts.tv_sec, extra_secs, nsecs);
}
Time Time::from_timeval(const struct timeval& tv)
Duration Duration::from_timeval(const struct timeval& tv)
{
i32 usecs = tv.tv_usec;
i32 extra_secs = sane_mod(usecs, 1'000'000);
VERIFY(0 <= usecs && usecs < 1'000'000);
return Time::from_half_sanitized(tv.tv_sec, extra_secs, usecs * 1'000);
return Duration::from_half_sanitized(tv.tv_sec, extra_secs, usecs * 1'000);
}
i64 Time::to_truncated_seconds() const
i64 Duration::to_truncated_seconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
if (m_seconds < 0 && m_nanoseconds) {
@ -72,7 +72,7 @@ i64 Time::to_truncated_seconds() const
return m_seconds;
}
i64 Time::to_truncated_milliseconds() const
i64 Duration::to_truncated_milliseconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
Checked<i64> milliseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
@ -91,7 +91,7 @@ i64 Time::to_truncated_milliseconds() const
return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
}
i64 Time::to_truncated_microseconds() const
i64 Duration::to_truncated_microseconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
Checked<i64> microseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
@ -110,7 +110,7 @@ i64 Time::to_truncated_microseconds() const
return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
}
i64 Time::to_seconds() const
i64 Duration::to_seconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
if (m_seconds >= 0 && m_nanoseconds) {
@ -121,7 +121,7 @@ i64 Time::to_seconds() const
return m_seconds;
}
i64 Time::to_milliseconds() const
i64 Duration::to_milliseconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
Checked<i64> milliseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
@ -138,7 +138,7 @@ i64 Time::to_milliseconds() const
return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
}
i64 Time::to_microseconds() const
i64 Duration::to_microseconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
Checked<i64> microseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
@ -155,7 +155,7 @@ i64 Time::to_microseconds() const
return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
}
i64 Time::to_nanoseconds() const
i64 Duration::to_nanoseconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
Checked<i64> nanoseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
@ -170,13 +170,13 @@ i64 Time::to_nanoseconds() const
return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL;
}
timespec Time::to_timespec() const
timespec Duration::to_timespec() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
return { static_cast<time_t>(m_seconds), static_cast<long>(m_nanoseconds) };
}
timeval Time::to_timeval() const
timeval Duration::to_timeval() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
// This is done because winsock defines tv_sec and tv_usec as long, and Linux64 as long int.
@ -185,7 +185,7 @@ timeval Time::to_timeval() const
return { static_cast<sec_type>(m_seconds), static_cast<usec_type>(m_nanoseconds) / 1000 };
}
Time Time::operator+(Time const& other) const
Duration Duration::operator+(Duration const& other) const
{
VERIFY(m_nanoseconds < 1'000'000'000);
VERIFY(other.m_nanoseconds < 1'000'000'000);
@ -209,7 +209,7 @@ Time Time::operator+(Time const& other) const
other_secs += 1;
} else {
/* If *both* are INT64_MAX, then adding them will overflow in any case. */
return Time::max();
return Duration::max();
}
}
@ -217,46 +217,46 @@ Time Time::operator+(Time const& other) const
new_secs += other_secs;
if (new_secs.has_overflow()) {
if (other_secs > 0)
return Time::max();
return Duration::max();
else
return Time::min();
return Duration::min();
}
return Time { new_secs.value(), new_nsecs };
return Duration { new_secs.value(), new_nsecs };
}
Time& Time::operator+=(Time const& other)
Duration& Duration::operator+=(Duration const& other)
{
*this = *this + other;
return *this;
}
Time Time::operator-(Time const& other) const
Duration Duration::operator-(Duration const& other) const
{
VERIFY(m_nanoseconds < 1'000'000'000);
VERIFY(other.m_nanoseconds < 1'000'000'000);
if (other.m_nanoseconds)
return *this + Time((i64) ~(u64)other.m_seconds, 1'000'000'000 - other.m_nanoseconds);
return *this + Duration((i64) ~(u64)other.m_seconds, 1'000'000'000 - other.m_nanoseconds);
if (other.m_seconds != (i64)-0x8000'0000'0000'0000)
return *this + Time(-other.m_seconds, 0);
return *this + Duration(-other.m_seconds, 0);
// Only remaining case: We want to subtract -0x8000'0000'0000'0000 seconds,
// i.e. add a very large number.
if (m_seconds >= 0)
return Time::max();
return Time { (m_seconds + 0x4000'0000'0000'0000) + 0x4000'0000'0000'0000, m_nanoseconds };
return Duration::max();
return Duration { (m_seconds + 0x4000'0000'0000'0000) + 0x4000'0000'0000'0000, m_nanoseconds };
}
Time& Time::operator-=(Time const& other)
Duration& Duration::operator-=(Duration const& other)
{
*this = *this - other;
return *this;
}
Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
Duration Duration::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
{
VERIFY(nanoseconds < 1'000'000'000);
@ -269,41 +269,41 @@ Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
// Now the only possible way to become invalid is overflowing i64 towards positive infinity:
if (Checked<i64>::addition_would_overflow<i64, i64>(seconds, extra_seconds)) {
if (seconds < 0) {
return Time::min();
return Duration::min();
} else {
return Time::max();
return Duration::max();
}
}
return Time { seconds + extra_seconds, nanoseconds };
return Duration { seconds + extra_seconds, nanoseconds };
}
#ifndef KERNEL
namespace {
static Time now_time_from_clock(clockid_t clock_id)
static Duration now_time_from_clock(clockid_t clock_id)
{
timespec now_spec {};
::clock_gettime(clock_id, &now_spec);
return Time::from_timespec(now_spec);
return Duration::from_timespec(now_spec);
}
}
Time Time::now_realtime()
Duration Duration::now_realtime()
{
return now_time_from_clock(CLOCK_REALTIME);
}
Time Time::now_realtime_coarse()
Duration Duration::now_realtime_coarse()
{
return now_time_from_clock(CLOCK_REALTIME_COARSE);
}
Time Time::now_monotonic()
Duration Duration::now_monotonic()
{
return now_time_from_clock(CLOCK_MONOTONIC);
}
Time Time::now_monotonic_coarse()
Duration Duration::now_monotonic_coarse()
{
return now_time_from_clock(CLOCK_MONOTONIC_COARSE);
}

View file

@ -130,26 +130,28 @@ constexpr i64 seconds_since_epoch_to_year(i64 seconds)
return 1970 + round_down(years_since_epoch);
}
/*
* Represents a time amount in a "safe" way.
* Minimum: 0 seconds, 0 nanoseconds
* Maximum: 2**63-1 seconds, 999'999'999 nanoseconds
* If any operation (e.g. 'from_timeval' or operator-) would over- or underflow, the closest legal value is returned instead.
* Inputs (e.g. to 'from_timespec') are allowed to be in non-normal form (e.g. "1 second, 2'012'345'678 nanoseconds" or "1 second, -2 microseconds").
* Outputs (e.g. from 'to_timeval') are always in normal form.
*/
class Time {
// Represents a duration in a "safe" way.
// Minimum: -(2**63) seconds, 0 nanoseconds
// Maximum: 2**63-1 seconds, 999'999'999 nanoseconds
// If any operation (e.g. 'from_timeval' or operator-) would over- or underflow, the closest legal value is returned instead.
// Inputs (e.g. to 'from_timespec') are allowed to be in non-normal form (e.g. "1 second, 2'012'345'678 nanoseconds" or "1 second, -2 microseconds").
// Outputs (e.g. from 'to_timeval') are always in normal form.
//
// NOTE: This class is naive. It may represent either absolute offsets or relative durations. It does not have a reference point in itself,
// and therefore comparing multiple instances of this class is only sensible if you are sure that their reference point is identical.
// You should not be using this class directly to represent absolute time.
class Duration {
public:
Time() = default;
Time(Time const&) = default;
Time& operator=(Time const&) = default;
Duration() = default;
Duration(Duration const&) = default;
Duration& operator=(Duration const&) = default;
Time(Time&& other)
Duration(Duration&& other)
: m_seconds(exchange(other.m_seconds, 0))
, m_nanoseconds(exchange(other.m_nanoseconds, 0))
{
}
Time& operator=(Time&& other)
Duration& operator=(Duration&& other)
{
if (this != &other) {
m_seconds = exchange(other.m_seconds, 0);
@ -188,7 +190,7 @@ private:
}
public:
[[nodiscard]] constexpr static Time from_timestamp(i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond)
[[nodiscard]] constexpr static Duration from_timestamp(i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond)
{
constexpr auto milliseconds_per_day = 86'400'000;
constexpr auto milliseconds_per_hour = 3'600'000;
@ -206,35 +208,35 @@ public:
return from_milliseconds(milliseconds_since_epoch);
}
[[nodiscard]] constexpr static Time from_seconds(i64 seconds) { return Time(seconds, 0); }
[[nodiscard]] constexpr static Time from_nanoseconds(i64 nanoseconds)
[[nodiscard]] constexpr static Duration from_seconds(i64 seconds) { return Duration(seconds, 0); }
[[nodiscard]] constexpr static Duration from_nanoseconds(i64 nanoseconds)
{
i64 seconds = sane_mod(nanoseconds, 1'000'000'000);
return Time(seconds, nanoseconds);
return Duration(seconds, nanoseconds);
}
[[nodiscard]] constexpr static Time from_microseconds(i64 microseconds)
[[nodiscard]] constexpr static Duration from_microseconds(i64 microseconds)
{
i64 seconds = sane_mod(microseconds, 1'000'000);
return Time(seconds, microseconds * 1'000);
return Duration(seconds, microseconds * 1'000);
}
[[nodiscard]] constexpr static Time from_milliseconds(i64 milliseconds)
[[nodiscard]] constexpr static Duration from_milliseconds(i64 milliseconds)
{
i64 seconds = sane_mod(milliseconds, 1'000);
return Time(seconds, milliseconds * 1'000'000);
return Duration(seconds, milliseconds * 1'000'000);
}
[[nodiscard]] static Time from_ticks(clock_t, time_t);
[[nodiscard]] static Time from_timespec(const struct timespec&);
[[nodiscard]] static Time from_timeval(const struct timeval&);
[[nodiscard]] static Duration from_ticks(clock_t, time_t);
[[nodiscard]] static Duration from_timespec(const struct timespec&);
[[nodiscard]] static Duration from_timeval(const struct timeval&);
// We don't pull in <stdint.h> for the pretty min/max definitions because this file is also included in the Kernel
[[nodiscard]] constexpr static Time min() { return Time(-__INT64_MAX__ - 1LL, 0); };
[[nodiscard]] constexpr static Time zero() { return Time(0, 0); };
[[nodiscard]] constexpr static Time max() { return Time(__INT64_MAX__, 999'999'999); };
[[nodiscard]] constexpr static Duration min() { return Duration(-__INT64_MAX__ - 1LL, 0); };
[[nodiscard]] constexpr static Duration zero() { return Duration(0, 0); };
[[nodiscard]] constexpr static Duration max() { return Duration(__INT64_MAX__, 999'999'999); };
#ifndef KERNEL
[[nodiscard]] static Time now_realtime();
[[nodiscard]] static Time now_realtime_coarse();
[[nodiscard]] static Time now_monotonic();
[[nodiscard]] static Time now_monotonic_coarse();
[[nodiscard]] static Duration now_realtime();
[[nodiscard]] static Duration now_realtime_coarse();
[[nodiscard]] static Duration now_monotonic();
[[nodiscard]] static Duration now_monotonic_coarse();
#endif
// Truncates towards zero (2.8s to 2s, -2.8s to -2s).
@ -253,13 +255,13 @@ public:
[[nodiscard]] bool is_zero() const { return (m_seconds == 0) && (m_nanoseconds == 0); }
[[nodiscard]] bool is_negative() const { return m_seconds < 0; }
Time operator+(Time const& other) const;
Time& operator+=(Time const& other);
Time operator-(Time const& other) const;
Time& operator-=(Time const& other);
Duration operator+(Duration const& other) const;
Duration& operator+=(Duration const& other);
Duration operator-(Duration const& other) const;
Duration& operator-=(Duration const& other);
constexpr bool operator==(Time const& other) const = default;
constexpr int operator<=>(Time const& other) const
constexpr bool operator==(Duration const& other) const = default;
constexpr int operator<=>(Duration const& other) const
{
if (int cmp = (m_seconds > other.m_seconds ? 1 : m_seconds < other.m_seconds ? -1
: 0);
@ -273,13 +275,13 @@ public:
}
private:
constexpr explicit Time(i64 seconds, u32 nanoseconds)
constexpr explicit Duration(i64 seconds, u32 nanoseconds)
: m_seconds(seconds)
, m_nanoseconds(nanoseconds)
{
}
[[nodiscard]] static Time from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds);
[[nodiscard]] static Duration from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds);
i64 m_seconds { 0 };
u32 m_nanoseconds { 0 }; // Always less than 1'000'000'000
@ -357,10 +359,10 @@ inline void timespec_to_timeval(TimespecType const& ts, TimevalType& tv)
// To use these, add a ``using namespace AK::TimeLiterals`` at block or file scope
namespace TimeLiterals {
constexpr Time operator""_ns(unsigned long long nanoseconds) { return Time::from_nanoseconds(static_cast<i64>(nanoseconds)); }
constexpr Time operator""_us(unsigned long long microseconds) { return Time::from_microseconds(static_cast<i64>(microseconds)); }
constexpr Time operator""_ms(unsigned long long milliseconds) { return Time::from_milliseconds(static_cast<i64>(milliseconds)); }
constexpr Time operator""_sec(unsigned long long seconds) { return Time::from_seconds(static_cast<i64>(seconds)); }
constexpr Duration operator""_ns(unsigned long long nanoseconds) { return Duration::from_nanoseconds(static_cast<i64>(nanoseconds)); }
constexpr Duration operator""_us(unsigned long long microseconds) { return Duration::from_microseconds(static_cast<i64>(microseconds)); }
constexpr Duration operator""_ms(unsigned long long milliseconds) { return Duration::from_milliseconds(static_cast<i64>(milliseconds)); }
constexpr Duration operator""_sec(unsigned long long seconds) { return Duration::from_seconds(static_cast<i64>(seconds)); }
}
@ -372,9 +374,9 @@ using AK::day_of_year;
using AK::days_in_month;
using AK::days_in_year;
using AK::days_since_epoch;
using AK::Duration;
using AK::is_leap_year;
using AK::seconds_since_epoch_to_year;
using AK::Time;
using AK::timespec_add;
using AK::timespec_add_timeval;
using AK::timespec_sub;