3
3
//! The goal of the calendar module of `boa_temporal` is to provide
4
4
//! Temporal compatible calendar implementations.
5
5
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
-
14
6
use crate :: {
15
7
builtins:: core:: {
16
8
duration:: { DateDuration , TimeDuration } ,
@@ -21,7 +13,9 @@ use crate::{
21
13
parsers:: parse_allowed_calendar_formats,
22
14
TemporalError , TemporalResult ,
23
15
} ;
16
+ use core:: str:: FromStr ;
24
17
18
+ use icu_calendar:: types:: { Era as IcuEra , MonthCode as IcuMonthCode , MonthInfo , YearInfo } ;
25
19
use icu_calendar:: {
26
20
any_calendar:: AnyDateInner ,
27
21
cal:: {
@@ -41,8 +35,8 @@ use super::{PartialDate, ZonedDateTime};
41
35
mod era;
42
36
mod types;
43
37
44
- pub use types:: ResolvedCalendarFields ;
45
38
pub ( crate ) use types:: { ascii_four_to_integer, month_to_month_code} ;
39
+ pub use types:: { ResolveType , ResolvedCalendarFields } ;
46
40
47
41
use era:: EraInfo ;
48
42
@@ -364,7 +358,6 @@ impl Calendar {
364
358
let date_duration = one. diff_iso_date ( two, largest_unit) ?;
365
359
return Ok ( Duration :: from ( date_duration) ) ;
366
360
}
367
-
368
361
Err ( TemporalError :: range ( ) . with_message ( "Not yet implemented." ) )
369
362
}
370
363
@@ -400,56 +393,56 @@ impl Calendar {
400
393
if self . is_iso ( ) {
401
394
return Ok ( iso_date. month ) ;
402
395
}
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 ( ) )
405
398
}
406
399
407
400
/// `CalendarMonthCode`
408
401
pub fn month_code ( & self , iso_date : & IsoDate ) -> TemporalResult < TinyAsciiStr < 4 > > {
409
402
if self . is_iso ( ) {
410
403
return Ok ( iso_date. as_icu4x ( ) ?. month ( ) . standard_code . 0 ) ;
411
404
}
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 )
414
407
}
415
408
416
409
/// `CalendarDay`
417
410
pub fn day ( & self , iso_date : & IsoDate ) -> TemporalResult < u8 > {
418
411
if self . is_iso ( ) {
419
412
return Ok ( iso_date. day ) ;
420
413
}
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 )
423
416
}
424
417
425
418
/// `CalendarDayOfWeek`
426
419
pub fn day_of_week ( & self , iso_date : & IsoDate ) -> TemporalResult < u16 > {
427
420
if self . is_iso ( ) {
428
421
return Ok ( iso_date. as_icu4x ( ) ?. day_of_week ( ) as u16 ) ;
429
422
}
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 )
432
426
}
433
427
434
428
/// `CalendarDayOfYear`
435
429
pub fn day_of_year ( & self , iso_date : & IsoDate ) -> TemporalResult < u16 > {
436
430
if self . is_iso ( ) {
437
431
return Ok ( iso_date. as_icu4x ( ) ?. day_of_year_info ( ) . day_of_year ) ;
438
432
}
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 )
440
435
}
441
436
442
437
/// `CalendarWeekOfYear`
443
438
pub fn week_of_year ( & self , iso_date : & IsoDate ) -> TemporalResult < Option < u16 > > {
444
439
if self . is_iso ( ) {
445
440
let date = iso_date. as_icu4x ( ) ?;
446
-
447
441
let week_calculator = WeekCalculator :: default ( ) ;
448
-
449
442
let week_of = date. week_of_year ( & week_calculator) ;
450
-
451
443
return Ok ( Some ( week_of. week as u16 ) ) ;
452
444
}
445
+ // TODO: Research in ICU4X and determine best approach.
453
446
Err ( TemporalError :: range ( ) . with_message ( "Not yet implemented." ) )
454
447
}
455
448
@@ -468,6 +461,7 @@ impl Calendar {
468
461
RelativeUnit :: Next => Ok ( Some ( date. year ( ) . extended_year + 1 ) ) ,
469
462
} ;
470
463
}
464
+ // TODO: Research in ICU4X and determine best approach.
471
465
Err ( TemporalError :: range ( ) . with_message ( "Not yet implemented." ) )
472
466
}
473
467
@@ -476,6 +470,7 @@ impl Calendar {
476
470
if self . is_iso ( ) {
477
471
return Ok ( 7 ) ;
478
472
}
473
+ // TODO: Research in ICU4X and determine best approach.
479
474
Err ( TemporalError :: range ( ) . with_message ( "Not yet implemented." ) )
480
475
}
481
476
@@ -484,40 +479,35 @@ impl Calendar {
484
479
if self . is_iso ( ) {
485
480
return Ok ( iso_date. as_icu4x ( ) ?. days_in_month ( ) as u16 ) ;
486
481
}
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 )
488
484
}
489
485
490
486
/// `CalendarDaysInYear`
491
487
pub fn days_in_year ( & self , iso_date : & IsoDate ) -> TemporalResult < u16 > {
492
488
if self . is_iso ( ) {
493
489
return Ok ( iso_date. as_icu4x ( ) ?. days_in_year ( ) ) ;
494
490
}
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 ) )
497
493
}
498
494
499
495
/// `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 > {
501
497
if self . is_iso ( ) {
502
498
return Ok ( 12 ) ;
503
499
}
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 )
505
502
}
506
503
507
504
/// `CalendarInLeapYear`
508
505
pub fn in_leap_year ( & self , iso_date : & IsoDate ) -> TemporalResult < bool > {
509
506
if self . is_iso ( ) {
510
507
return Ok ( iso_date. as_icu4x ( ) ?. is_in_leap_year ( ) ) ;
511
508
}
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) )
521
511
}
522
512
523
513
/// Returns the identifier of this calendar slot.
0 commit comments