/// <summary> /// Work out the year, based on fields of: /// - Year /// - YearOfEra /// - YearTwoDigits (implies YearOfEra) /// - Era /// /// If the year is specified, that trumps everything else - any other fields /// are just used for checking. /// /// If nothing is specified, the year of the template value is used. /// /// If just the era is specified, the year of the template value is used, /// and the specified era is checked against it. (Hopefully no-one will /// expect to get useful information from a format string with era but no year...) /// /// Otherwise, we have the year of era (possibly only two digits) and possibly the /// era. If the era isn't specified, take it from the template value. /// Finally, if we only have two digits, then use either the century of the template /// value or the previous century if the year-of-era is greater than TwoDigitYearMax... /// and if the template value isn't in the first century already. /// /// Phew. /// </summary> private ParseResult <LocalDate> DetermineYear(PatternFields usedFields, string text) { if (usedFields.HasAny(PatternFields.Year)) { if (Year > Calendar.MaxYear || Year < Calendar.MinYear) { return(ParseResult <LocalDate> .FieldValueOutOfRangePostParse(text, Year, 'u')); } if (usedFields.HasAny(PatternFields.Era) && Era != Calendar.GetEra(Year)) { return(ParseResult <LocalDate> .InconsistentValues(text, 'g', 'u')); } if (usedFields.HasAny(PatternFields.YearOfEra)) { int yearOfEraFromYear = Calendar.GetYearOfEra(Year); if (usedFields.HasAny(PatternFields.YearTwoDigits)) { // We're only checking the last two digits yearOfEraFromYear = yearOfEraFromYear % 100; } if (yearOfEraFromYear != YearOfEra) { return(ParseResult <LocalDate> .InconsistentValues(text, 'y', 'u')); } } return(null); } // Use the year from the template value, possibly checking the era. if (!usedFields.HasAny(PatternFields.YearOfEra)) { Year = TemplateValue.Year; return(usedFields.HasAny(PatternFields.Era) && Era != Calendar.GetEra(Year) ? ParseResult <LocalDate> .InconsistentValues(text, 'g', 'u') : null); } if (!usedFields.HasAny(PatternFields.Era)) { Era = TemplateValue.Era; } if (usedFields.HasAny(PatternFields.YearTwoDigits)) { int century = TemplateValue.YearOfEra / 100; if (YearOfEra > TwoDigitYearMax && century > 1) { century--; } YearOfEra += century * 100; } if (YearOfEra < Calendar.GetMinYearOfEra(Era) || YearOfEra > Calendar.GetMaxYearOfEra(Era)) { return(ParseResult <LocalDate> .YearOfEraOutOfRange(text, YearOfEra, Era, Calendar)); } Year = Calendar.GetAbsoluteYear(YearOfEra, Era); return(null); }