Skip to content

Commit 24bac4e

Browse files
Arithmetic API review (unicode-org#7762)
## Changelog: N/A --------- Co-authored-by: Manish Goregaokar <manishsmail@gmail.com>
1 parent aedc827 commit 24bac4e

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

components/calendar/src/cal/hebrew.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use calendrical_calculations::rata_die::RataDie;
4545
/// Due to Rosh Hashanah postponement rules, Ḥešvan and Kislev vary in length.
4646
///
4747
/// In leap years (years 3, 6, 8, 11, 17, 19 in a 19-year cycle), the leap month Adar I (`M05L`, 30 days)
48-
/// is inserted before Adar, and Adar is called Adar II (the `formatting_code` returned by [`MonthInfo`]
49-
/// will be `M06L` to mark this, while the `standard_code` remains `M06`).
48+
/// is inserted before Adar (`M06`), which is then called Adar II ([`MonthInfo::leap_status`] will be
49+
/// [`LeapStatus::LeapBase`] to mark this).
5050
///
5151
/// Standard years thus have 353-355 days, and leap years 383-385.
5252
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Default)]

components/calendar/src/date.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ impl<C> Deref for Ref<'_, C> {
110110
/// by changing the era/calendar. Furthermore, this is not the case for [`Date::try_from_fields`] and
111111
/// date arithmetic APIs: these APIs let you construct dates outside of that range.
112112
///
113-
/// `Date` types have a fundamental range invariant as well, and ICU4X will refuse to construct
113+
/// The `Date` type has a fundamental range invariant as well, and it's not possible to construct
114114
/// dates outside of that range, regardless of the calendar.
115-
/// ICU4X APIs will return an `Overflow` error (e.g. `DateAddError::Overflow`) in these cases, or clamp
115+
/// APIs will return an `Overflow` error (e.g. `DateAddError::Overflow`) in these cases, or clamp
116116
/// in the case of `Date::from_rata_die()`.
117117
///
118118
/// This range is currently dates with an ISO year between `-999_999..=999_999`, but
119-
/// ICU4X reserves the right to change these bounds in the future.
119+
/// we reserve the right to change these bounds in the future.
120120
///
121121
/// Since `icu_calendar` is intended to be usable by implementors of the ECMA Temporal specification,
122122
/// this range will never be smaller than Temporal's validity range, which roughly maps to ISO years
@@ -338,7 +338,7 @@ impl<A: AsCalendar> Date<A> {
338338
self.calendar.as_calendar().months_in_year(self.inner())
339339
}
340340

341-
/// Add a `duration` to this date, mutating it
341+
/// Add a `duration` to this [`Date`], mutating it.
342342
///
343343
/// This API will not construct dates outside of the fundamental range described on the [`Date`] type,
344344
/// instead returning [`DateAddError::Overflow`].
@@ -366,15 +366,11 @@ impl<A: AsCalendar> Date<A> {
366366
Ok(())
367367
}
368368

369-
/// Add a `duration` to this date, returning the new one.
369+
/// Add a `duration` to this [`Date`].
370370
///
371371
/// This API will not construct dates outside of the fundamental range described on the [`Date`] type,
372372
/// instead returning [`DateAddError::Overflow`].
373373
///
374-
/// This clones the calendar: the calendars in this crate are cheap to clone (and usually `Copy`), but
375-
/// the `A` wrapper you are using may not be. Consider using a wrapper like [`Ref`] for `A` if the cloning
376-
/// will be a problem.
377-
///
378374
/// <div class="stab unstable">
379375
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
380376
/// including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.

components/calendar/src/duration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ pub struct DateDuration {
118118
/// Whether the duration is negative.
119119
///
120120
/// A negative duration is an abstract concept that could result, for example, from
121-
/// taking the difference between two [`Date`](crate::Date)s.
121+
/// taking the difference between two [`Date`](crate::Date)s in ascending order.
122122
///
123123
/// The fields of the duration are either all positive or all negative. Mixed signs
124-
/// are not allowed.
124+
/// are not possible.
125125
///
126126
/// By convention, this field should be `false` if the duration is zero.
127127
pub is_negative: bool,

components/calendar/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ mod unstable {
451451
/// fields.day = Some(31);
452452
///
453453
/// let err = Date::try_from_fields(fields, Default::default(), Iso)
454-
/// .expect_err("no day 31 in November");
454+
/// .expect_err("date out of range");
455455
///
456456
/// assert!(matches!(
457457
/// err,

components/calendar/src/options.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ mod unstable {
175175
pub struct DateDifferenceOptions {
176176
/// Which date field to allow as the largest in a duration when taking the difference.
177177
///
178+
/// This defaults to [`DateDurationUnit::Days`].
179+
///
178180
/// When choosing [`Months`] or [`Years`], the resulting [`DateDuration`] might not be
179181
/// associative or commutative in subsequent arithmetic operations, and it might require
180182
/// [`Overflow::Constrain`] in addition.
@@ -408,7 +410,7 @@ mod unstable {
408410
/// This strategy makes the following changes:
409411
///
410412
/// 1. If the fields identify a year and a month, but not a day, then set `day` to 1.
411-
/// 2. If `month_code` and `day` are set but nothing else, then set the year to a
413+
/// 2. If month and day are set but nothing else, then set the year to a
412414
/// _reference year_: some year in the calendar that contains the specified month
413415
/// and day, according to the ECMAScript specification.
414416
///

components/calendar/src/types.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,23 +460,21 @@ impl fmt::Display for MonthCode {
460460
///
461461
/// A month has a "number" and "leap flag". In calendars without leap months (non-lunisolar
462462
/// calendars), the month with number n is always the nth month of the year (_ordinal month_),
463-
/// for example the Gregorian September is `Month:new(9)` and the 9th month of the year.
463+
/// for example the Gregorian September is `Month::new(9)` and the 9th month of the year.
464464
/// However, in calendars with leap months (lunisolar calendars), such as the Hebrew calendar,
465465
/// a month might repeat (leap) without affecting the number of each subsequent month (but
466466
/// obviously affecting their _ordinal number_). For example, the Hebrew month Nisan
467467
/// (`Month::new(7)`) might be the 7th or 8th month of the year, depending if the month
468468
/// Adar was repeated or not.
469469
///
470-
/// Check the docs for a particular calendar for details on what its months are.
470+
/// Check the docs for a particular calendar (e.g. [`Hebrew`](crate::cal::Hebrew)) for details on
471+
/// what its months are.
471472
///
472473
/// This concept of months matches the "month code" in [Temporal], and borrows its string
473474
/// representation:
474475
/// * `Month::new(7)` = `M07`
475476
/// * `Month::leap(2)` = `M02L`
476477
///
477-
/// This means that the Hebrew months "Adar" and "Adar II"
478-
/// ("Adar, but during a leap year") are considered the same month, `Month::new(6)`.
479-
///
480478
/// [Temporal]: https://tc39.es/proposal-intl-era-monthcode/
481479
#[derive(Copy, Clone, Debug, PartialEq, Hash, Eq, PartialOrd)]
482480
pub struct Month {

0 commit comments

Comments
 (0)