Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibJS: Some prepatory commits to begin anew with Temporal #2387

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions AK/NumberFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
Expand Down
3 changes: 2 additions & 1 deletion AK/NumberFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <AK/String.h>
#include <AK/Time.h>

namespace AK {

Expand All @@ -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);

}
Expand Down
35 changes: 3 additions & 32 deletions Libraries/LibJS/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*/

#include <AK/MemoryStream.h>
#include <AK/NumberFormat.h>
#include <AK/StringBuilder.h>
#include <LibJS/Console.h>
#include <LibJS/Print.h>
#include <LibJS/Runtime/AbstractOperations.h>
#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 {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 0 additions & 1 deletion Libraries/LibJS/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Console : public Cell {
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;
Expand Down
Loading
Loading