From 213025f210a785ca71b7659d7dc4f5dc3c030622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 13 Mar 2023 16:30:34 +0100 Subject: [PATCH] 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. --- AK/DOSPackedTime.cpp | 6 +- AK/DOSPackedTime.h | 2 +- AK/Forward.h | 4 +- AK/Time.cpp | 74 ++--- AK/Time.h | 92 +++--- Kernel/Arch/x86_64/RTC.cpp | 4 +- Kernel/Arch/x86_64/RTC.h | 3 +- Kernel/Bus/USB/UHCI/UHCIController.cpp | 4 +- Kernel/Devices/AsyncDeviceRequest.cpp | 2 +- Kernel/Devices/AsyncDeviceRequest.h | 2 +- Kernel/Devices/Audio/IntelHDA/Stream.cpp | 2 +- Kernel/FileSystem/DevPtsFS/Inode.cpp | 2 +- Kernel/FileSystem/Ext2FS/Inode.cpp | 10 +- Kernel/FileSystem/Ext2FS/Inode.h | 2 +- Kernel/FileSystem/ISO9660FS/Inode.cpp | 4 +- Kernel/FileSystem/ISO9660FS/Inode.h | 2 +- Kernel/FileSystem/Inode.cpp | 2 +- Kernel/FileSystem/Inode.h | 2 +- Kernel/FileSystem/InodeMetadata.h | 8 +- Kernel/FileSystem/ProcFS/Inode.h | 2 +- Kernel/FileSystem/RAMFS/Inode.cpp | 2 +- Kernel/FileSystem/RAMFS/Inode.h | 2 +- Kernel/FileSystem/SysFS/Inode.cpp | 2 +- Kernel/FileSystem/SysFS/Inode.h | 2 +- Kernel/FileSystem/VirtualFileSystem.cpp | 6 +- Kernel/Graphics/VMWare/Console.cpp | 2 +- Kernel/Graphics/VirtIOGPU/Console.cpp | 2 +- Kernel/Net/IPv4Socket.cpp | 6 +- Kernel/Net/IPv4Socket.h | 8 +- Kernel/Net/LocalSocket.cpp | 2 +- Kernel/Net/LocalSocket.h | 2 +- Kernel/Net/NetworkAdapter.cpp | 2 +- Kernel/Net/NetworkAdapter.h | 6 +- Kernel/Net/NetworkTask.cpp | 22 +- Kernel/Net/Socket.cpp | 2 +- Kernel/Net/Socket.h | 14 +- Kernel/Net/TCPSocket.cpp | 4 +- Kernel/Net/TCPSocket.h | 4 +- Kernel/PerformanceManager.h | 6 +- Kernel/Process.cpp | 2 +- Kernel/Process.h | 2 +- Kernel/StdLib.cpp | 16 +- Kernel/StdLib.h | 6 +- Kernel/Storage/NVMe/NVMeController.cpp | 2 +- Kernel/Storage/NVMe/NVMeController.h | 2 +- Kernel/Syscalls/alarm.cpp | 4 +- Kernel/Syscalls/beep.cpp | 2 +- Kernel/Syscalls/clock.cpp | 4 +- Kernel/Syscalls/resource.cpp | 8 +- Kernel/Syscalls/socket.cpp | 2 +- Kernel/Tasks/SyncTask.cpp | 2 +- Kernel/Thread.cpp | 4 +- Kernel/Thread.h | 22 +- Kernel/ThreadBlockers.cpp | 10 +- Kernel/Time/TimeManagement.cpp | 34 +-- Kernel/Time/TimeManagement.h | 22 +- Kernel/TimerQueue.cpp | 8 +- Kernel/TimerQueue.h | 14 +- .../LibTimeZone/GenerateTimeZoneData.cpp | 24 +- ...0001-Add-SerenityOS-platform-support.patch | 2 +- Tests/AK/CMakeLists.txt | 2 +- Tests/AK/{TestTime.cpp => TestDuration.cpp} | 262 +++++++++--------- Tests/Kernel/TestKernelAlarm.cpp | 8 +- Tests/LibLocale/TestDateTimeFormat.cpp | 10 +- Tests/LibTimeZone/TestTimeZone.cpp | 4 +- Userland/Applications/3DFileViewer/main.cpp | 2 +- Userland/Applications/Browser/CookieJar.cpp | 10 +- .../Applications/Browser/StorageWidget.cpp | 2 +- .../ClockSettings/TimeZoneSettingsWidget.cpp | 2 +- .../Applications/HexEditor/HexDocument.cpp | 2 +- Userland/Applications/HexEditor/HexDocument.h | 6 +- .../Applications/Piano/AudioPlayerLoop.cpp | 2 +- .../Applications/ThemeEditor/MainWidget.cpp | 4 +- .../Applications/ThemeEditor/MainWidget.h | 2 +- .../VideoPlayer/VideoPlayerWidget.cpp | 8 +- .../VideoPlayer/VideoPlayerWidget.h | 4 +- .../DevTools/HackStudio/LanguageClient.cpp | 2 +- Userland/Games/BrickGame/BrickGame.cpp | 36 +-- .../Libraries/LibAudio/ConnectionToServer.cpp | 2 +- Userland/Libraries/LibC/time.cpp | 8 +- Userland/Libraries/LibCore/ElapsedTimer.cpp | 6 +- Userland/Libraries/LibCore/ElapsedTimer.h | 6 +- .../LibCore/EventLoopImplementationUnix.cpp | 26 +- .../LibCore/EventLoopImplementationUnix.h | 2 +- .../Libraries/LibCore/FileWatcherMacOS.mm | 5 +- Userland/Libraries/LibCore/Socket.cpp | 6 +- Userland/Libraries/LibCore/Socket.h | 6 +- Userland/Libraries/LibDesktop/Screensaver.cpp | 2 +- Userland/Libraries/LibDesktop/Screensaver.h | 4 +- Userland/Libraries/LibGUI/MessageBox.cpp | 6 +- Userland/Libraries/LibGUI/MessageBox.h | 4 +- Userland/Libraries/LibGUI/TextDocument.cpp | 4 +- Userland/Libraries/LibGUI/TextDocument.h | 6 +- Userland/Libraries/LibGUI/UndoStack.cpp | 2 +- Userland/Libraries/LibGUI/UndoStack.h | 4 +- Userland/Libraries/LibIPC/Decoder.cpp | 4 +- Userland/Libraries/LibIPC/Decoder.h | 2 +- Userland/Libraries/LibIPC/Encoder.cpp | 2 +- Userland/Libraries/LibIPC/Encoder.h | 2 +- .../Libraries/LibJS/Bytecode/PassManager.h | 2 +- .../LibJS/Contrib/Test262/AgentObject.cpp | 2 +- Userland/Libraries/LibJS/Heap/Heap.cpp | 2 +- Userland/Libraries/LibJS/Runtime/Date.cpp | 8 +- .../LibJS/Runtime/DateConstructor.cpp | 6 +- .../Libraries/LibJS/Runtime/DatePrototype.cpp | 2 +- .../LibJS/Runtime/Intl/DateTimeFormat.h | 4 +- .../Libraries/LibJS/Runtime/Temporal/Now.cpp | 4 +- .../Libraries/LibLocale/DateTimeFormat.cpp | 2 +- Userland/Libraries/LibLocale/DateTimeFormat.h | 2 +- Userland/Libraries/LibTimeZone/TimeZone.cpp | 8 +- Userland/Libraries/LibTimeZone/TimeZone.h | 8 +- .../Libraries/LibVideo/Containers/Demuxer.h | 4 +- .../LibVideo/Containers/Matroska/Document.h | 22 +- .../Containers/Matroska/MatroskaDemuxer.cpp | 6 +- .../Containers/Matroska/MatroskaDemuxer.h | 4 +- .../LibVideo/Containers/Matroska/Reader.cpp | 18 +- .../LibVideo/Containers/Matroska/Reader.h | 8 +- .../Libraries/LibVideo/PlaybackManager.cpp | 36 +-- Userland/Libraries/LibVideo/PlaybackManager.h | 30 +- Userland/Libraries/LibVideo/Sample.h | 6 +- Userland/Libraries/LibVideo/Track.h | 2 +- Userland/Libraries/LibWeb/Cookie/Cookie.cpp | 8 +- Userland/Libraries/LibWeb/Cookie/Cookie.h | 6 +- .../Libraries/LibWeb/Cookie/ParsedCookie.cpp | 14 +- .../Libraries/LibWeb/Cookie/ParsedCookie.h | 4 +- Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- Userland/Libraries/LibWeb/DOM/Document.h | 2 +- Userland/Libraries/LibWeb/FileAPI/File.cpp | 2 +- .../LibWeb/HTML/HTMLMediaElement.cpp | 4 +- .../Libraries/LibWeb/HTML/HTMLMediaElement.h | 2 +- .../LibWeb/HTML/HTMLVideoElement.cpp | 2 +- Userland/Libraries/LibWeb/HTML/VideoTrack.cpp | 6 +- Userland/Libraries/LibWeb/HTML/VideoTrack.h | 6 +- .../LibWeb/HighResolutionTime/TimeOrigin.cpp | 2 +- .../Libraries/LibWeb/Loader/LoadRequest.h | 2 +- .../LibWeb/WebDriver/ExecuteScript.cpp | 4 +- .../Services/LookupServer/LookupServer.cpp | 2 +- .../WebContent/WebDriverConnection.cpp | 8 +- Userland/Utilities/disk_benchmark.cpp | 2 +- Userland/Utilities/touch.cpp | 4 +- 140 files changed, 634 insertions(+), 628 deletions(-) rename Tests/AK/{TestTime.cpp => TestDuration.cpp} (59%) diff --git a/AK/DOSPackedTime.cpp b/AK/DOSPackedTime.cpp index 28d869f444b..f964db14d2f 100644 --- a/AK/DOSPackedTime.cpp +++ b/AK/DOSPackedTime.cpp @@ -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) diff --git a/AK/DOSPackedTime.h b/AK/DOSPackedTime.h index 3dd46aa10e4..dfce4114d8c 100644 --- a/AK/DOSPackedTime.h +++ b/AK/DOSPackedTime.h @@ -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); diff --git a/AK/Forward.h b/AK/Forward.h index 42f9d3a8c0f..a11fe2c6f27 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -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; diff --git a/AK/Time.cpp b/AK/Time.cpp index f91dd358dd1..369226f3dfa 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -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 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 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 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 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 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(m_seconds), static_cast(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(m_seconds), static_cast(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::addition_would_overflow(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); } diff --git a/AK/Time.h b/AK/Time.h index 8a2382522a1..2c1af843137 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -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 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(nanoseconds)); } -constexpr Time operator""_us(unsigned long long microseconds) { return Time::from_microseconds(static_cast(microseconds)); } -constexpr Time operator""_ms(unsigned long long milliseconds) { return Time::from_milliseconds(static_cast(milliseconds)); } -constexpr Time operator""_sec(unsigned long long seconds) { return Time::from_seconds(static_cast(seconds)); } +constexpr Duration operator""_ns(unsigned long long nanoseconds) { return Duration::from_nanoseconds(static_cast(nanoseconds)); } +constexpr Duration operator""_us(unsigned long long microseconds) { return Duration::from_microseconds(static_cast(microseconds)); } +constexpr Duration operator""_ms(unsigned long long milliseconds) { return Duration::from_milliseconds(static_cast(milliseconds)); } +constexpr Duration operator""_sec(unsigned long long seconds) { return Duration::from_seconds(static_cast(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; diff --git a/Kernel/Arch/x86_64/RTC.cpp b/Kernel/Arch/x86_64/RTC.cpp index e7a8cd48f82..d932e5b422a 100644 --- a/Kernel/Arch/x86_64/RTC.cpp +++ b/Kernel/Arch/x86_64/RTC.cpp @@ -19,9 +19,9 @@ void initialize() s_boot_time = now(); } -Time boot_time() +Duration boot_time() { - return Time::from_timespec({ s_boot_time, 0 }); + return Duration::from_timespec({ s_boot_time, 0 }); } static bool update_in_progress() diff --git a/Kernel/Arch/x86_64/RTC.h b/Kernel/Arch/x86_64/RTC.h index 80e49ff1e77..859baa82d7d 100644 --- a/Kernel/Arch/x86_64/RTC.h +++ b/Kernel/Arch/x86_64/RTC.h @@ -6,12 +6,13 @@ #pragma once +#include #include namespace Kernel::RTC { void initialize(); time_t now(); -Time boot_time(); +Duration boot_time(); } diff --git a/Kernel/Bus/USB/UHCI/UHCIController.cpp b/Kernel/Bus/USB/UHCI/UHCIController.cpp index a2411ee5268..ce373e7acb2 100644 --- a/Kernel/Bus/USB/UHCI/UHCIController.cpp +++ b/Kernel/Bus/USB/UHCI/UHCIController.cpp @@ -590,7 +590,7 @@ ErrorOr UHCIController::spawn_port_process() if (m_root_hub) m_root_hub->check_for_port_updates(); - (void)Thread::current()->sleep(Time::from_seconds(1)); + (void)Thread::current()->sleep(Duration::from_seconds(1)); } })); return {}; @@ -616,7 +616,7 @@ ErrorOr UHCIController::spawn_async_poll_process() } } } - (void)Thread::current()->sleep(Time::from_milliseconds(poll_interval_ms)); + (void)Thread::current()->sleep(Duration::from_milliseconds(poll_interval_ms)); } })); return {}; diff --git a/Kernel/Devices/AsyncDeviceRequest.cpp b/Kernel/Devices/AsyncDeviceRequest.cpp index 2a70a28b284..a45f70dea03 100644 --- a/Kernel/Devices/AsyncDeviceRequest.cpp +++ b/Kernel/Devices/AsyncDeviceRequest.cpp @@ -51,7 +51,7 @@ void AsyncDeviceRequest::request_finished() m_queue.wake_all(); } -auto AsyncDeviceRequest::wait(Time* timeout) -> RequestWaitResult +auto AsyncDeviceRequest::wait(Duration* timeout) -> RequestWaitResult { VERIFY(!m_parent_request); auto request_result = get_request_result(); diff --git a/Kernel/Devices/AsyncDeviceRequest.h b/Kernel/Devices/AsyncDeviceRequest.h index 350ded3d11a..8e9d57610c8 100644 --- a/Kernel/Devices/AsyncDeviceRequest.h +++ b/Kernel/Devices/AsyncDeviceRequest.h @@ -60,7 +60,7 @@ public: void add_sub_request(NonnullLockRefPtr); - [[nodiscard]] RequestWaitResult wait(Time* = nullptr); + [[nodiscard]] RequestWaitResult wait(Duration* = nullptr); void do_start(SpinlockLocker>&& requests_lock) { diff --git a/Kernel/Devices/Audio/IntelHDA/Stream.cpp b/Kernel/Devices/Audio/IntelHDA/Stream.cpp index 52f8a886aed..d7eb1ee7e7f 100644 --- a/Kernel/Devices/Audio/IntelHDA/Stream.cpp +++ b/Kernel/Devices/Audio/IntelHDA/Stream.cpp @@ -192,7 +192,7 @@ ErrorOr OutputStream::write(UserOrKernelBuffer const& data, size_t lengt dbgln_if(INTEL_HDA_DEBUG, "IntelHDA: Waiting {} µs until buffer {} becomes writeable", microseconds_to_wait, buffer_index); // NOTE: we don't care about the reason for interruption - we simply calculate the next delay - [[maybe_unused]] auto block_result = Thread::current()->sleep(Time::from_microseconds(microseconds_to_wait)); + [[maybe_unused]] auto block_result = Thread::current()->sleep(Duration::from_microseconds(microseconds_to_wait)); } }; diff --git a/Kernel/FileSystem/DevPtsFS/Inode.cpp b/Kernel/FileSystem/DevPtsFS/Inode.cpp index 92f3de94b6e..419d73c5bc9 100644 --- a/Kernel/FileSystem/DevPtsFS/Inode.cpp +++ b/Kernel/FileSystem/DevPtsFS/Inode.cpp @@ -38,7 +38,7 @@ InodeMetadata DevPtsFSInode::metadata() const { if (auto pty = m_pty.strong_ref()) { auto metadata = m_metadata; - metadata.mtime = Time::from_timespec({ pty->time_of_last_write(), 0 }); + metadata.mtime = Duration::from_timespec({ pty->time_of_last_write(), 0 }); return metadata; } return m_metadata; diff --git a/Kernel/FileSystem/Ext2FS/Inode.cpp b/Kernel/FileSystem/Ext2FS/Inode.cpp index 3698cc64679..dfd50f124d3 100644 --- a/Kernel/FileSystem/Ext2FS/Inode.cpp +++ b/Kernel/FileSystem/Ext2FS/Inode.cpp @@ -474,10 +474,10 @@ InodeMetadata Ext2FSInode::metadata() const metadata.uid = m_raw_inode.i_uid; metadata.gid = m_raw_inode.i_gid; metadata.link_count = m_raw_inode.i_links_count; - metadata.atime = Time::from_timespec({ m_raw_inode.i_atime, 0 }); - metadata.ctime = Time::from_timespec({ m_raw_inode.i_ctime, 0 }); - metadata.mtime = Time::from_timespec({ m_raw_inode.i_mtime, 0 }); - metadata.dtime = Time::from_timespec({ m_raw_inode.i_dtime, 0 }); + metadata.atime = Duration::from_timespec({ m_raw_inode.i_atime, 0 }); + metadata.ctime = Duration::from_timespec({ m_raw_inode.i_ctime, 0 }); + metadata.mtime = Duration::from_timespec({ m_raw_inode.i_mtime, 0 }); + metadata.dtime = Duration::from_timespec({ m_raw_inode.i_dtime, 0 }); metadata.block_size = fs().block_size(); metadata.block_count = m_raw_inode.i_blocks; @@ -985,7 +985,7 @@ ErrorOr> Ext2FSInode::lookup(StringView name) return fs().get_inode({ fsid(), inode_index }); } -ErrorOr Ext2FSInode::update_timestamps(Optional