Skip to content

Commit 630ff7a

Browse files
committed
Editorial: refactor FormatSecondsStringPart
This editorial commit refactors FormatSecondsStringPart to include similar editorial changes made to GetOffsetStringFor in tc39#2607.
1 parent d0268f9 commit 630ff7a

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,18 +2358,18 @@ export function ISODateTimePartString(part) {
23582358
export function FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision) {
23592359
if (precision === 'minute') return '';
23602360

2361-
const secs = `:${ISODateTimePartString(second)}`;
2362-
let fraction = millisecond * 1e6 + microsecond * 1e3 + nanosecond;
2361+
const secondsString = `:${ISODateTimePartString(second)}`;
2362+
let fractionNanoseconds = millisecond * 1e6 + microsecond * 1e3 + nanosecond;
23632363

2364+
let fractionString;
23642365
if (precision === 'auto') {
2365-
if (fraction === 0) return secs;
2366-
fraction = `${fraction}`.padStart(9, '0');
2367-
while (fraction[fraction.length - 1] === '0') fraction = fraction.slice(0, -1);
2366+
if (fractionNanoseconds === 0) return secondsString;
2367+
fractionString = `${fractionNanoseconds}`.padStart(9, '0').replace(/0+$/, '');
23682368
} else {
2369-
if (precision === 0) return secs;
2370-
fraction = `${fraction}`.padStart(9, '0').slice(0, precision);
2369+
if (precision === 0) return secondsString;
2370+
fractionString = `${fractionNanoseconds}`.padStart(9, '0').slice(0, precision);
23712371
}
2372-
return `${secs}.${fraction}`;
2372+
return `${secondsString}.${fractionString}`;
23732373
}
23742374

23752375
export function TemporalInstantToString(instant, timeZone, precision) {

spec/abstractops.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,16 @@ <h1>FormatSecondsStringPart ( _second_, _millisecond_, _microsecond_, _nanosecon
687687
1. Assert: _second_, _millisecond_, _microsecond_, and _nanosecond_ are integers.
688688
1. If _precision_ is *"minute"*, return *""*.
689689
1. Let _secondsString_ be the string-concatenation of the code unit 0x003A (COLON) and ToZeroPaddedDecimalString(_second_, 2).
690-
1. Let _fraction_ be _millisecond_ &times; 10<sup>6</sup> + _microsecond_ &times; 10<sup>3</sup> + _nanosecond_.
690+
1. Let _fractionNanoseconds_ be _millisecond_ &times; 10<sup>6</sup> + _microsecond_ &times; 10<sup>3</sup> + _nanosecond_.
691691
1. If _precision_ is *"auto"*, then
692-
1. If _fraction_ is 0, return _secondsString_.
693-
1. Set _fraction_ to ToZeroPaddedDecimalString(_fraction_, 9).
694-
1. Set _fraction_ to the longest possible substring of _fraction_ starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
692+
1. If _fractionNanoseconds_ is 0, return _secondsString_.
693+
1. Let _fractionString_ be ToZeroPaddedDecimalString(_fractionNanoseconds_, 9).
694+
1. Set _fractionString_ to the longest prefix of _fractionString_ ending with a code unit other than 0x0030 (DIGIT ZERO).
695695
1. Else,
696696
1. If _precision_ is 0, return _secondsString_.
697-
1. Set _fraction_ to ToZeroPaddedDecimalString(_fraction_, 9).
698-
1. Set _fraction_ to the substring of _fraction_ from 0 to _precision_.
699-
1. Return the string-concatenation of _secondsString_, the code unit 0x002E (FULL STOP), and _fraction_.
697+
1. Let _fractionString_ be ToZeroPaddedDecimalString(_fractionNanoseconds_, 9).
698+
1. Set _fractionString_ to the substring of _fractionString_ from 0 to _precision_.
699+
1. Return the string-concatenation of _secondsString_, the code unit 0x002E (FULL STOP), and _fractionString_.
700700
</emu-alg>
701701
</emu-clause>
702702

0 commit comments

Comments
 (0)