|
| 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.plaindate.prototype.subtract |
| 6 | +description: Constraining the day at end of month (indian calendar) |
| 7 | +includes: [temporalHelpers.js] |
| 8 | +features: [Temporal, Intl.Era-monthcode] |
| 9 | +---*/ |
| 10 | + |
| 11 | +const calendar = "indian"; |
| 12 | +const options = { overflow: "reject" }; |
| 13 | + |
| 14 | +// 31-day months: 02-06 |
| 15 | +// 30-day months: 07-12 |
| 16 | +// Chaitra (01) has 30 days in common years and 31 in leap years |
| 17 | +// See leap-year-indian.js for tests adding years |
| 18 | + |
| 19 | +const common0231 = Temporal.PlainDate.from({ year: 1944, monthCode: "M02", day: 31, calendar }, options); |
| 20 | +const leap0131 = Temporal.PlainDate.from({ year: 1946, monthCode: "M01", day: 31, calendar }, options); |
| 21 | +const common0231After = Temporal.PlainDate.from({ year: 1947, monthCode: "M02", day: 31, calendar }, options); |
| 22 | + |
| 23 | +const months1 = new Temporal.Duration(0, -1); |
| 24 | +const months2 = new Temporal.Duration(0, -2); |
| 25 | +const months3 = new Temporal.Duration(0, -3); |
| 26 | +const months4 = new Temporal.Duration(0, -4); |
| 27 | +const months5 = new Temporal.Duration(0, -5); |
| 28 | +const months6 = new Temporal.Duration(0, -6); |
| 29 | +const months7 = new Temporal.Duration(0, -7); |
| 30 | +const months8 = new Temporal.Duration(0, -8); |
| 31 | +const months9 = new Temporal.Duration(0, -9); |
| 32 | +const months10 = new Temporal.Duration(0, -10); |
| 33 | +const months11 = new Temporal.Duration(0, -11); |
| 34 | +const months1n = new Temporal.Duration(0, 1); |
| 35 | +const months2n = new Temporal.Duration(0, 2); |
| 36 | +const months3n = new Temporal.Duration(0, 3); |
| 37 | +const months4n = new Temporal.Duration(0, 4); |
| 38 | +const months5n = new Temporal.Duration(0, 5); |
| 39 | +const months6n = new Temporal.Duration(0, 6); |
| 40 | +const months7n = new Temporal.Duration(0, 7); |
| 41 | +const months8n = new Temporal.Duration(0, 8); |
| 42 | +const months9n = new Temporal.Duration(0, 9); |
| 43 | +const months10n = new Temporal.Duration(0, 10); |
| 44 | +const months11n = new Temporal.Duration(0, 11); |
| 45 | +const months12n = new Temporal.Duration(0, 12); |
| 46 | +const months13n = new Temporal.Duration(0, 13); |
| 47 | + |
| 48 | +// Common year, forwards |
| 49 | + |
| 50 | +[ |
| 51 | + [months1, 3, "M03"], |
| 52 | + [months2, 4, "M04"], |
| 53 | + [months3, 5, "M05"], |
| 54 | + [months4, 6, "M06"], |
| 55 | +].forEach(function ([months, month, monthCode]) { |
| 56 | + TemporalHelpers.assertPlainDate( |
| 57 | + common0231.subtract(months, options), |
| 58 | + 1944, month, monthCode, 31, `common-year ${monthCode} does not reject 31 when adding`, |
| 59 | + "shaka", 1944); |
| 60 | +}); |
| 61 | + |
| 62 | +[ |
| 63 | + [months5, 7, "M07"], |
| 64 | + [months6, 8, "M08"], |
| 65 | + [months7, 9, "M09"], |
| 66 | + [months8, 10, "M10"], |
| 67 | + [months9, 11, "M11"], |
| 68 | + [months10, 12, "M12"], |
| 69 | +].forEach(function ([months, month, monthCode]) { |
| 70 | + TemporalHelpers.assertPlainDate( |
| 71 | + common0231.subtract(months), |
| 72 | + 1944, month, monthCode, 30, `common-year ${monthCode} constrains to 30 when adding`, |
| 73 | + "shaka", 1944); |
| 74 | + assert.throws(RangeError, function () { |
| 75 | + common0231.subtract(months, options); |
| 76 | + }, `common-year ${monthCode} rejects 31 when adding`); |
| 77 | +}); |
| 78 | + |
| 79 | +TemporalHelpers.assertPlainDate( |
| 80 | + common0231.subtract(months11), |
| 81 | + 1945, 1, "M01", 30, "common-year Chaitra constrains to 30 when adding", |
| 82 | + "shaka", 1945); |
| 83 | +assert.throws(RangeError, function () { |
| 84 | + common0231.subtract(months11, options); |
| 85 | +}, "common-year Chaitra rejects 31 when adding"); |
| 86 | + |
| 87 | +// Leap year, forwards |
| 88 | + |
| 89 | +[ |
| 90 | + [months1, 2, "M02"], |
| 91 | + [months2, 3, "M03"], |
| 92 | + [months3, 4, "M04"], |
| 93 | + [months4, 5, "M05"], |
| 94 | + [months5, 6, "M06"], |
| 95 | +].forEach(function ([months, month, monthCode]) { |
| 96 | + TemporalHelpers.assertPlainDate( |
| 97 | + leap0131.subtract(months, options), |
| 98 | + 1946, month, monthCode, 31, `leap-year ${monthCode} does not reject 31 when adding`, |
| 99 | + "shaka", 1946); |
| 100 | +}); |
| 101 | + |
| 102 | +[ |
| 103 | + [months6, 7, "M07"], |
| 104 | + [months7, 8, "M08"], |
| 105 | + [months8, 9, "M09"], |
| 106 | + [months9, 10, "M10"], |
| 107 | + [months10, 11, "M11"], |
| 108 | + [months11, 12, "M12"], |
| 109 | +].forEach(function ([months, month, monthCode]) { |
| 110 | + TemporalHelpers.assertPlainDate( |
| 111 | + leap0131.subtract(months), |
| 112 | + 1946, month, monthCode, 30, `leap-year ${monthCode} constrains to 30 when adding`, |
| 113 | + "shaka", 1946); |
| 114 | + assert.throws(RangeError, function () { |
| 115 | + leap0131.subtract(months, options); |
| 116 | + }, `leap-year ${monthCode} rejects 31 when adding`); |
| 117 | +}); |
| 118 | + |
| 119 | +// Common year, backwards |
| 120 | + |
| 121 | +TemporalHelpers.assertPlainDate( |
| 122 | + common0231.subtract(months1n), |
| 123 | + 1944, 1, "M01", 30, `common-year Chaitra constrains to 30 when subtracting`, |
| 124 | + "shaka", 1944); |
| 125 | +assert.throws(RangeError, function () { |
| 126 | + common0231.subtract(months1n, options); |
| 127 | +}, "common-year Chaitra rejects 31 when subtracting"); |
| 128 | + |
| 129 | +[ |
| 130 | + [months12n, 2, "M02"], |
| 131 | + [months11n, 3, "M03"], |
| 132 | + [months10n, 4, "M04"], |
| 133 | + [months9n, 5, "M05"], |
| 134 | + [months8n, 6, "M06"] |
| 135 | +].forEach(function ([months, month, monthCode]) { |
| 136 | + TemporalHelpers.assertPlainDate( |
| 137 | + common0231.subtract(months, options), |
| 138 | + 1943, month, monthCode, 31, `common-year ${monthCode} does not reject 31 when subtracting`, |
| 139 | + "shaka", 1943); |
| 140 | +}); |
| 141 | + |
| 142 | +[ |
| 143 | + [months7n, 7, "M07"], |
| 144 | + [months6n, 8, "M08"], |
| 145 | + [months5n, 9, "M09"], |
| 146 | + [months4n, 10, "M10"], |
| 147 | + [months3n, 11, "M11"], |
| 148 | + [months2n, 12, "M12"], |
| 149 | +].forEach(function ([months, month, monthCode]) { |
| 150 | + TemporalHelpers.assertPlainDate( |
| 151 | + common0231.subtract(months), |
| 152 | + 1943, month, monthCode, 30, `common-year ${monthCode} constrains to 30 when subtracting`, |
| 153 | + "shaka", 1943); |
| 154 | + assert.throws(RangeError, function () { |
| 155 | + common0231.subtract(months, options); |
| 156 | + }, `common-year ${monthCode} rejects 31 when adding`); |
| 157 | +}); |
| 158 | + |
| 159 | +// Leap year, backwards |
| 160 | + |
| 161 | +[ |
| 162 | + [months13n, 1, "M01"], |
| 163 | + [months12n, 2, "M02"], |
| 164 | + [months11n, 3, "M03"], |
| 165 | + [months10n, 4, "M04"], |
| 166 | + [months9n, 5, "M05"], |
| 167 | + [months8n, 6, "M06"], |
| 168 | +].forEach(function ([months, month, monthCode]) { |
| 169 | + TemporalHelpers.assertPlainDate( |
| 170 | + common0231After.subtract(months, options), |
| 171 | + 1946, month, monthCode, 31, `leap-year ${monthCode} does not reject 31 when subtracting`, |
| 172 | + "shaka", 1946); |
| 173 | +}); |
| 174 | + |
| 175 | +[ |
| 176 | + [months7n, 7, "M07"], |
| 177 | + [months6n, 8, "M08"], |
| 178 | + [months5n, 9, "M09"], |
| 179 | + [months4n, 10, "M10"], |
| 180 | + [months3n, 11, "M11"], |
| 181 | + [months2n, 12, "M12"], |
| 182 | +].forEach(function ([months, month, monthCode]) { |
| 183 | + TemporalHelpers.assertPlainDate( |
| 184 | + common0231After.subtract(months), |
| 185 | + 1946, month, monthCode, 30, `leap-year ${monthCode} constrains to 30 when subtracting`, |
| 186 | + "shaka", 1946); |
| 187 | + assert.throws(RangeError, function () { |
| 188 | + common0231After.subtract(months, options); |
| 189 | + }, `leap-year ${monthCode} rejects 31 when adding`); |
| 190 | +}); |
0 commit comments