@@ -16,7 +16,7 @@ public enum DataType
1616 Month = 2 ,
1717 Day = 8
1818 }
19-
19+
2020 public static Calendar [ ] s_calendars = new Calendar [ ]
2121 {
2222 new ThaiBuddhistCalendar ( ) ,
@@ -55,6 +55,25 @@ private static int MaxCalendarYearInEra(Calendar calendar, int era)
5555 return calendar . GetYear ( calendar . ToDateTime ( 1 , 1 , 1 , 0 , 0 , 0 , 0 , era + 1 ) . AddDays ( - 1 ) ) + 1 ;
5656 }
5757
58+ // Get the max year in the passed era plus the sum of the max year for each subsequent era
59+ private static int MaxCalendarYearInEras ( Calendar calendar , int era )
60+ {
61+ int [ ] eras = calendar . Eras ;
62+ Assert . InRange ( era , 0 , eras [ 0 ] ) ;
63+ if ( eras . Length == 1 || era == eras [ 0 ] || era == 0 )
64+ {
65+ return MaxCalendarYearInEra ( calendar , era ) ;
66+ }
67+
68+ int year = 0 ;
69+ for ( int i = era ; i <= calendar . Eras [ 0 ] ; i ++ )
70+ {
71+ year += MaxCalendarYearInEra ( calendar , i ) ;
72+ }
73+
74+ return year ;
75+ }
76+
5877 private static int MaxGregorianYearInEra ( Calendar calendar , int era )
5978 {
6079 int [ ] eras = calendar . Eras ;
@@ -92,7 +111,8 @@ public static IEnumerable<object[]> Calendars_TestData()
92111 {
93112 foreach ( Calendar calendar in s_calendars )
94113 {
95- yield return new object [ ] { calendar } ;
114+ if ( ! ( calendar is JapaneseLunisolarCalendar ) || ! PlatformDetection . IsFullFramework )
115+ yield return new object [ ] { calendar } ;
96116 }
97117 }
98118
@@ -102,12 +122,12 @@ public static IEnumerable<object[]> Year_Month_Day_Era_TestData(DataType type, b
102122 int day = 1 ;
103123 foreach ( Calendar calendar in s_calendars )
104124 {
105- if ( ignoreJapaneseLunisolarCalendar && calendar is JapaneseLunisolarCalendar )
125+ if ( calendar is JapaneseLunisolarCalendar && ( ignoreJapaneseLunisolarCalendar || PlatformDetection . IsFullFramework ) )
106126 {
107127 // desktop has a bug in JapaneseLunisolarCalendar which is fixed in .Net Core.
108128 // in case of a new era starts in the middle of a month which means part of the month will belong to one
109- // era and the rest will belong to the new era. When calculating the calendar year number for dates which
110- // in the rest of the month and exist in the new started era, we should still use the old era info instead
129+ // era and the rest will belong to the new era. When calculating the calendar year number for dates which
130+ // in the rest of the month and exist in the new started era, we should still use the old era info instead
111131 // of the new era info because the rest of the month still belong to the year of last era.
112132 // https://github.com/dotnet/coreclr/pull/3662
113133 continue ;
@@ -119,7 +139,8 @@ public static IEnumerable<object[]> Year_Month_Day_Era_TestData(DataType type, b
119139 // Year is invalid
120140 yield return new object [ ] { calendar , - 1 , month , day , era , "year" } ;
121141 yield return new object [ ] { calendar , 0 , month , day , era , "year" } ;
122- yield return new object [ ] { calendar , MaxCalendarYearInEra ( calendar , era ) + 1 , month , day , era , "year" } ;
142+
143+ yield return new object [ ] { calendar , MaxCalendarYearInEras ( calendar , era ) + 1 , month , day , era , "year" } ;
123144
124145 if ( ( type & DataType . Month ) != 0 )
125146 {
@@ -158,7 +179,7 @@ public static IEnumerable<object[]> DateTime_TestData()
158179 DateTime maxDate = calendar . MaxSupportedDateTime ;
159180 if ( maxDate != DateTime . MaxValue )
160181 {
161- yield return new object [ ] { calendar , maxDate . AddDays ( 1 ) } ;
182+ yield return new object [ ] { calendar , maxDate . AddDays ( 1 ) } ;
162183 }
163184 }
164185 }
@@ -334,7 +355,7 @@ public static void ToDateTime_Invalid(Calendar calendar)
334355 // Year is invalid
335356 Assert . Throws < ArgumentOutOfRangeException > ( ( ) => calendar . ToDateTime ( - 1 , month , day , hour , minute , second , millisecond , era ) ) ;
336357 Assert . Throws < ArgumentOutOfRangeException > ( ( ) => calendar . ToDateTime ( 0 , month , day , hour , minute , second , millisecond , era ) ) ;
337- Assert . Throws < ArgumentOutOfRangeException > ( ( ) => calendar . ToDateTime ( MaxCalendarYearInEra ( calendar , era ) + 1 , month , day , hour , minute , second , millisecond , era ) ) ;
358+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => calendar . ToDateTime ( MaxCalendarYearInEras ( calendar , era ) + 1 , month , day , hour , minute , second , millisecond , era ) ) ;
338359
339360 // Month is invalid
340361 Assert . Throws < ArgumentOutOfRangeException > ( ( ) => calendar . ToDateTime ( year , - 1 , day , hour , minute , second , millisecond , era ) ) ;
@@ -388,7 +409,7 @@ public static void ToFourDigitYear_Invalid(Calendar calendar)
388409 {
389410 AssertExtensions . Throws < ArgumentOutOfRangeException > ( "year" , ( ) => calendar . ToFourDigitYear ( - 1 ) ) ;
390411 AssertExtensions . Throws < ArgumentOutOfRangeException > ( "year" , ( ) => calendar . ToFourDigitYear ( MaxCalendarYearInEra ( calendar , MaxEra ( calendar ) ) + 1 ) ) ;
391-
412+
392413 if ( ! ( calendar is JapaneseLunisolarCalendar ) )
393414 {
394415 AssertExtensions . Throws < ArgumentOutOfRangeException > ( "year" , ( ) => calendar . ToFourDigitYear ( MinCalendarYearInEra ( calendar , MinEra ( calendar ) ) - 2 ) ) ;
@@ -460,5 +481,16 @@ public static void GetDayOfWeek_Invalid(Calendar calendar, DateTime dt)
460481 AssertExtensions . Throws < ArgumentOutOfRangeException > ( "time" , ( ) => calendar . GetDayOfWeek ( dt ) ) ;
461482 }
462483 }
484+
485+ [ Fact ]
486+ [ SkipOnTargetFramework ( TargetFrameworkMonikers . NetFramework ) ]
487+ public static void TestJapaneseCalendarDateParsing ( )
488+ {
489+ CultureInfo ciJapanese = new CultureInfo ( "ja-JP" ) { DateTimeFormat = { Calendar = new JapaneseCalendar ( ) } } ;
490+
491+ DateTime dt = new DateTime ( 1970 , 1 , 1 ) ;
492+ string eraName = dt . ToString ( "gg" , ciJapanese ) ;
493+ Assert . Equal ( new DateTime ( 1995 , 1 , 1 ) , DateTime . Parse ( eraName + " 70/1/1 0:00:00" , ciJapanese ) ) ;
494+ }
463495 }
464496}
0 commit comments