Skip to content

Commit c55bdc4

Browse files
authored
Implement the remaining non-ISO calendar method calls (#209)
Some of the method calls into `icu_calendar` were not fully implemented / active. This implements the methods that currently can be implemented and marks the methods that either need to be implemented or need more research completed with a TODO.
1 parent 25d45b2 commit c55bdc4

File tree

1 file changed

+26
-36
lines changed

1 file changed

+26
-36
lines changed

src/builtins/core/calendar.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
//! The goal of the calendar module of `boa_temporal` is to provide
44
//! Temporal compatible calendar implementations.
55
6-
// TODO: It may finally be time to clean up API to use `IsoDate` and `DateDuration` directly.
7-
8-
use alloc::string::String;
9-
use alloc::vec::Vec;
10-
use core::str::FromStr;
11-
use icu_calendar::types::{Era as IcuEra, MonthCode as IcuMonthCode, MonthInfo, YearInfo};
12-
use types::ResolveType;
13-
146
use crate::{
157
builtins::core::{
168
duration::{DateDuration, TimeDuration},
@@ -21,7 +13,9 @@ use crate::{
2113
parsers::parse_allowed_calendar_formats,
2214
TemporalError, TemporalResult,
2315
};
16+
use core::str::FromStr;
2417

18+
use icu_calendar::types::{Era as IcuEra, MonthCode as IcuMonthCode, MonthInfo, YearInfo};
2519
use icu_calendar::{
2620
any_calendar::AnyDateInner,
2721
cal::{
@@ -41,8 +35,8 @@ use super::{PartialDate, ZonedDateTime};
4135
mod era;
4236
mod types;
4337

44-
pub use types::ResolvedCalendarFields;
4538
pub(crate) use types::{ascii_four_to_integer, month_to_month_code};
39+
pub use types::{ResolveType, ResolvedCalendarFields};
4640

4741
use era::EraInfo;
4842

@@ -364,7 +358,6 @@ impl Calendar {
364358
let date_duration = one.diff_iso_date(two, largest_unit)?;
365359
return Ok(Duration::from(date_duration));
366360
}
367-
368361
Err(TemporalError::range().with_message("Not yet implemented."))
369362
}
370363

@@ -400,56 +393,56 @@ impl Calendar {
400393
if self.is_iso() {
401394
return Ok(iso_date.month);
402395
}
403-
404-
Err(TemporalError::range().with_message("Not yet implemented."))
396+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
397+
Ok(self.0.month(&calendar_date).month_number())
405398
}
406399

407400
/// `CalendarMonthCode`
408401
pub fn month_code(&self, iso_date: &IsoDate) -> TemporalResult<TinyAsciiStr<4>> {
409402
if self.is_iso() {
410403
return Ok(iso_date.as_icu4x()?.month().standard_code.0);
411404
}
412-
413-
Err(TemporalError::range().with_message("Not yet implemented."))
405+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
406+
Ok(self.0.month(&calendar_date).standard_code.0)
414407
}
415408

416409
/// `CalendarDay`
417410
pub fn day(&self, iso_date: &IsoDate) -> TemporalResult<u8> {
418411
if self.is_iso() {
419412
return Ok(iso_date.day);
420413
}
421-
422-
Err(TemporalError::range().with_message("Not yet implemented."))
414+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
415+
Ok(self.0.day_of_month(&calendar_date).0)
423416
}
424417

425418
/// `CalendarDayOfWeek`
426419
pub fn day_of_week(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
427420
if self.is_iso() {
428421
return Ok(iso_date.as_icu4x()?.day_of_week() as u16);
429422
}
430-
431-
Err(TemporalError::range().with_message("Not yet implemented."))
423+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
424+
// TODO: Understand ICU4X's decision for `IsoWeekDay` to be `i8`
425+
Ok(self.0.day_of_week(&calendar_date) as u16)
432426
}
433427

434428
/// `CalendarDayOfYear`
435429
pub fn day_of_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
436430
if self.is_iso() {
437431
return Ok(iso_date.as_icu4x()?.day_of_year_info().day_of_year);
438432
}
439-
Err(TemporalError::range().with_message("Not yet implemented."))?
433+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
434+
Ok(self.0.day_of_year_info(&calendar_date).day_of_year)
440435
}
441436

442437
/// `CalendarWeekOfYear`
443438
pub fn week_of_year(&self, iso_date: &IsoDate) -> TemporalResult<Option<u16>> {
444439
if self.is_iso() {
445440
let date = iso_date.as_icu4x()?;
446-
447441
let week_calculator = WeekCalculator::default();
448-
449442
let week_of = date.week_of_year(&week_calculator);
450-
451443
return Ok(Some(week_of.week as u16));
452444
}
445+
// TODO: Research in ICU4X and determine best approach.
453446
Err(TemporalError::range().with_message("Not yet implemented."))
454447
}
455448

@@ -468,6 +461,7 @@ impl Calendar {
468461
RelativeUnit::Next => Ok(Some(date.year().extended_year + 1)),
469462
};
470463
}
464+
// TODO: Research in ICU4X and determine best approach.
471465
Err(TemporalError::range().with_message("Not yet implemented."))
472466
}
473467

@@ -476,6 +470,7 @@ impl Calendar {
476470
if self.is_iso() {
477471
return Ok(7);
478472
}
473+
// TODO: Research in ICU4X and determine best approach.
479474
Err(TemporalError::range().with_message("Not yet implemented."))
480475
}
481476

@@ -484,40 +479,35 @@ impl Calendar {
484479
if self.is_iso() {
485480
return Ok(iso_date.as_icu4x()?.days_in_month() as u16);
486481
}
487-
Err(TemporalError::range().with_message("Not yet implemented."))
482+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
483+
Ok(self.0.days_in_month(&calendar_date) as u16)
488484
}
489485

490486
/// `CalendarDaysInYear`
491487
pub fn days_in_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
492488
if self.is_iso() {
493489
return Ok(iso_date.as_icu4x()?.days_in_year());
494490
}
495-
496-
Err(TemporalError::range().with_message("Not yet implemented."))
491+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
492+
Ok(self.0.days_in_year(&calendar_date))
497493
}
498494

499495
/// `CalendarMonthsInYear`
500-
pub fn months_in_year(&self, _iso_date: &IsoDate) -> TemporalResult<u16> {
496+
pub fn months_in_year(&self, iso_date: &IsoDate) -> TemporalResult<u16> {
501497
if self.is_iso() {
502498
return Ok(12);
503499
}
504-
Err(TemporalError::range().with_message("Not yet implemented."))
500+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
501+
Ok(self.0.months_in_year(&calendar_date) as u16)
505502
}
506503

507504
/// `CalendarInLeapYear`
508505
pub fn in_leap_year(&self, iso_date: &IsoDate) -> TemporalResult<bool> {
509506
if self.is_iso() {
510507
return Ok(iso_date.as_icu4x()?.is_in_leap_year());
511508
}
512-
Err(TemporalError::range().with_message("Not yet implemented."))
513-
}
514-
515-
/// `CalendarFields`
516-
pub fn fields(&self, fields: Vec<String>) -> TemporalResult<Vec<String>> {
517-
if self.is_iso() {
518-
return Ok(fields);
519-
}
520-
Err(TemporalError::range().with_message("Not yet implemented."))
509+
let calendar_date = self.0.date_from_iso(iso_date.as_icu4x()?);
510+
Ok(self.0.is_in_leap_year(&calendar_date))
521511
}
522512

523513
/// Returns the identifier of this calendar slot.

0 commit comments

Comments
 (0)