1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally
2 // Copyright (C) 2024 Igalia, S.L. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
6 esid: sec-temporal.plaindate.prototype.since
7 description: Tests balancing of days to months at end of month
8 includes: [temporalHelpers.js]
12 // Difference between end of longer month to end of following shorter month
14 const end = new Temporal.PlainDate(1970, 2, 28);
15 for (const largestUnit of ["years", "months"]) {
16 TemporalHelpers.assertDuration(
17 new Temporal.PlainDate(1970, 1, 28).since(end, { largestUnit }),
18 0, -1, 0, 0, 0, 0, 0, 0, 0, 0,
19 "Jan 28th to Feb 28th is one month"
21 TemporalHelpers.assertDuration(
22 new Temporal.PlainDate(1970, 1, 29).since(end, { largestUnit }),
23 0, 0, 0, -30, 0, 0, 0, 0, 0, 0,
24 "Jan 29th to Feb 28th is 30 days, not one month"
26 TemporalHelpers.assertDuration(
27 new Temporal.PlainDate(1970, 1, 30).since(end, { largestUnit }),
28 0, 0, 0, -29, 0, 0, 0, 0, 0, 0,
29 "Jan 30th to Feb 28th is 29 days, not one month"
31 TemporalHelpers.assertDuration(
32 new Temporal.PlainDate(1970, 1, 31).since(end, { largestUnit }),
33 0, 0, 0, -28, 0, 0, 0, 0, 0, 0,
34 "Jan 31st to Feb 28th is 28 days, not one month"
39 // Difference between end of leap-year January to end of leap-year February
41 const end = new Temporal.PlainDate(1972, 2, 29);
42 for (const largestUnit of ["years", "months"]) {
43 TemporalHelpers.assertDuration(
44 new Temporal.PlainDate(1972, 1, 29).since(end, { largestUnit }),
45 0, -1, 0, 0, 0, 0, 0, 0, 0, 0,
46 "Jan 29th to Feb 29th is one month"
48 TemporalHelpers.assertDuration(
49 new Temporal.PlainDate(1972, 1, 30).since(end, { largestUnit }),
50 0, 0, 0, -30, 0, 0, 0, 0, 0, 0,
51 "Jan 30th to Feb 29th is 30 days, not one month"
53 TemporalHelpers.assertDuration(
54 new Temporal.PlainDate(1972, 1, 31).since(end, { largestUnit }),
55 0, 0, 0, -29, 0, 0, 0, 0, 0, 0,
56 "Jan 31st to Feb 29th is 29 days, not one month"
61 // Difference between end of longer month to end of not-immediately-following
64 const end = new Temporal.PlainDate(1970, 11, 30);
65 for (const largestUnit of ["years", "months"]) {
66 TemporalHelpers.assertDuration(
67 new Temporal.PlainDate(1970, 8, 30).since(end, { largestUnit }),
68 0, -3, 0, 0, 0, 0, 0, 0, 0, 0,
69 "Aug 30th to Nov 30th is 3 months"
71 TemporalHelpers.assertDuration(
72 new Temporal.PlainDate(1970, 8, 31).since(end, { largestUnit }),
73 0, -2, 0, -30, 0, 0, 0, 0, 0, 0,
74 "Aug 31st to Nov 30th is 2 months 30 days, not 3 months"
79 // Difference between end of longer month in one year to shorter month in
82 const end = new Temporal.PlainDate(1973, 4, 30);
83 TemporalHelpers.assertDuration(
84 new Temporal.PlainDate(1970, 12, 30).since(end, { largestUnit: "months" }),
85 0, -28, 0, 0, 0, 0, 0, 0, 0, 0,
86 "Dec 30th 1970 to Apr 30th 1973 is 28 months"
88 TemporalHelpers.assertDuration(
89 new Temporal.PlainDate(1970, 12, 30).since(end, { largestUnit: "years" }),
90 -2, -4, 0, 0, 0, 0, 0, 0, 0, 0,
91 "Dec 30th 1970 to Apr 30th 1973 is 2 years, 4 months"
93 TemporalHelpers.assertDuration(
94 new Temporal.PlainDate(1970, 12, 31).since(end, { largestUnit: "months" }),
95 0, -27, 0, -30, 0, 0, 0, 0, 0, 0,
96 "Dec 30th 1970 to Apr 30th 1973 is 27 months, 30 days, not 28 months"
98 TemporalHelpers.assertDuration(
99 new Temporal.PlainDate(1970, 12, 31).since(end, { largestUnit: "years" }),
100 -2, -3, 0, -30, 0, 0, 0, 0, 0, 0,
101 "Dec 30th 1970 to Apr 30th 1973 is 2 years, 3 months, 30 days, not 2 years 4 months"
105 // Difference where months passes through a month that's the same length or
106 // shorter than either the start or end month
108 TemporalHelpers.assertDuration(
109 new Temporal.PlainDate(1970, 1, 29).since(new Temporal.PlainDate(1970, 3, 28), { largestUnit: "months" }),
110 0, -1, 0, -28, 0, 0, 0, 0, 0, 0,
111 "Jan 29th to Mar 28th is 1 month 28 days, not 58 days"
113 TemporalHelpers.assertDuration(
114 new Temporal.PlainDate(1970, 1, 31).since(new Temporal.PlainDate(1971, 5, 30), { largestUnit: "years" }),
115 -1, -3, 0, -30, 0, 0, 0, 0, 0, 0,
116 "Jan 31st 1970 to May 30th 1971 is 1 year, 3 months, 30 days, not 1 year, 2 months, 60 days"