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.
This commit is contained in:
Timothy Flynn 2024-11-16 12:43:27 -05:00 committed by Tim Flynn
parent 8bd394f349
commit be09893fa7
Notes: github-actions[bot] 2024-11-18 22:47:34 +00:00
4 changed files with 17 additions and 42 deletions

View file

@ -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<double>(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());
}

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/String.h>
#include <AK/Time.h>
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);
}

View file

@ -8,6 +8,7 @@
*/
#include <AK/MemoryStream.h>
#include <AK/NumberFormat.h>
#include <AK/StringBuilder.h>
#include <LibJS/Console.h>
#include <LibJS/Print.h>
@ -15,7 +16,6 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/StringConstructor.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/ValueInlines.h>
namespace JS {
@ -604,7 +604,7 @@ ThrowCompletionOr<Value> 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<Value> 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<String> Console::value_vector_to_string(GC::MarkedVector<Value
return MUST(builder.to_string());
}
ThrowCompletionOr<String> 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)
{

View file

@ -96,7 +96,6 @@ private:
virtual void visit_edges(Visitor&) override;
ThrowCompletionOr<String> value_vector_to_string(GC::MarkedVector<Value> const&);
ThrowCompletionOr<String> format_time_since(Core::ElapsedTimer timer);
GC::Ref<Realm> m_realm;
GC::Ptr<ConsoleClient> m_client;