Skip to content

Commit 993e632

Browse files
ptomatoMs2ger
authored andcommitted
Polyfill: Update Meiji era start date
Update internal era data to change hasYearZero to startingYear, so that we can accommodate the starting year of 6.
1 parent fe12f07 commit 993e632

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

polyfill/lib/calendar.mjs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,12 @@ const nonIsoHelperBase = {
767767
}
768768
// last era always gets all "leftover" (older than epoch) years,
769769
// so no need for a comparison like below.
770-
eraYear = year - e.anchorEpoch.year + (e.hasYearZero ? 0 : 1);
770+
eraYear = year - e.anchorEpoch.year + e.startingYear;
771771
return true;
772772
}
773773
const comparison = nonIsoHelperBase.compareCalendarDates(calendarDate, e.anchorEpoch);
774774
if (comparison >= 0) {
775-
eraYear = year - e.anchorEpoch.year + (e.hasYearZero ? 0 : 1);
775+
eraYear = year - e.anchorEpoch.year + e.startingYear;
776776
return true;
777777
}
778778
return false;
@@ -803,7 +803,7 @@ const nonIsoHelperBase = {
803803
if (matchingEra.reverseOf) {
804804
year = matchingEra.anchorEpoch.year - eraYear;
805805
} else {
806-
year = eraYear + matchingEra.anchorEpoch.year - (matchingEra.hasYearZero ? 0 : 1);
806+
year = eraYear + matchingEra.anchorEpoch.year - matchingEra.startingYear;
807807
}
808808
// We'll accept dates where the month/day is earlier than the start of
809809
// the era or after its end as long as it's in the same year. If that
@@ -1577,9 +1577,9 @@ const helperIndian = makeNonISOHelper([{ code: 'shaka', isoEpoch: { year: 79, mo
15771577
* // the last (oldest) era in the array.
15781578
* reverseOf?: Era;
15791579
*
1580-
* // If true, the era's years are 0-based. If omitted or false,
1581-
* // then the era's years are 1-based.
1582-
* hasYearZero?: boolean;
1580+
* // Number of the era's starting year (for example, 0 if there's a Year 0).
1581+
* // If omitted, the era's years are 1-based.
1582+
* startingYear?: number = 1;
15831583
*
15841584
* // Override if this era is the anchor. Not normally used because
15851585
* // anchor eras are inferred.
@@ -1601,10 +1601,11 @@ function adjustEras(eras) {
16011601
let anchorEra;
16021602
Call(ArrayPrototypeForEach, eras, [
16031603
(e) => {
1604+
e.startingYear ??= 1;
16041605
if (e.isAnchor || (!e.anchorEpoch && !e.reverseOf)) {
16051606
assert(!anchorEra, 'Invalid era data: cannot have multiple anchor eras');
16061607
anchorEra = e;
1607-
e.anchorEpoch = { year: e.hasYearZero ? 0 : 1 };
1608+
e.anchorEpoch = { year: e.startingYear };
16081609
} else {
16091610
assert(e.code, 'Invalid era data: if era name is blank, it must be the anchor era');
16101611
}
@@ -1701,7 +1702,7 @@ const makeHelperGregorian = (id, originalEras) => {
17011702
calendarDate = this.adjustCalendarDate(calendarDate);
17021703
const { year, month, day } = calendarDate;
17031704
const { anchorEra } = this;
1704-
const isoYearEstimate = year + anchorEra.isoEpoch.year - (anchorEra.hasYearZero ? 0 : 1);
1705+
const isoYearEstimate = year + anchorEra.isoEpoch.year - anchorEra.startingYear;
17051706
return ES.RegulateISODate(isoYearEstimate, month, day, 'constrain');
17061707
}
17071708
});
@@ -1821,18 +1822,18 @@ const helperGregory = ObjectAssign(
18211822

18221823
const helperJapanese = ObjectAssign(
18231824
// NOTE: Only the 5 modern eras (Meiji and later) are included. For dates
1824-
// before Meiji 1, the `ce` and `bce` eras are used. Challenges with pre-Meiji
1825+
// before Meiji 6, the `ce` and `bce` eras are used. Challenges with pre-Meiji
18251826
// eras include:
18261827
// - Start/end dates of older eras are not precisely defined, which is
18271828
// challenging given Temporal's need for precision
18281829
// - Some era dates and/or names are disputed by historians
18291830
// - As historical research proceeds, new eras are discovered and existing era
18301831
// dates are modified, leading to considerable churn which is not good for
18311832
// Temporal use.
1832-
// - The earliest era (in 645 CE) may not end up being the earliest depending
1833-
// on future historical scholarship
1834-
// - Before Meiji, Japan used a lunar (or lunisolar?) calendar but AFAIK
1835-
// that's not reflected in the ICU implementation.
1833+
// - The earliest era (in 645 CE) may not end up being the earliest depending
1834+
// on future historical scholarship
1835+
// - Before Meiji, Japan used a lunisolar calendar but that's not reflected in
1836+
// the ICU implementation.
18361837
//
18371838
// For more discussion: https://github.com/tc39/proposal-temporal/issues/526.
18381839
//
@@ -1842,21 +1843,16 @@ const helperJapanese = ObjectAssign(
18421843
//
18431844
// NOTE: Japan started using the Gregorian calendar in 6 Meiji, replacing a
18441845
// lunisolar calendar. So the day before January 1 of 6 Meiji (1873) was not
1845-
// December 31, but December 2, of 5 Meiji (1872). The existing Ecma-402
1846-
// Japanese calendar doesn't seem to take this into account, so neither do we:
1847-
// > args = ['en-ca-u-ca-japanese', { era: 'short' }]
1848-
// > new Date('1873-01-01T12:00').toLocaleString(...args)
1849-
// '1 1, 6 Meiji, 12:00:00 PM'
1850-
// > new Date('1872-12-31T12:00').toLocaleString(...args)
1851-
// '12 31, 5 Meiji, 12:00:00 PM'
1846+
// December 31, but M12-02, of 5 Meiji (1872). To avoid confusion between
1847+
// lunisolar and solar dates we count years prior to 1872 as CE.
18521848
makeHelperSameMonthDayAsGregorian('japanese', [
18531849
// The Japanese calendar `year` is just the ISO year, because (unlike other
18541850
// ICU calendars) there's no obvious "default era", we use the ISO year.
18551851
{ code: 'reiwa', isoEpoch: { year: 2019, month: 5, day: 1 }, anchorEpoch: { year: 2019, month: 5, day: 1 } },
18561852
{ code: 'heisei', isoEpoch: { year: 1989, month: 1, day: 8 }, anchorEpoch: { year: 1989, month: 1, day: 8 } },
18571853
{ code: 'showa', isoEpoch: { year: 1926, month: 12, day: 25 }, anchorEpoch: { year: 1926, month: 12, day: 25 } },
18581854
{ code: 'taisho', isoEpoch: { year: 1912, month: 7, day: 30 }, anchorEpoch: { year: 1912, month: 7, day: 30 } },
1859-
{ code: 'meiji', isoEpoch: { year: 1868, month: 10, day: 23 }, anchorEpoch: { year: 1868, month: 10, day: 23 } },
1855+
{ code: 'meiji', isoEpoch: { year: 1873, month: 1, day: 1 }, anchorEpoch: { year: 1873 }, startingYear: 6 },
18601856
{ code: 'ce', isoEpoch: { year: 1, month: 1, day: 1 } },
18611857
{ code: 'bce', reverseOf: 'ce' }
18621858
]),

0 commit comments

Comments
 (0)