From be09893fa754842553e4aad39657c8893df8c6cd Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 16 Nov 2024 12:43:27 -0500 Subject: [PATCH] AK+LibJS: Don't use Temporal for console.time() and console.timeLog() We don't need nanosecond precision here anyways, as we only display millisecond resolution. This uses our simple duration formatter from AK, which is updated to accept a Duration here. This method did not have any users after the move from Serenity. --- AK/NumberFormat.cpp | 20 ++++++++++++-------- AK/NumberFormat.h | 3 ++- Libraries/LibJS/Console.cpp | 35 +++-------------------------------- Libraries/LibJS/Console.h | 1 - 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/AK/NumberFormat.cpp b/AK/NumberFormat.cpp index 2027b22bf38..85d4ae208a2 100644 --- a/AK/NumberFormat.cpp +++ b/AK/NumberFormat.cpp @@ -73,16 +73,20 @@ String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_se return MUST(String::formatted("{} ({} bytes)", human_readable_size_string, size)); } -String human_readable_time(i64 time_in_seconds) +String human_readable_time(Duration duration) { - auto days = time_in_seconds / 86400; - time_in_seconds = time_in_seconds % 86400; + auto milliseconds = duration.to_milliseconds(); - auto hours = time_in_seconds / 3600; - time_in_seconds = time_in_seconds % 3600; + auto days = milliseconds / 86400000; + milliseconds = milliseconds % 86400000; - auto minutes = time_in_seconds / 60; - time_in_seconds = time_in_seconds % 60; + auto hours = milliseconds / 3600000; + milliseconds = milliseconds % 3600000; + + auto minutes = milliseconds / 60000; + milliseconds = milliseconds % 60000; + + auto seconds = static_cast(milliseconds) / 1000.0; StringBuilder builder; @@ -95,7 +99,7 @@ String human_readable_time(i64 time_in_seconds) if (minutes > 0) builder.appendff("{} minute{} ", minutes, minutes == 1 ? "" : "s"); - builder.appendff("{} second{}", time_in_seconds, time_in_seconds == 1 ? "" : "s"); + builder.appendff("{:.3} second{}", seconds, seconds == 1.0 ? "" : "s"); return MUST(builder.to_string()); } diff --git a/AK/NumberFormat.h b/AK/NumberFormat.h index ba99d0caf7c..c2809e7eeb4 100644 --- a/AK/NumberFormat.h +++ b/AK/NumberFormat.h @@ -7,6 +7,7 @@ #pragma once #include +#include namespace AK { @@ -24,7 +25,7 @@ String human_readable_size(u64 size, HumanReadableBasedOn based_on = HumanReadab String human_readable_quantity(u64 quantity, HumanReadableBasedOn based_on = HumanReadableBasedOn::Base2, StringView unit = "B"sv, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No); String human_readable_size_long(u64 size, UseThousandsSeparator use_thousands_separator = UseThousandsSeparator::No); -String human_readable_time(i64 time_in_seconds); +String human_readable_time(Duration); String human_readable_digital_time(i64 time_in_seconds); } diff --git a/Libraries/LibJS/Console.cpp b/Libraries/LibJS/Console.cpp index 5c93af23b92..6b81804d8ce 100644 --- a/Libraries/LibJS/Console.cpp +++ b/Libraries/LibJS/Console.cpp @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include namespace JS { @@ -604,7 +604,7 @@ ThrowCompletionOr Console::time_log() auto start_time = maybe_start_time->value; // 3. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format. - auto duration = TRY(format_time_since(start_time)); + auto duration = AK::human_readable_time(start_time.elapsed_time()); // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration)); @@ -653,7 +653,7 @@ ThrowCompletionOr Console::time_end() m_timer_table.remove(label); // 4. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format. - auto duration = TRY(format_time_since(start_time)); + auto duration = AK::human_readable_time(start_time.elapsed_time()); // 5. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration)); @@ -724,35 +724,6 @@ ThrowCompletionOr Console::value_vector_to_string(GC::MarkedVector Console::format_time_since(Core::ElapsedTimer timer) -{ - auto& vm = realm().vm(); - - auto elapsed_ms = timer.elapsed_time().to_milliseconds(); - auto duration = TRY(Temporal::balance_duration(vm, 0, 0, 0, 0, elapsed_ms, 0, "0"_sbigint, "year"sv)); - - auto append = [&](auto& builder, auto format, auto number) { - if (!builder.is_empty()) - builder.append(' '); - builder.appendff(format, number); - }; - - StringBuilder builder; - - if (duration.days > 0) - append(builder, "{:.0} day(s)"sv, duration.days); - if (duration.hours > 0) - append(builder, "{:.0} hour(s)"sv, duration.hours); - if (duration.minutes > 0) - append(builder, "{:.0} minute(s)"sv, duration.minutes); - if (duration.seconds > 0 || duration.milliseconds > 0) { - double combined_seconds = duration.seconds + (0.001 * duration.milliseconds); - append(builder, "{:.3} seconds"sv, combined_seconds); - } - - return MUST(builder.to_string()); -} - ConsoleClient::ConsoleClient(Console& console) : m_console(console) { diff --git a/Libraries/LibJS/Console.h b/Libraries/LibJS/Console.h index a0a6c4ad6da..5791aebb185 100644 --- a/Libraries/LibJS/Console.h +++ b/Libraries/LibJS/Console.h @@ -96,7 +96,6 @@ private: virtual void visit_edges(Visitor&) override; ThrowCompletionOr value_vector_to_string(GC::MarkedVector const&); - ThrowCompletionOr format_time_since(Core::ElapsedTimer timer); GC::Ref m_realm; GC::Ptr m_client;