Skip to content

Commit

Permalink
Editorial: refactor FormatSecondsStringPart
Browse files Browse the repository at this point in the history
This editorial commit refactors FormatSecondsStringPart to include
similar editorial changes made to GetOffsetStringFor in #2607.
  • Loading branch information
justingrant committed Jul 15, 2023
1 parent 736d6db commit 281f1bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,18 +2358,18 @@ export function ISODateTimePartString(part) {
export function FormatSecondsStringPart(second, millisecond, microsecond, nanosecond, precision) {
if (precision === 'minute') return '';

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

let fractionString;
if (precision === 'auto') {
if (fraction === 0) return secs;
fraction = `${fraction}`.padStart(9, '0');
while (fraction[fraction.length - 1] === '0') fraction = fraction.slice(0, -1);
if (fractionNanoseconds === 0) return secondsString;
fractionString = `${fractionNanoseconds}`.padStart(9, '0').replace(/0+$/, '');
} else {
if (precision === 0) return secs;
fraction = `${fraction}`.padStart(9, '0').slice(0, precision);
if (precision === 0) return secondsString;
fractionString = `${fractionNanoseconds}`.padStart(9, '0').slice(0, precision);
}
return `${secs}.${fraction}`;
return `${secondsString}.${fractionString}`;
}

export function TemporalInstantToString(instant, timeZone, precision) {
Expand Down
14 changes: 7 additions & 7 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,16 @@ <h1>FormatSecondsStringPart ( _second_, _millisecond_, _microsecond_, _nanosecon
1. Assert: _second_, _millisecond_, _microsecond_, and _nanosecond_ are integers.
1. If _precision_ is *"minute"*, return *""*.
1. Let _secondsString_ be the string-concatenation of the code unit 0x003A (COLON) and ToZeroPaddedDecimalString(_second_, 2).
1. Let _fraction_ be _millisecond_ &times; 10<sup>6</sup> + _microsecond_ &times; 10<sup>3</sup> + _nanosecond_.
1. Let _fractionNanoseconds_ be _millisecond_ &times; 10<sup>6</sup> + _microsecond_ &times; 10<sup>3</sup> + _nanosecond_.
1. If _precision_ is *"auto"*, then
1. If _fraction_ is 0, return _secondsString_.
1. Set _fraction_ to ToZeroPaddedDecimalString(_fraction_, 9).
1. Set _fraction_ to the longest possible substring of _fraction_ starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO).
1. If _fractionNanoseconds_ is 0, return _secondsString_.
1. Let _fractionString_ be ToZeroPaddedDecimalString(_fractionNanoseconds_, 9).
1. Set _fractionString_ to the longest prefix of _fractionString_ ending with a code unit other than 0x0030 (DIGIT ZERO).
1. Else,
1. If _precision_ is 0, return _secondsString_.
1. Set _fraction_ to ToZeroPaddedDecimalString(_fraction_, 9).
1. Set _fraction_ to the substring of _fraction_ from 0 to _precision_.
1. Return the string-concatenation of _secondsString_, the code unit 0x002E (FULL STOP), and _fraction_.
1. Let _fractionString_ be ToZeroPaddedDecimalString(_fractionNanoseconds_, 9).
1. Set _fractionString_ to the substring of _fractionString_ from 0 to _precision_.
1. Return the string-concatenation of _secondsString_, the code unit 0x002E (FULL STOP), and _fractionString_.
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit 281f1bc

Please sign in to comment.