Skip to content

Commit b121053

Browse files
committed
Polyfill: Further optimize fix to format plain Temporal objects in UTC
We can move the hardcoding of `timeZone: 'UTC'` into the individual amend functions, and not need the isPlain parameter.
1 parent 688d8e7 commit b121053

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

polyfill/lib/intl.mjs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,10 @@ const OPTIONS = SymbolCtor('options');
6666
// Construction of built-in Intl.DateTimeFormat objects is sloooooow,
6767
// so we'll only create those instances when we need them.
6868
// See https://bugs.chromium.org/p/v8/issues/detail?id=6528
69-
function getSlotLazy(obj, slot, isPlain) {
69+
function getSlotLazy(obj, slot) {
7070
let val = GetSlot(obj, slot);
7171
if (typeof val === 'function') {
72-
const baseOptions = val(GetSlot(obj, OPTIONS));
73-
const options = ObjectAssign({}, baseOptions);
74-
const amendedOptions = val(options);
75-
if (isPlain) {
76-
amendedOptions.timeZone = 'UTC';
77-
}
78-
val = new IntlDateTimeFormat(GetSlot(obj, LOCALE), amendedOptions);
72+
val = new IntlDateTimeFormat(GetSlot(obj, LOCALE), val(GetSlot(obj, OPTIONS)));
7973
SetSlot(obj, slot, val);
8074
}
8175
return val;
@@ -436,6 +430,7 @@ function timeAmend(originalOptions) {
436430
second: 'numeric'
437431
});
438432
}
433+
options.timeZone = 'UTC';
439434
return options;
440435
}
441436

@@ -469,6 +464,7 @@ function yearMonthAmend(originalOptions) {
469464
}
470465
ObjectAssign(options, { year: 'numeric', month: 'numeric' });
471466
}
467+
options.timeZone = 'UTC';
472468
return options;
473469
}
474470

@@ -502,6 +498,7 @@ function monthDayAmend(originalOptions) {
502498
}
503499
ObjectAssign(options, { month: 'numeric', day: 'numeric' });
504500
}
501+
options.timeZone = 'UTC';
505502
return options;
506503
}
507504

@@ -524,6 +521,7 @@ function dateAmend(originalOptions) {
524521
day: 'numeric'
525522
});
526523
}
524+
options.timeZone = 'UTC';
527525
return options;
528526
}
529527

@@ -560,6 +558,7 @@ function datetimeAmend(originalOptions) {
560558
second: 'numeric'
561559
});
562560
}
561+
options.timeZone = 'UTC';
563562
return options;
564563
}
565564

@@ -631,7 +630,7 @@ function extractOverrides(temporalObj, main) {
631630
isoDate: { year: 1970, month: 1, day: 1 },
632631
time: GetSlot(temporalObj, TIME)
633632
};
634-
const formatter = getSlotLazy(main, TIME_FMT, /* isPlain = */ true);
633+
const formatter = getSlotLazy(main, TIME_FMT);
635634
if (!formatter) throw new TypeErrorCtor('cannot format PlainTime with only date options');
636635
return {
637636
epochNs: ES.GetUTCEpochNanoseconds(isoDateTime),
@@ -648,7 +647,7 @@ function extractOverrides(temporalObj, main) {
648647
);
649648
}
650649
const isoDateTime = ES.CombineISODateAndTimeRecord(GetSlot(temporalObj, ISO_DATE), ES.NoonTimeRecord());
651-
const formatter = getSlotLazy(main, YM, /* isPlain = */ true);
650+
const formatter = getSlotLazy(main, YM);
652651
if (!formatter) throw new TypeErrorCtor('cannot format PlainYearMonth with only time options');
653652
return {
654653
epochNs: ES.GetUTCEpochNanoseconds(isoDateTime),
@@ -665,7 +664,7 @@ function extractOverrides(temporalObj, main) {
665664
);
666665
}
667666
const isoDateTime = ES.CombineISODateAndTimeRecord(GetSlot(temporalObj, ISO_DATE), ES.NoonTimeRecord());
668-
const formatter = getSlotLazy(main, MD, /* isPlain = */ true);
667+
const formatter = getSlotLazy(main, MD);
669668
if (!formatter) throw new TypeErrorCtor('cannot format PlainMonthDay with only time options');
670669
return {
671670
epochNs: ES.GetUTCEpochNanoseconds(isoDateTime),
@@ -682,7 +681,7 @@ function extractOverrides(temporalObj, main) {
682681
);
683682
}
684683
const isoDateTime = ES.CombineISODateAndTimeRecord(GetSlot(temporalObj, ISO_DATE), ES.NoonTimeRecord());
685-
const formatter = getSlotLazy(main, DATE, /* isPlain = */ true);
684+
const formatter = getSlotLazy(main, DATE);
686685
if (!formatter) throw new TypeErrorCtor('cannot format PlainDate with only time options');
687686
return {
688687
epochNs: ES.GetUTCEpochNanoseconds(isoDateTime),
@@ -701,7 +700,7 @@ function extractOverrides(temporalObj, main) {
701700
const isoDateTime = GetSlot(temporalObj, ISO_DATE_TIME);
702701
return {
703702
epochNs: ES.GetUTCEpochNanoseconds(isoDateTime),
704-
formatter: getSlotLazy(main, DATETIME, /* isPlain = */ true)
703+
formatter: getSlotLazy(main, DATETIME)
705704
};
706705
}
707706

@@ -714,7 +713,7 @@ function extractOverrides(temporalObj, main) {
714713
if (ES.IsTemporalInstant(temporalObj)) {
715714
return {
716715
epochNs: GetSlot(temporalObj, EPOCHNANOSECONDS),
717-
formatter: getSlotLazy(main, INST, /* isPlain = */ false)
716+
formatter: getSlotLazy(main, INST)
718717
};
719718
}
720719

0 commit comments

Comments
 (0)