8
8
//! use icu::calendar::{cal::Chinese, Date};
9
9
//!
10
10
//! let chinese = Chinese::new();
11
- //! let chinese_date = Date::try_new_chinese_with_calendar(4660 , 6, 6, chinese)
11
+ //! let chinese_date = Date::try_new_chinese_with_calendar(2023 , 6, 6, chinese)
12
12
//! .expect("Failed to initialize Chinese Date instance.");
13
13
//!
14
- //! assert_eq!(chinese_date.year().era_year_or_extended (), 4660 );
14
+ //! assert_eq!(chinese_date.year().era_year_or_related_iso (), 2023 );
15
15
//! assert_eq!(chinese_date.year().cyclic().unwrap().get(), 40);
16
16
//! assert_eq!(chinese_date.month().ordinal, 6);
17
17
//! assert_eq!(chinese_date.day_of_month().0, 6);
@@ -28,6 +28,7 @@ use crate::error::DateError;
28
28
use crate :: provider:: chinese_based:: CalendarChineseV1 ;
29
29
use crate :: AsCalendar ;
30
30
use crate :: { types, Calendar , Date , DateDuration , DateDurationUnit } ;
31
+ use calendrical_calculations:: chinese_based:: { self , ChineseBased } ;
31
32
use calendrical_calculations:: rata_die:: RataDie ;
32
33
use core:: cmp:: Ordering ;
33
34
use core:: num:: NonZeroU8 ;
@@ -211,7 +212,7 @@ impl Calendar for Chinese {
211
212
Iso . from_rata_die ( self . to_rata_die ( date) )
212
213
}
213
214
214
- //Count the number of months in a given year, specified by providing a date
215
+ // Count the number of months in a given year, specified by providing a date
215
216
// from that year
216
217
fn days_in_year ( & self , date : & Self :: DateInner ) -> u16 {
217
218
date. 0 . days_in_year_inner ( )
@@ -250,11 +251,13 @@ impl Calendar for Chinese {
250
251
251
252
fn year ( & self , date : & Self :: DateInner ) -> types:: YearInfo {
252
253
let year = date. 0 . 0 . year ;
253
- let cyclic = ( year. value - 1 ) . rem_euclid ( 60 ) as u8 ;
254
+ let cyclic = ( year. related_iso - 4 ) . rem_euclid ( 60 ) as u8 ;
254
255
let cyclic = NonZeroU8 :: new ( cyclic + 1 ) . unwrap_or ( NonZeroU8 :: MIN ) ; // 1-indexed
255
- let rata_die_in_year = date. 0 . 0 . year . new_year :: < ChineseCB > ( ) ;
256
- let related_iso = Iso . from_rata_die ( rata_die_in_year) . 0 . year ;
257
- types:: YearInfo :: new_cyclic ( year. value , cyclic, related_iso)
256
+ types:: YearInfo :: new_cyclic (
257
+ chinese_based:: Chinese :: extended_from_iso ( year. related_iso ) ,
258
+ cyclic,
259
+ year. related_iso ,
260
+ )
258
261
}
259
262
260
263
fn is_in_leap_year ( & self , date : & Self :: DateInner ) -> bool {
@@ -290,7 +293,7 @@ impl Calendar for Chinese {
290
293
291
294
impl < A : AsCalendar < Calendar = Chinese > > Date < A > {
292
295
/// Construct a new Chinese date from a `year`, `month`, and `day`.
293
- /// `year` represents the Chinese year counted infinitely with -2636 (2637 BCE) as Chinese year 1 ;
296
+ /// `year` represents the [ISO](crate::Iso) year that roughly matches the Chinese year;
294
297
/// `month` represents the month of the year ordinally (ex. if it is a leap year, the last month will be 13, not 12);
295
298
/// `day` indicates the day of month
296
299
///
@@ -303,25 +306,25 @@ impl<A: AsCalendar<Calendar = Chinese>> Date<A> {
303
306
/// let chinese = Chinese::new_always_calculating();
304
307
///
305
308
/// let date_chinese =
306
- /// Date::try_new_chinese_with_calendar(4660 , 6, 11, chinese)
309
+ /// Date::try_new_chinese_with_calendar(2023 , 6, 11, chinese)
307
310
/// .expect("Failed to initialize Chinese Date instance.");
308
311
///
309
- /// assert_eq!(date_chinese.year().era_year_or_extended (), 4660 );
312
+ /// assert_eq!(date_chinese.year().era_year_or_related_iso (), 2023 );
310
313
/// assert_eq!(date_chinese.year().cyclic().unwrap().get(), 40);
311
314
/// assert_eq!(date_chinese.year().related_iso(), Some(2023));
312
315
/// assert_eq!(date_chinese.month().ordinal, 6);
313
316
/// assert_eq!(date_chinese.day_of_month().0, 11);
314
317
/// ```
315
318
pub fn try_new_chinese_with_calendar (
316
- year : i32 ,
319
+ related_iso_year : i32 ,
317
320
month : u8 ,
318
321
day : u8 ,
319
322
calendar : A ,
320
323
) -> Result < Date < A > , DateError > {
321
324
let year = calendar
322
325
. as_calendar ( )
323
326
. get_precomputed_data ( )
324
- . load_or_compute_info ( year ) ;
327
+ . load_or_compute_info ( related_iso_year ) ;
325
328
let arithmetic = Inner :: new_from_ordinals ( year, month, day) ;
326
329
Ok ( Date :: from_raw (
327
330
ChineseDateInner ( ChineseBasedDateInner ( arithmetic?) ) ,
@@ -456,23 +459,25 @@ mod test {
456
459
let chinese_cached = Chinese :: new ( ) ;
457
460
for case in cases {
458
461
let rata_die = RataDie :: new ( case. rd ) ;
459
- let iso = Iso . from_rata_die ( rata_die) ;
460
462
461
463
do_twice (
462
464
& chinese_calculating,
463
465
& chinese_cached,
464
466
|chinese, calendar_type| {
465
- let chinese = Inner :: chinese_based_date_from_rd ( chinese . 0 , rata_die, iso . 0 ) ;
467
+ let chinese = Date :: from_rata_die ( rata_die, chinese ) ;
466
468
assert_eq ! (
467
- case. expected_year, chinese. 0 . year. value,
469
+ case. expected_year,
470
+ chinese. year( ) . extended_year,
468
471
"[{calendar_type}] Chinese from RD failed, case: {case:?}"
469
472
) ;
470
473
assert_eq ! (
471
- case. expected_month, chinese. 0 . month,
474
+ case. expected_month,
475
+ chinese. month( ) . ordinal,
472
476
"[{calendar_type}] Chinese from RD failed, case: {case:?}"
473
477
) ;
474
478
assert_eq ! (
475
- case. expected_day, chinese. 0 . day,
479
+ case. expected_day,
480
+ chinese. day_of_month( ) . 0 ,
476
481
"[{calendar_type}] Chinese from RD failed, case: {case:?}"
477
482
) ;
478
483
} ,
@@ -492,14 +497,14 @@ mod test {
492
497
493
498
let cases = [
494
499
TestCase {
495
- year : 4660 ,
500
+ year : 2023 ,
496
501
month : 6 ,
497
502
day : 6 ,
498
503
// June 23 2023
499
504
expected : 738694 ,
500
505
} ,
501
506
TestCase {
502
- year : 1 ,
507
+ year : - 2636 ,
503
508
month : 1 ,
504
509
day : 1 ,
505
510
expected : -963099 ,
@@ -562,7 +567,7 @@ mod test {
562
567
|chinese, _calendar_type| {
563
568
let chinese = iso. to_calendar ( chinese) ;
564
569
565
- assert_eq ! ( chinese. year( ) . era_year_or_extended ( ) , 1 ) ;
570
+ assert_eq ! ( chinese. year( ) . era_year_or_related_iso ( ) , - 2636 ) ;
566
571
assert_eq ! ( chinese. month( ) . ordinal, 1 ) ;
567
572
assert_eq ! ( chinese. month( ) . standard_code. 0 , "M01" ) ;
568
573
assert_eq ! ( chinese. day_of_month( ) . 0 , 1 ) ;
@@ -589,15 +594,15 @@ mod test {
589
594
iso_year : -2636 ,
590
595
iso_month : 2 ,
591
596
iso_day : 14 ,
592
- expected_year : 0 ,
597
+ expected_year : - 2637 ,
593
598
expected_month : 13 ,
594
599
expected_day : 30 ,
595
600
} ,
596
601
TestCase {
597
602
iso_year : -2636 ,
598
603
iso_month : 1 ,
599
604
iso_day : 15 ,
600
- expected_year : 0 ,
605
+ expected_year : - 2637 ,
601
606
expected_month : 12 ,
602
607
expected_day : 30 ,
603
608
} ,
@@ -615,7 +620,7 @@ mod test {
615
620
let chinese = iso. to_calendar ( chinese) ;
616
621
assert_eq ! (
617
622
case. expected_year,
618
- chinese. year( ) . era_year_or_extended ( ) ,
623
+ chinese. year( ) . era_year_or_related_iso ( ) ,
619
624
"[{calendar_type}] ISO to Chinese failed for case: {case:?}"
620
625
) ;
621
626
assert_eq ! (
@@ -674,10 +679,9 @@ mod test {
674
679
675
680
#[ test]
676
681
fn test_month_days ( ) {
677
- let year = 4660 ;
678
682
let year =
679
683
ChineseBasedPrecomputedData :: < <Chinese as ChineseBasedWithDataLoading >:: CB > :: default ( )
680
- . load_or_compute_info ( year ) ;
684
+ . load_or_compute_info ( 2023 ) ;
681
685
let cases = [
682
686
( 1 , 29 ) ,
683
687
( 2 , 30 ) ,
@@ -831,7 +835,7 @@ mod test {
831
835
// construct using ::default() to force recomputation
832
836
let year =
833
837
ChineseBasedPrecomputedData :: < <Chinese as ChineseBasedWithDataLoading >:: CB > :: default ( )
834
- . load_or_compute_info ( 4660 ) ;
838
+ . load_or_compute_info ( 2023 ) ;
835
839
let codes = [
836
840
( 1 , tinystr ! ( 4 , "M01" ) ) ,
837
841
( 2 , tinystr ! ( 4 , "M02" ) ) ,
@@ -854,7 +858,7 @@ mod test {
854
858
ordinal,
855
859
Some ( ordinal_code_pair. 0 ) ,
856
860
"Code to ordinal failed for year: {}, code: {code}" ,
857
- year. value
861
+ year. related_iso
858
862
) ;
859
863
}
860
864
}
@@ -884,7 +888,7 @@ mod test {
884
888
assert_eq ! (
885
889
ordinal, None ,
886
890
"Invalid month code failed for year: {}, code: {code}" ,
887
- year. value
891
+ year. related_iso
888
892
) ;
889
893
}
890
894
}
0 commit comments