|
| 1 | +// Copyright (C) 2025 Igalia, S.L. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/*--- |
| 5 | +esid: sec-temporal.plainyearmonth.prototype.add |
| 6 | +description: Arithmetic around leap months in the chinese calendar |
| 7 | +features: [Temporal, Intl.Era-monthcode] |
| 8 | +includes: [temporalHelpers.js] |
| 9 | +---*/ |
| 10 | + |
| 11 | +const calendar = "chinese"; |
| 12 | +const options = { overflow: "reject" }; |
| 13 | + |
| 14 | +// Years |
| 15 | + |
| 16 | +const years1 = new Temporal.Duration(1); |
| 17 | +const years1n = new Temporal.Duration(-1); |
| 18 | + |
| 19 | +const leap193807L = Temporal.PlainYearMonth.from({ year: 1938, monthCode: "M07L", calendar }, options); |
| 20 | +const leap195205L = Temporal.PlainYearMonth.from({ year: 1952, monthCode: "M05L", calendar }, options); |
| 21 | +const leap196603L = Temporal.PlainYearMonth.from({ year: 1966, monthCode: "M03L", calendar }, options); |
| 22 | +const common200008 = Temporal.PlainYearMonth.from({ year: 2000, monthCode: "M08", calendar }, options); |
| 23 | +const common200108 = Temporal.PlainYearMonth.from({ year: 2001, monthCode: "M08", calendar }, options); |
| 24 | +const common201901 = Temporal.PlainYearMonth.from({ year: 2019, monthCode: "M01", calendar }, options); |
| 25 | +const common201904 = Temporal.PlainYearMonth.from({ year: 2019, monthCode: "M04", calendar }, options); |
| 26 | +const leap202004 = Temporal.PlainYearMonth.from({ year: 2020, monthCode: "M04", calendar }, options); |
| 27 | +const leap202004L = Temporal.PlainYearMonth.from({ year: 2020, monthCode: "M04L", calendar }, options); |
| 28 | +const common202104 = Temporal.PlainYearMonth.from({ year: 2021, monthCode: "M04", calendar }, options); |
| 29 | + |
| 30 | +TemporalHelpers.assertPlainYearMonth( |
| 31 | + common201901.add(years1), |
| 32 | + 2020, 1, "M01", "add 1 year from non-leap day", |
| 33 | + undefined, undefined, null |
| 34 | +); |
| 35 | + |
| 36 | +TemporalHelpers.assertPlainYearMonth( |
| 37 | + leap196603L.add(years1), |
| 38 | + 1967, 3, "M03", "Adding 1 year to leap month M03L lands in common-year M03 with overflow constrain", |
| 39 | + undefined, undefined, null |
| 40 | +); |
| 41 | + |
| 42 | +assert.throws(RangeError, function () { |
| 43 | + leap196603L.add(years1, options); |
| 44 | +}, "Adding 1 year to leap month rejects"); |
| 45 | + |
| 46 | +TemporalHelpers.assertPlainYearMonth( |
| 47 | + leap193807L.add(years1), |
| 48 | + 1939, 7, "M07", "Adding 1 year to leap month M07L on day 30 constrains to M07 day 29", |
| 49 | + undefined, undefined, null |
| 50 | +); |
| 51 | + |
| 52 | +assert.throws(RangeError, function () { |
| 53 | + leap193807L.add(years1, options); |
| 54 | +}, "Adding 1 year to leap month day 30 rejects"); |
| 55 | + |
| 56 | +TemporalHelpers.assertPlainYearMonth( |
| 57 | + common201904.add(years1, options), |
| 58 | + 2020, 4, "M04", "Adding 1 year to common-year M04 lands in leap-year M04", |
| 59 | + undefined, undefined, null |
| 60 | +); |
| 61 | + |
| 62 | +TemporalHelpers.assertPlainYearMonth( |
| 63 | + leap202004.add(years1, options), |
| 64 | + 2021, 4, "M04", "Adding 1 year to leap-year M04 lands in common-year M04", |
| 65 | + undefined, undefined, null |
| 66 | +); |
| 67 | + |
| 68 | +TemporalHelpers.assertPlainYearMonth( |
| 69 | + Temporal.PlainYearMonth.from({ year: 2012, monthCode: "M04L", calendar }, options).add(new Temporal.Duration(8), options), |
| 70 | + 2020, 5, "M04L", "Adding years to go from one M04L to the next M04L", |
| 71 | + undefined, undefined, null |
| 72 | +); |
| 73 | + |
| 74 | +TemporalHelpers.assertPlainYearMonth( |
| 75 | + common200008.add(years1, options), |
| 76 | + 2001, 9, "M08", "Adding 1 year crossing leap month", |
| 77 | + undefined, undefined, null |
| 78 | +); |
| 79 | + |
| 80 | +TemporalHelpers.assertPlainYearMonth( |
| 81 | + common201904.add(new Temporal.Duration(2), options), |
| 82 | + 2021, 4, "M04", "Adding 2 years to common-year M04 crossing leap year lands in common-year M04", |
| 83 | + undefined, undefined, null |
| 84 | +); |
| 85 | + |
| 86 | +TemporalHelpers.assertPlainYearMonth( |
| 87 | + common201901.add(years1n), |
| 88 | + 2018, 1, "M01", "Subtracting 1 year from non-leap day", |
| 89 | + undefined, undefined, null |
| 90 | +); |
| 91 | + |
| 92 | +TemporalHelpers.assertPlainYearMonth( |
| 93 | + leap196603L.add(years1n), |
| 94 | + 1965, 3, "M03", "Subtracting 1 year from leap month M03L lands in common-year M03 with overflow constrain", |
| 95 | + undefined, undefined, null |
| 96 | +); |
| 97 | + |
| 98 | +assert.throws(RangeError, function () { |
| 99 | + leap196603L.add(years1n, options); |
| 100 | +}, "Subtracting 1 year from leap month rejects"); |
| 101 | + |
| 102 | +TemporalHelpers.assertPlainYearMonth( |
| 103 | + leap195205L.add(years1n), |
| 104 | + 1951, 5, "M05", "Subtracting 1 year from leap month M05L on day 30 constrains to M05 day 29", |
| 105 | + undefined, undefined, null |
| 106 | +); |
| 107 | + |
| 108 | +assert.throws(RangeError, function () { |
| 109 | + leap195205L.add(years1n, options); |
| 110 | +}, "Subtracting 1 year from leap month day 30 rejects"); |
| 111 | + |
| 112 | +TemporalHelpers.assertPlainYearMonth( |
| 113 | + common202104.add(years1n, options), |
| 114 | + 2020, 4, "M04", "Subtracting 1 year from common-year M04 lands in leap-year M04", |
| 115 | + undefined, undefined, null |
| 116 | +); |
| 117 | + |
| 118 | +TemporalHelpers.assertPlainYearMonth( |
| 119 | + leap202004.add(years1n, options), |
| 120 | + 2019, 4, "M04", "Subtracting 1 year from leap-year M04 lands in common-year M04", |
| 121 | + undefined, undefined, null |
| 122 | +); |
| 123 | + |
| 124 | +TemporalHelpers.assertPlainYearMonth( |
| 125 | + leap202004L.add(new Temporal.Duration(-8), options), |
| 126 | + 2012, 5, "M04L", "Subtracting years to go from one M04L to the previous M04L", |
| 127 | + undefined, undefined, null |
| 128 | +); |
| 129 | + |
| 130 | +TemporalHelpers.assertPlainYearMonth( |
| 131 | + common200108.add(years1n, options), |
| 132 | + 2000, 8, "M08", "Subtracting 1 year crossing leap month", |
| 133 | + undefined, undefined, null |
| 134 | +); |
| 135 | + |
| 136 | +TemporalHelpers.assertPlainYearMonth( |
| 137 | + common202104.add(new Temporal.Duration(-2), options), |
| 138 | + 2019, 4, "M04", "Subtracting 2 years from common-year M04 crossing leap year lands in common-year M04", |
| 139 | + undefined, undefined, null |
| 140 | +); |
| 141 | + |
| 142 | +// Months |
| 143 | + |
| 144 | +const months1 = new Temporal.Duration(0, 1); |
| 145 | +const months1n = new Temporal.Duration(0, -1); |
| 146 | +const months12 = new Temporal.Duration(0, 12); |
| 147 | +const months12n = new Temporal.Duration(0, -12); |
| 148 | +const months13 = new Temporal.Duration(0, 13); |
| 149 | +const months13n = new Temporal.Duration(0, -13); |
| 150 | + |
| 151 | +const leap202003 = Temporal.PlainYearMonth.from({ year: 2020, monthCode: "M03", calendar }, options); |
| 152 | +const leap202006 = Temporal.PlainYearMonth.from({ year: 2020, monthCode: "M06", calendar }, options); |
| 153 | + |
| 154 | +TemporalHelpers.assertPlainYearMonth( |
| 155 | + Temporal.PlainYearMonth.from({ year: 1947, monthCode: "M02L", calendar }, options).add(months1), |
| 156 | + 1947, 4, "M03", "add 1 month, starting at start of leap month", |
| 157 | + undefined, undefined, null |
| 158 | +); |
| 159 | + |
| 160 | +TemporalHelpers.assertPlainYearMonth( |
| 161 | + Temporal.PlainYearMonth.from({ year: 1955, monthCode: "M03L", calendar }, options).add(months1), |
| 162 | + 1955, 5, "M04", "add 1 month, starting at start of leap month with 30 days", |
| 163 | + undefined, undefined, null |
| 164 | +); |
| 165 | + |
| 166 | +TemporalHelpers.assertPlainYearMonth( |
| 167 | + leap202003.add(months1), |
| 168 | + 2020, 4, "M04", "adding 1 month to M03 in leap year lands in M04 (not M04L)", |
| 169 | + undefined, undefined, null |
| 170 | +); |
| 171 | + |
| 172 | +TemporalHelpers.assertPlainYearMonth( |
| 173 | + leap202003.add(new Temporal.Duration(0, 2)), |
| 174 | + 2020, 5, "M04L", "adding 2 months to M03 in leap year lands in M04L (leap month)", |
| 175 | + undefined, undefined, null |
| 176 | +); |
| 177 | + |
| 178 | +TemporalHelpers.assertPlainYearMonth( |
| 179 | + leap202003.add(new Temporal.Duration(0, 3)), |
| 180 | + 2020, 6, "M05", "adding 3 months to M03 in leap year lands in M05 (not M06)", |
| 181 | + undefined, undefined, null |
| 182 | +); |
| 183 | + |
| 184 | +TemporalHelpers.assertPlainYearMonth( |
| 185 | + common201904.add(months12), |
| 186 | + 2020, 4, "M04", "Adding 12 months to common-year M04 lands in leap-year M04", |
| 187 | + undefined, undefined, null |
| 188 | +); |
| 189 | + |
| 190 | +TemporalHelpers.assertPlainYearMonth( |
| 191 | + common201904.add(months13), |
| 192 | + 2020, 5, "M04L", "Adding 13 months to common-year M04 lands in leap-year M04L", |
| 193 | + undefined, undefined, null |
| 194 | +); |
| 195 | + |
| 196 | +TemporalHelpers.assertPlainYearMonth( |
| 197 | + leap202004.add(months12), |
| 198 | + 2021, 3, "M03", "Adding 12 months to leap-year M04 lands in common-year M03", |
| 199 | + undefined, undefined, null |
| 200 | +); |
| 201 | + |
| 202 | +TemporalHelpers.assertPlainYearMonth( |
| 203 | + leap202004.add(months13), |
| 204 | + 2021, 4, "M04", "Adding 13 months to leap-year M04 lands in common-year M04", |
| 205 | + undefined, undefined, null |
| 206 | +); |
| 207 | + |
| 208 | +TemporalHelpers.assertPlainYearMonth( |
| 209 | + leap202004L.add(months12), |
| 210 | + 2021, 4, "M04", "Adding 12 months to M04L lands in common-year M04", |
| 211 | + undefined, undefined, null |
| 212 | +); |
| 213 | + |
| 214 | +TemporalHelpers.assertPlainYearMonth( |
| 215 | + common200008.add(new Temporal.Duration(1, 12), options), |
| 216 | + 2002, 8, "M08", "Adding 1y 12mo crossing leap month in the year part", |
| 217 | + undefined, undefined, null |
| 218 | +); |
| 219 | + |
| 220 | +TemporalHelpers.assertPlainYearMonth( |
| 221 | + common200108.add(new Temporal.Duration(2, 13), options), |
| 222 | + 2004, 9, "M08", "Adding 1y 13mo crossing leap month in the months part", |
| 223 | + undefined, undefined, null |
| 224 | +); |
| 225 | + |
| 226 | +TemporalHelpers.assertPlainYearMonth( |
| 227 | + common201904.add(new Temporal.Duration(0, 24)), |
| 228 | + 2021, 3, "M03", "Adding 24 months to common-year M04 crossing leap year with M04L, lands in common-year M03", |
| 229 | + undefined, undefined, null |
| 230 | +); |
| 231 | + |
| 232 | +TemporalHelpers.assertPlainYearMonth( |
| 233 | + common201904.add(new Temporal.Duration(0, 25)), |
| 234 | + 2021, 4, "M04", "Adding 25 months to common-year M04 crossing leap year with M04L, lands in common-year M04", |
| 235 | + undefined, undefined, null |
| 236 | +); |
| 237 | + |
| 238 | +TemporalHelpers.assertPlainYearMonth( |
| 239 | + leap202006.add(months1n), |
| 240 | + 2020, 6, "M05", "Subtracting 1 month from M06 in leap year lands in M05", |
| 241 | + undefined, undefined, null |
| 242 | +); |
| 243 | + |
| 244 | +TemporalHelpers.assertPlainYearMonth( |
| 245 | + leap202006.add(new Temporal.Duration(0, -2)), |
| 246 | + 2020, 5, "M04L", "Subtracting 2 months from M06 in leap year lands in M04L (leap month)", |
| 247 | + undefined, undefined, null |
| 248 | +); |
| 249 | + |
| 250 | +TemporalHelpers.assertPlainYearMonth( |
| 251 | + leap202006.add(new Temporal.Duration(0, -3)), |
| 252 | + 2020, 4, "M04", "Subtracting 3 months from M06 in leap year lands in M04 (not M03)", |
| 253 | + undefined, undefined, null |
| 254 | +); |
| 255 | + |
| 256 | +TemporalHelpers.assertPlainYearMonth( |
| 257 | + Temporal.PlainYearMonth.from({ year: 2020, monthCode: "M05", calendar }, options).add(months1n), |
| 258 | + 2020, 5, "M04L", "Subtracting 1 month from M05 in leap year lands in M04L", |
| 259 | + undefined, undefined, null |
| 260 | +); |
| 261 | + |
| 262 | +TemporalHelpers.assertPlainYearMonth( |
| 263 | + leap202004L.add(months1n), |
| 264 | + 2020, 4, "M04", "Subtracting 1 month from M04L in calendar lands in M04", |
| 265 | + undefined, undefined, null |
| 266 | +); |
| 267 | + |
| 268 | +TemporalHelpers.assertPlainYearMonth( |
| 269 | + common202104.add(months12n), |
| 270 | + 2020, 5, "M04L", "Subtracting 12 months from common-year M04 lands in leap-year M04L", |
| 271 | + undefined, undefined, null |
| 272 | +); |
| 273 | + |
| 274 | +TemporalHelpers.assertPlainYearMonth( |
| 275 | + common202104.add(months13n), |
| 276 | + 2020, 4, "M04", "Subtracting 13 months from common-year M04 lands in leap-year M04", |
| 277 | + undefined, undefined, null |
| 278 | +); |
| 279 | + |
| 280 | +TemporalHelpers.assertPlainYearMonth( |
| 281 | + leap202004.add(months12n), |
| 282 | + 2019, 4, "M04", "Subtracting 12 months from leap-year M04 lands in common-year M04", |
| 283 | + undefined, undefined, null |
| 284 | +); |
| 285 | + |
| 286 | +TemporalHelpers.assertPlainYearMonth( |
| 287 | + leap202004L.add(months12n), |
| 288 | + 2019, 5, "M05", "Subtracting 12 months from M04L lands in common-year M05", |
| 289 | + undefined, undefined, null |
| 290 | +); |
| 291 | + |
| 292 | +TemporalHelpers.assertPlainYearMonth( |
| 293 | + leap202004L.add(months13n), |
| 294 | + 2019, 4, "M04", "Subtracting 13 months from M04L lands in common-year M04", |
| 295 | + undefined, undefined, null |
| 296 | +); |
| 297 | + |
| 298 | +TemporalHelpers.assertPlainYearMonth( |
| 299 | + common200108.add(new Temporal.Duration(-1, -12), options), |
| 300 | + 1999, 8, "M08", "Adding 1y 12mo crossing leap month in the year part", |
| 301 | + undefined, undefined, null |
| 302 | +); |
| 303 | + |
| 304 | +TemporalHelpers.assertPlainYearMonth( |
| 305 | + common200008.add(new Temporal.Duration(-2, -13), options), |
| 306 | + 1997, 8, "M08", "Adding 1y 13mo crossing leap month in the months part", |
| 307 | + undefined, undefined, null |
| 308 | +); |
| 309 | + |
| 310 | +TemporalHelpers.assertPlainYearMonth( |
| 311 | + common202104.add(new Temporal.Duration(0, -24)), |
| 312 | + 2019, 5, "M05", "Subtracting 24 months from common-year M04 crossing leap year with M04L, lands in common-year M05", |
| 313 | + undefined, undefined, null |
| 314 | +); |
| 315 | + |
| 316 | +TemporalHelpers.assertPlainYearMonth( |
| 317 | + common202104.add(new Temporal.Duration(0, -25)), |
| 318 | + 2019, 4, "M04", "Subtracting 25 months from common-year M04 crossing leap year with M04L, lands in common-year M04", |
| 319 | + undefined, undefined, null |
| 320 | +); |
0 commit comments