Skip to content

Commit f2b335c

Browse files
authored
Integrate MonthCode into public API and related adjustments (#208)
This change integrate `MonthCode` into the public API of the partial fields and `month_code` methods in order to better represent [PrepareCalendarFields](https://tc39.es/proposal-temporal/#sec-temporal-preparecalendarfields) conversion. This does also add a host of public APIs to `MonthCode` for interacting with it and adjusts the general validation methods (although, I do suspect this will need to be iterated on in the future to better support more calendars). CC: @Manishearth as this includes updates to `temporal_capi`
1 parent c55bdc4 commit f2b335c

File tree

16 files changed

+217
-148
lines changed

16 files changed

+217
-148
lines changed

src/builtins/compiled/zoneddatetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
ArithmeticOverflow, DifferenceSettings, Disambiguation, DisplayCalendar, DisplayOffset,
66
DisplayTimeZone, OffsetDisambiguation, ToStringRoundingOptions,
77
},
8-
Duration, PlainDate, PlainDateTime, PlainTime, TemporalError, TemporalResult,
8+
Duration, MonthCode, PlainDate, PlainDateTime, PlainTime, TemporalError, TemporalResult,
99
};
1010
use alloc::string::String;
1111
use tinystr::TinyAsciiStr;
@@ -58,7 +58,7 @@ impl ZonedDateTime {
5858
/// Returns the `ZonedDateTime`'s calendar month code.
5959
///
6060
/// Enable with the `compiled_data` feature flag.
61-
pub fn month_code(&self) -> TemporalResult<TinyAsciiStr<4>> {
61+
pub fn month_code(&self) -> TemporalResult<MonthCode> {
6262
let provider = TZ_PROVIDER
6363
.lock()
6464
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;

src/builtins/core/calendar.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use super::{PartialDate, ZonedDateTime};
3535
mod era;
3636
mod types;
3737

38-
pub(crate) use types::{ascii_four_to_integer, month_to_month_code};
39-
pub use types::{ResolveType, ResolvedCalendarFields};
38+
pub(crate) use types::{month_to_month_code, ResolutionType};
39+
pub use types::{MonthCode, ResolvedCalendarFields};
4040

4141
use era::EraInfo;
4242

@@ -222,13 +222,13 @@ impl Calendar {
222222
overflow: ArithmeticOverflow,
223223
) -> TemporalResult<PlainDate> {
224224
let resolved_fields =
225-
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolveType::Date)?;
225+
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolutionType::Date)?;
226226

227227
if self.is_iso() {
228228
// Resolve month and monthCode;
229229
return PlainDate::new_with_overflow(
230230
resolved_fields.era_year.year,
231-
resolved_fields.month_code.as_iso_month_integer()?,
231+
resolved_fields.month_code.to_month_integer(),
232232
resolved_fields.day,
233233
self.clone(),
234234
overflow,
@@ -261,10 +261,10 @@ impl Calendar {
261261
overflow: ArithmeticOverflow,
262262
) -> TemporalResult<PlainMonthDay> {
263263
let resolved_fields =
264-
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolveType::MonthDay)?;
264+
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolutionType::MonthDay)?;
265265
if self.is_iso() {
266266
return PlainMonthDay::new_with_overflow(
267-
resolved_fields.month_code.as_iso_month_integer()?,
267+
resolved_fields.month_code.to_month_integer(),
268268
resolved_fields.day,
269269
self.clone(),
270270
overflow,
@@ -284,11 +284,11 @@ impl Calendar {
284284
overflow: ArithmeticOverflow,
285285
) -> TemporalResult<PlainYearMonth> {
286286
let resolved_fields =
287-
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolveType::YearMonth)?;
287+
ResolvedCalendarFields::try_from_partial(partial, overflow, ResolutionType::YearMonth)?;
288288
if self.is_iso() {
289289
return PlainYearMonth::new_with_overflow(
290290
resolved_fields.era_year.year,
291-
resolved_fields.month_code.as_iso_month_integer()?,
291+
resolved_fields.month_code.to_month_integer(),
292292
Some(resolved_fields.day),
293293
self.clone(),
294294
overflow,
@@ -398,12 +398,13 @@ impl Calendar {
398398
}
399399

400400
/// `CalendarMonthCode`
401-
pub fn month_code(&self, iso_date: &IsoDate) -> TemporalResult<TinyAsciiStr<4>> {
401+
pub fn month_code(&self, iso_date: &IsoDate) -> TemporalResult<MonthCode> {
402402
if self.is_iso() {
403-
return Ok(iso_date.as_icu4x()?.month().standard_code.0);
403+
let mc = iso_date.as_icu4x()?.month().standard_code.0;
404+
return Ok(MonthCode(mc));
404405
}
405406
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
406-
Ok(self.0.month(&calendar_date).standard_code.0)
407+
Ok(MonthCode(self.0.month(&calendar_date).standard_code.0))
407408
}
408409

409410
/// `CalendarDay`

0 commit comments

Comments
 (0)