Skip to content

Commit

Permalink
fix(calendar): Fixed a potential crash when SetToMin()/SetToMax()
Browse files Browse the repository at this point in the history
… were used in multi-eras calendars
  • Loading branch information
carldebilly committed Jun 3, 2021
1 parent c9a73aa commit 9549557
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ public async Task CanSelectOutOfRangeDate()

rootPanel = await CalendarHelper.CreateTestResources();



// load into visual tree
await RunOnUIThread(() =>
{
Expand Down
49 changes: 49 additions & 0 deletions src/Uno.UI.Tests/Windows_Globalization/When_Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,5 +695,54 @@ string dayOfWeekAsString
SUT.DayOfWeekAsString().Should().Be(dayOfWeekAsString, nameof(dayOfWeekAsString));
}
}

[TestMethod]
[DataRow(WG.CalendarIdentifiers.JulianValue)]
[DataRow(WG.CalendarIdentifiers.GregorianValue)]
[DataRow(WG.CalendarIdentifiers.HebrewValue)]
[DataRow(WG.CalendarIdentifiers.HijriValue)]
[DataRow(WG.CalendarIdentifiers.JapaneseValue)]
[DataRow(WG.CalendarIdentifiers.KoreanValue)]
[DataRow(WG.CalendarIdentifiers.TaiwanValue)]
[DataRow(WG.CalendarIdentifiers.ThaiValue)]
[DataRow(WG.CalendarIdentifiers.UmAlQuraValue)]
[DataRow(WG.CalendarIdentifiers.PersianValue)]
[DataRow(WG.CalendarIdentifiers.ChineseLunarValue)]
[DataRow(WG.CalendarIdentifiers.VietnameseLunarValue)]
[DataRow(WG.CalendarIdentifiers.TaiwanLunarValue)]
[DataRow(WG.CalendarIdentifiers.KoreanLunarValue)]
[DataRow(WG.CalendarIdentifiers.JapaneseLunarValue)]
public void TestCalendarLimits(string calendar)
{
using var _ = new AssertionScope();

var sut = new WG.Calendar(new [] {"en"}, WG.CalendarIdentifiers.Japanese, "24HourClock");

sut.SetToMin();
CheckLimits($"Min");

sut.SetToMax();
CheckLimits($"Max");

sut.SetToNow();
CheckLimits($"Max");

void CheckLimits(string context)
{
// Following tests are just to ensure no exceptions are raised
// by asking those values
sut.NumberOfEras.Should().BePositive(context);
sut.Era.Should().BePositive(context);
sut.FirstMonthInThisYear.Should().BePositive(context);
sut.NumberOfDaysInThisMonth.Should().BePositive(context);
sut.DayOfWeek.Should().NotBeNull(context);
sut.NumberOfEras.Should().BePositive(context);
sut.FirstEra.Should().BePositive(context);
sut.LastEra.Should().BePositive(context);
sut.Period.Should().BePositive(context);
sut.ResolvedLanguage.Should().NotBeEmpty(context);
}
}
}
}

20 changes: 10 additions & 10 deletions src/Uno.UWP/Globalization/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,35 @@ public void ChangeTimeZone(string timeZoneId)
#region Read / Write _time
public int Era
{
get => _calendar.GetEra(_time.DateTime);
get => _calendar.GetEra(_time.UtcDateTime);
[NotImplemented] set => global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.Globalization.Calendar", "int Calendar.Era");
}

public int Year
{
get => _calendar.GetYear(_time.DateTime);
get => _calendar.GetYear(_time.UtcDateTime);
set => AddYears(value - Year);
}

public int Month
{
get => _calendar.GetMonth(_time.DateTime);
get => _calendar.GetMonth(_time.UtcDateTime);
set => AddMonths(value - Month);
}

public global::Windows.Globalization.DayOfWeek DayOfWeek => (global::Windows.Globalization.DayOfWeek)_calendar.GetDayOfWeek(_time.DateTime);
public global::Windows.Globalization.DayOfWeek DayOfWeek => (global::Windows.Globalization.DayOfWeek)_calendar.GetDayOfWeek(_time.UtcDateTime);

public int Day
{
get => _calendar.GetDayOfMonth(_time.DateTime);
get => _calendar.GetDayOfMonth(_time.UtcDateTime);
set => AddDays(value - Day);
}

public int Hour
{
get
{
var hour = _calendar.GetHour(_time.DateTime);
var hour = _calendar.GetHour(_time.UtcDateTime);

if(hour < 12 || _clock == ClockIdentifiers.TwentyFourHour)
{
Expand All @@ -233,13 +233,13 @@ public int Hour

public int Minute
{
get => _calendar.GetMinute(_time.DateTime);
get => _calendar.GetMinute(_time.UtcDateTime);
set => AddMinutes(value - Minute);
}

public int Second
{
get => _calendar.GetSecond(_time.DateTime);
get => _calendar.GetSecond(_time.UtcDateTime);
set => AddSeconds(value - Second);
}

Expand Down Expand Up @@ -278,15 +278,15 @@ public int Period

public int Nanosecond
{
get => (int)(_calendar.GetMilliseconds(_time.DateTime) * 1000);
get => (int)(_calendar.GetMilliseconds(_time.UtcDateTime) * 1000);
set => AddNanoseconds(value - Nanosecond);
}

public void SetDateTime(global::System.DateTimeOffset value)
=> _time = value;

public void SetToNow()
=> _time = DateTime.Now;
=> _time = DateTimeOffset.Now;

internal void SetToday() // Useful helper not part of UWP contract
=> _time = DateTime.Today;
Expand Down

0 comments on commit 9549557

Please sign in to comment.