Skip to content

Truly independent web browser

AK: Round time points down to whole seconds and milliseconds

Shannon Boothe69a4b8e0952
diff --git a/AK/Format.cpp b/AK/Format.cpp
index f875f6924c5..7d0c408a7cd 100644
--- a/AK/Format.cpp
+++ b/AK/Format.cpp
@@ -1230,7 +1230,7 @@ void vdbg(StringView fmtstr, TypeErasedFormatParams& params, bool newline)
if (!process_name.is_empty()) {
bool const colorize = ak_colorize_output();
auto time = MonotonicTime::now_coarse();
- builder.appendff("{}.{:03} ", time.truncated_seconds(), time.nanoseconds_within_second() / 1000000);
+ builder.appendff("{}.{:03} ", time.seconds(), time.nanoseconds_within_second() / 1000000);
if (colorize)
builder.append(BOLD_YELLOW_FORMAT ""sv);
builder.append(process_name);
diff --git a/AK/Time.cpp b/AK/Time.cpp
index f7029c3e9cb..4c8e4864c96 100644
--- a/AK/Time.cpp
+++ b/AK/Time.cpp
@@ -193,6 +193,26 @@ i64 Duration::to_truncated_microseconds() const
return m_seconds < 0 ? NumericLimits<i64>::min() : NumericLimits<i64>::max();
}
+i64 Duration::to_floored_seconds() const
+{
+ VERIFY(m_nanoseconds < 1'000'000'000);
+ return m_seconds;
+}
+
+i64 Duration::to_floored_milliseconds() const
+{
+ VERIFY(m_nanoseconds < 1'000'000'000);
+ // Drop one second from negative durations so that the multiplication can't overflow before the remainder is added.
+ Checked<i64> milliseconds((m_seconds < 0) ? m_seconds + 1 : m_seconds);
+ milliseconds *= 1'000;
+ milliseconds += m_nanoseconds / 1'000'000;
+ if (m_seconds < 0)
+ milliseconds -= 1'000;
+ if (!milliseconds.has_overflow())
+ return milliseconds.value();
+ return m_seconds < 0 ? NumericLimits<i64>::min() : NumericLimits<i64>::max();
+}
+
i64 Duration::to_seconds() const
{
VERIFY(m_nanoseconds < 1'000'000'000);
diff --git a/AK/Time.h b/AK/Time.h
index 417f4dcd7bb..21416b463f5 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -253,6 +253,9 @@ public:
[[nodiscard]] i64 to_truncated_seconds() const;
[[nodiscard]] i64 to_truncated_milliseconds() const;
[[nodiscard]] i64 to_truncated_microseconds() const;
+ // Rounds towards -inf (2.8s to 2s, -2.3s to -3s).
+ [[nodiscard]] i64 to_floored_seconds() const;
+ [[nodiscard]] i64 to_floored_milliseconds() const;
// Rounds away from zero (2.3s to 3s, -2.3s to -3s).
[[nodiscard]] i64 to_seconds() const;
[[nodiscard]] f64 to_seconds_f64() const;
@@ -467,12 +470,10 @@ public:
[[nodiscard]] constexpr static UnixDateTime latest() { return UnixDateTime { Duration::max() }; }
[[nodiscard]] constexpr Duration offset_to_epoch() const { return m_offset; }
- // May return an epoch offset *after* what this UnixDateTime contains, because rounding to seconds occurs.
- [[nodiscard]] i64 seconds_since_epoch() const { return m_offset.to_seconds(); }
- [[nodiscard]] i64 milliseconds_since_epoch() const { return m_offset.to_milliseconds(); }
+ // Never returns a point after this UnixDateTime, since fractions are rounded towards -inf.
+ [[nodiscard]] i64 seconds_since_epoch() const { return m_offset.to_floored_seconds(); }
+ [[nodiscard]] i64 milliseconds_since_epoch() const { return m_offset.to_floored_milliseconds(); }
[[nodiscard]] i64 nanoseconds_since_epoch() const { return m_offset.to_nanoseconds(); }
- // Never returns a point after this UnixDateTime, since fractional seconds are cut off.
- [[nodiscard]] i64 truncated_seconds_since_epoch() const { return m_offset.to_truncated_seconds(); }
enum class LocalTime {
Yes,
@@ -563,11 +564,10 @@ public:
[[nodiscard]] static MonotonicTime now();
[[nodiscard]] static MonotonicTime now_coarse();
- [[nodiscard]] i64 seconds() const { return m_offset.to_seconds(); }
- [[nodiscard]] i64 milliseconds() const { return m_offset.to_milliseconds(); }
+ // Never returns a point in the future, since fractions are rounded towards -inf.
+ [[nodiscard]] i64 seconds() const { return m_offset.to_floored_seconds(); }
+ [[nodiscard]] i64 milliseconds() const { return m_offset.to_floored_milliseconds(); }
[[nodiscard]] i64 nanoseconds() const { return m_offset.to_nanoseconds(); }
- // Never returns a point in the future, since fractional seconds are cut off.
- [[nodiscard]] i64 truncated_seconds() const { return m_offset.to_truncated_seconds(); }
[[nodiscard]] i64 nanoseconds_within_second() const { return to_timespec().tv_nsec; }
constexpr bool operator==(MonotonicTime const& other) const { return this->m_offset == other.m_offset; }
diff --git a/Libraries/LibJS/Rust/src/contrib/test262/agent_object.rs b/Libraries/LibJS/Rust/src/contrib/test262/agent_object.rs
index 08249e349b0..394954d2dca 100644
--- a/Libraries/LibJS/Rust/src/contrib/test262/agent_object.rs
+++ b/Libraries/LibJS/Rust/src/contrib/test262/agent_object.rs
@@ -249,8 +249,7 @@ fn sleep_milliseconds(milliseconds: u32) {
std::thread::sleep(core::time::Duration::from_millis(u64::from(milliseconds)));
}
-/// MonotonicTime::now().milliseconds(): CLOCK_MONOTONIC, with the milliseconds rounded up as AK's
-/// Duration::to_milliseconds() rounds them.
+/// MonotonicTime::now().milliseconds(): CLOCK_MONOTONIC, with the milliseconds rounded down.
#[cfg(unix)]
fn monotonic_time_now_in_milliseconds() -> i64 {
let mut time = libc::timespec { tv_sec: 0, tv_nsec: 0 };
@@ -258,18 +257,14 @@ fn monotonic_time_now_in_milliseconds() -> i64 {
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &raw mut time) };
let seconds: i64 = time.tv_sec;
let nanoseconds: i64 = time.tv_nsec;
- let mut milliseconds = seconds * 1000 + nanoseconds / 1_000_000;
- if nanoseconds % 1_000_000 != 0 {
- milliseconds += 1;
- }
- milliseconds
+ seconds * 1000 + nanoseconds / 1_000_000
}
-/// MonotonicTime::now().milliseconds() on Windows, rounded up the same way, but counted from the first call rather than
-/// from boot, which makes no difference to scripts, as they can only compare the times they read.
+/// MonotonicTime::now().milliseconds() on Windows, rounded down the same way, but counted from the first call rather
+/// than from boot, which makes no difference to scripts, as they can only compare the times they read.
#[cfg(windows)]
fn monotonic_time_now_in_milliseconds() -> i64 {
static FIRST_CALL: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
let elapsed = FIRST_CALL.get_or_init(std::time::Instant::now).elapsed();
- i64::try_from(elapsed.as_nanos().div_ceil(1_000_000)).unwrap_or(i64::MAX)
+ i64::try_from(elapsed.as_millis()).unwrap_or(i64::MAX)
}
diff --git a/Libraries/LibWebView/CrashReportStore.cpp b/Libraries/LibWebView/CrashReportStore.cpp
index 4edd0636a96..23cbc995e26 100644
--- a/Libraries/LibWebView/CrashReportStore.cpp
+++ b/Libraries/LibWebView/CrashReportStore.cpp
@@ -322,7 +322,7 @@ ErrorOr<ByteString> CrashReportStore::store_report(ProcessType process_type, Str
{
auto directory = TRY(open_report_directory(m_directory));
- auto seconds = static_cast<time_t>(crashed_at.truncated_seconds_since_epoch());
+ auto seconds = static_cast<time_t>(crashed_at.seconds_since_epoch());
tm utc_time {};
if (!gmtime_r(&seconds, &utc_time))
return Error::from_errno(errno);
diff --git a/Tests/AK/TestTime.cpp b/Tests/AK/TestTime.cpp
index 333fa6fbfdc..d71036abe6a 100644
--- a/Tests/AK/TestTime.cpp
+++ b/Tests/AK/TestTime.cpp
@@ -293,6 +293,29 @@ TEST_CASE(truncation)
#endif
}
+TEST_CASE(flooring)
+{
+ EXPECT_EQ(DURATION(2, 800'800'800).to_floored_seconds(), 2);
+ EXPECT_EQ(DURATION(2, 800'800'800).to_floored_milliseconds(), 2'800);
+ EXPECT_EQ(DURATION(-2, -800'800'800).to_floored_seconds(), -3);
+ EXPECT_EQ(DURATION(-2, -800'800'800).to_floored_milliseconds(), -2'801);
+ EXPECT_EQ(DURATION(0, -1).to_floored_seconds(), -1);
+ EXPECT_EQ(DURATION(0, -1).to_floored_milliseconds(), -1);
+
+ EXPECT_EQ(Duration::min().to_floored_seconds(), (i64)-0x8000'0000'0000'0000);
+ EXPECT_EQ(Duration::max().to_floored_seconds(), 0x7fff'ffff'ffff'ffff);
+ EXPECT_EQ(Duration::min().to_floored_milliseconds(), (i64)-0x8000'0000'0000'0000);
+ EXPECT_EQ(Duration::max().to_floored_milliseconds(), 0x7fff'ffff'ffff'ffff);
+ EXPECT_EQ(Duration::from_milliseconds(NumericLimits<i64>::min() + 1).to_floored_milliseconds(), NumericLimits<i64>::min() + 1);
+
+ auto after_epoch = UnixDateTime::from_nanoseconds_since_epoch(2'800'800'800);
+ EXPECT_EQ(after_epoch.seconds_since_epoch(), 2);
+ EXPECT_EQ(after_epoch.milliseconds_since_epoch(), 2'800);
+ auto before_epoch = UnixDateTime::from_nanoseconds_since_epoch(-2'800'800'800);
+ EXPECT_EQ(before_epoch.seconds_since_epoch(), -3);
+ EXPECT_EQ(before_epoch.milliseconds_since_epoch(), -2'801);
+}
+
TEST_CASE(is_negative)
{
auto small = Duration::from_nanoseconds(10);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/formdata/append.any.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/formdata/append.any.txt
new file mode 100644
index 00000000000..737234c613a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/formdata/append.any.txt
@@ -0,0 +1,12 @@
+Harness status: OK
+
+Found 7 tests
+
+7 Pass
+Pass testFormDataAppend1
+Pass testFormDataAppend2
+Pass testFormDataAppendUndefined1
+Pass testFormDataAppendUndefined2
+Pass testFormDataAppendNull1
+Pass testFormDataAppendNull2
+Pass testFormDataAppendEmptyBlob
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.html b/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.html
new file mode 100644
index 00000000000..a8ce30c1c5b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>FormData.append</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../xhr/formdata/append.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.js b/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.js
new file mode 100644
index 00000000000..6871f69df59
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/formdata/append.any.js
@@ -0,0 +1,37 @@
+// META: title=FormData.append
+
+ test(function() {
+ assert_equals(create_formdata(['key', 'value1']).get('key'), "value1");
+ }, 'testFormDataAppend1');
+ test(function() {
+ assert_equals(create_formdata(['key', 'value2'], ['key', 'value1']).get('key'), "value2");
+ }, 'testFormDataAppend2');
+ test(function() {
+ assert_equals(create_formdata(['key', undefined]).get('key'), "undefined");
+ }, 'testFormDataAppendUndefined1');
+ test(function() {
+ assert_equals(create_formdata(['key', undefined], ['key', 'value1']).get('key'), "undefined");
+ }, 'testFormDataAppendUndefined2');
+ test(function() {
+ assert_equals(create_formdata(['key', null]).get('key'), "null");
+ }, 'testFormDataAppendNull1');
+ test(function() {
+ assert_equals(create_formdata(['key', null], ['key', 'value1']).get('key'), "null");
+ }, 'testFormDataAppendNull2');
+ test(function() {
+ var before = new Date(new Date().getTime() - 2000).getTime(); // two seconds ago, in case there's clock drift
+ var fd = create_formdata(['key', new Blob(), 'blank.txt']).get('key');
+ assert_equals(fd.name, "blank.txt");
+ assert_equals(fd.type, "");
+ assert_equals(fd.size, 0);
+ assert_greater_than_equal(fd.lastModified, before);
+ assert_less_than_equal(fd.lastModified, new Date().getTime());
+ }, 'testFormDataAppendEmptyBlob');
+
+ function create_formdata() {
+ var fd = new FormData();
+ for (var i = 0; i < arguments.length; i++) {
+ fd.append.apply(fd, arguments[i]);
+ };
+ return fd;
+ }