LibWeb/HTML: Correctly convert number to time/local datetime string

This fixes a bug where non-zero milliseconds were previously not
included if the second component was zero.
This commit is contained in:
Glenn Skrzypczak 2025-08-11 08:03:32 +02:00 committed by Tim Flynn
commit ec807d40dd
Notes: github-actions[bot] 2025-08-14 15:06:42 +00:00
3 changed files with 10 additions and 14 deletions

View file

@ -2422,11 +2422,10 @@ static Utf16String convert_number_to_time_string(double input)
// string that represents the time that is input milliseconds after midnight on a day with no time changes. // string that represents the time that is input milliseconds after midnight on a day with no time changes.
auto seconds = JS::sec_from_time(input); auto seconds = JS::sec_from_time(input);
auto milliseconds = JS::ms_from_time(input); auto milliseconds = JS::ms_from_time(input);
if (seconds > 0) { if (milliseconds > 0)
if (milliseconds > 0) return Utf16String::formatted("{:02d}:{:02d}:{:02d}.{:03d}", JS::hour_from_time(input), JS::min_from_time(input), seconds, milliseconds);
return Utf16String::formatted("{:02d}:{:02d}:{:02d}.{:3d}", JS::hour_from_time(input), JS::min_from_time(input), seconds, milliseconds); if (seconds > 0)
return Utf16String::formatted("{:02d}:{:02d}:{:02d}", JS::hour_from_time(input), JS::min_from_time(input), seconds); return Utf16String::formatted("{:02d}:{:02d}:{:02d}", JS::hour_from_time(input), JS::min_from_time(input), seconds);
}
return Utf16String::formatted("{:02d}:{:02d}", JS::hour_from_time(input), JS::min_from_time(input)); return Utf16String::formatted("{:02d}:{:02d}", JS::hour_from_time(input), JS::min_from_time(input));
} }
@ -2444,12 +2443,10 @@ static Utf16String convert_number_to_local_date_and_time_string(double input)
auto seconds = JS::sec_from_time(input); auto seconds = JS::sec_from_time(input);
auto milliseconds = JS::ms_from_time(input); auto milliseconds = JS::ms_from_time(input);
if (seconds > 0) { if (milliseconds > 0)
if (milliseconds > 0) return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}", year, month, day, hour, minutes, seconds, milliseconds).trim("0"sv, TrimMode::Right);
return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.{:03d}", year, month, day, hour, minutes, seconds, milliseconds); if (seconds > 0)
return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}", year, month, day, hour, minutes, seconds); return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}", year, month, day, hour, minutes, seconds);
}
return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}", year, month, day, hour, minutes); return Utf16String::formatted("{:04d}-{:02d}-{:02d}T{:02d}:{:02d}", year, month, day, hour, minutes);
} }

View file

@ -34,8 +34,8 @@ password threw exception: InvalidStateError: valueAsNumber: Invalid input type u
date did not throw: 0 date did not throw: 0
month did not throw: 100 month did not throw: 100
week did not throw: 345600000 week did not throw: 345600000
time did not throw: 0 time did not throw: 100
datetime-local did not throw: 0 datetime-local did not throw: 100
color threw exception: InvalidStateError: valueAsNumber: Invalid input type used color threw exception: InvalidStateError: valueAsNumber: Invalid input type used
checkbox threw exception: InvalidStateError: valueAsNumber: Invalid input type used checkbox threw exception: InvalidStateError: valueAsNumber: Invalid input type used
radio threw exception: InvalidStateError: valueAsNumber: Invalid input type used radio threw exception: InvalidStateError: valueAsNumber: Invalid input type used

View file

@ -2,8 +2,7 @@ Harness status: OK
Found 32 tests Found 32 tests
31 Pass 32 Pass
1 Fail
Pass time element of default time value Pass time element of default time value
Pass step attribute on default value check Pass step attribute on default value check
Pass max attribute on default value check Pass max attribute on default value check
@ -26,7 +25,7 @@ Pass stepUp on step value hour
Pass stepDown on step value hour Pass stepDown on step value hour
Pass stepUp on step value second Pass stepUp on step value second
Pass stepDown on step value second Pass stepDown on step value second
Fail stepUp on step value with fractional seconds Pass stepUp on step value with fractional seconds
Pass stepDown on step value with fractional seconds Pass stepDown on step value with fractional seconds
Pass stepUp argument 2 times Pass stepUp argument 2 times
Pass stepDown argument 2 times Pass stepDown argument 2 times