Change no-year sentinel value to 0004

This allows setting the date "Feb 29" with no year, since the year 0004 is a leap year in the Gregorian calendar, while the year 0001 isn't.
This commit is contained in:
Ske
2020-02-09 22:36:02 +01:00
parent 9394b14a38
commit 30ed293dc6
3 changed files with 10 additions and 5 deletions

View File

@@ -128,7 +128,8 @@ namespace PluralKit
public PrivacyLevel MemberPrivacy { get; set; }
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" is hidden
/// Returns a formatted string representing the member's birthday, taking into account that a year of "0001" or "0004" is hidden
/// Before Feb 10 2020, the sentinel year was 0001, now it is 0004.
[JsonIgnore] public string BirthdayString
{
get
@@ -136,7 +137,7 @@ namespace PluralKit
if (Birthday == null) return null;
var format = LocalDatePattern.CreateWithInvariantCulture("MMM dd, yyyy");
if (Birthday?.Year == 1) format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
if (Birthday?.Year == 1 || Birthday?.Year == 4) format = LocalDatePattern.CreateWithInvariantCulture("MMM dd");
return format.Format(Birthday.Value);
}
}

View File

@@ -105,10 +105,12 @@ namespace PluralKit
"MM/dd" // 01/01
});
// Giving a template value so year will be parsed as 0001 if not present
// Giving a template value so year will be parsed as 0004 if not present
// This means we can later disambiguate whether a null year was given
// We use the basis year 0004 (rather than, say, 0001) because 0004 is a leap year in the Gregorian calendar
// which means the date "Feb 29, 0004" is a valid date. 0001 is still accepted as a null year for legacy reasons.
// TODO: should we be using invariant culture here?
foreach (var pattern in patterns.Select(p => LocalDatePattern.CreateWithInvariantCulture(p).WithTemplateValue(new LocalDate(0001, 1, 1))))
foreach (var pattern in patterns.Select(p => LocalDatePattern.CreateWithInvariantCulture(p).WithTemplateValue(new LocalDate(0004, 1, 1))))
{
var result = pattern.Parse(str);
if (result.Success) return result.Value;