Skip to content

Commit 134b2e2

Browse files
ryzokukenMs2ger
authored andcommitted
polyfill: fix Instant precision bug
Fix a bug where Instant.prototype.toString produced the incorrect result where the fractional part included leading zeroes and precision was not auto or 0. Includes test262 test for the same. Fixes: #1336
1 parent acb4f53 commit 134b2e2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ export const ES = ObjectAssign({}, ES2020, {
16761676
while (fraction[fraction.length - 1] === '0') fraction = fraction.slice(0, -1);
16771677
} else {
16781678
if (precision === 0) return secs;
1679-
fraction = `${fraction}`.slice(0, precision).padStart(precision, '0');
1679+
fraction = `${fraction}`.padStart(9, '0').slice(0, precision);
16801680
}
16811681
return `${secs}.${fraction}`;
16821682
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (C) 2021 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
const { Instant } = Temporal;
5+
6+
const isoString = '2020-01-01T23:58:57.012034Z';
7+
const instant = Instant.from(isoString);
8+
const instantIsoStrMicros = instant.toString({
9+
smallestUnit: 'microseconds'
10+
});
11+
12+
assert.sameValue(instantIsoStrMicros, isoString);

0 commit comments

Comments
 (0)