Skip to content

Commit

Permalink
Added handling of out-of-range numbers and leap years. (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
niyari committed Mar 29, 2022
1 parent cf075a7 commit ff8dd15
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ internal int UtcYear
get => _utcDateTime.Year;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
ResetUtcLeapYear(value);
_utcDateTime = new DateTime(value, UtcMonth, UtcDay, UtcHour, UtcMinute, UtcSecond, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -63,6 +68,11 @@ internal int UtcMonth
get => _utcDateTime.Month;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
ResetUtcLeapYear(UtcYear);
_utcDateTime = new DateTime(UtcYear, value, UtcDay, UtcHour, UtcMinute, UtcSecond, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -74,6 +84,17 @@ internal int UtcDay
get => _utcDateTime.Day;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
if (UtcMonth == 2 && value > 28)
{
if (value != 29 || !DateTime.IsLeapYear(UtcYear))
{
return;
}
}
_utcDateTime = new DateTime(UtcYear, UtcMonth, value, UtcHour, UtcMinute, UtcSecond, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -85,6 +106,10 @@ internal int UtcHour
get => _utcDateTime.Hour;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
_utcDateTime = new DateTime(UtcYear, UtcMonth, UtcDay, value, UtcMinute, UtcSecond, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -96,6 +121,10 @@ internal int UtcMinute
get => _utcDateTime.Minute;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
_utcDateTime = new DateTime(UtcYear, UtcMonth, UtcDay, UtcHour, value, UtcSecond, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -107,6 +136,10 @@ internal int UtcSecond
get => _utcDateTime.Second;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
_utcDateTime = new DateTime(UtcYear, UtcMonth, UtcDay, UtcHour, UtcMinute, value, DateTimeKind.Utc);
ResetLocalDateTime();
ResetTimestamp();
Expand All @@ -118,6 +151,11 @@ internal int LocalYear
get => _localDateTime.Value.Year;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
ResetLocalLeapYear(value);
var localDateTime = new DateTime(value, LocalMonth, LocalDay, LocalHour, LocalMinute, LocalSecond, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand All @@ -131,6 +169,11 @@ internal int LocalMonth
get => _localDateTime.Value.Month;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
ResetLocalLeapYear(LocalYear);
var localDateTime = new DateTime(LocalYear, value, LocalDay, LocalHour, LocalMinute, LocalSecond, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand All @@ -144,6 +187,17 @@ internal int LocalDay
get => _localDateTime.Value.Day;
set
{
if (value < 1) // empty = -2147483648
{
return;
}
if (LocalMonth == 2 && value > 28)
{
if (value != 29 || !DateTime.IsLeapYear(LocalYear))
{
return;
}
}
var localDateTime = new DateTime(LocalYear, LocalMonth, value, LocalHour, LocalMinute, LocalSecond, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand All @@ -157,6 +211,10 @@ internal int LocalHour
get => _localDateTime.Value.Hour;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
var localDateTime = new DateTime(LocalYear, LocalMonth, LocalDay, value, LocalMinute, LocalSecond, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand All @@ -170,6 +228,10 @@ internal int LocalMinute
get => _localDateTime.Value.Minute;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
var localDateTime = new DateTime(LocalYear, LocalMonth, LocalDay, LocalHour, value, LocalSecond, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand All @@ -183,6 +245,10 @@ internal int LocalSecond
get => _localDateTime.Value.Second;
set
{
if (value < 0) // empty = -2147483648
{
return;
}
var localDateTime = new DateTime(LocalYear, LocalMonth, LocalDay, LocalHour, LocalMinute, value, DateTimeKind.Local);
_timestamp = new DateTimeOffset(localDateTime.ToUniversalTime()).ToUnixTimeSeconds();
ResetUtcDateTime();
Expand Down Expand Up @@ -290,5 +356,21 @@ private void ResetTimestamp()
_timestamp = new DateTimeOffset(_utcDateTime).ToUnixTimeSeconds();
OnPropertyChanged(nameof(Timestamp));
}

private void ResetUtcLeapYear(int setValue) {
if (UtcDay > 28 && UtcMonth == 2 && !DateTime.IsLeapYear(setValue))
{
UtcDay = 28;
}
}

private void ResetLocalLeapYear(int setValue)
{
if (LocalDay > 28 && LocalMonth == 2 && !DateTime.IsLeapYear(setValue))
{
LocalDay = 28;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
SpinButtonPlacementMode="Compact"
SmallChange="1"
LargeChange="10"
Minimum="0"
Minimum="1"
Maximum="9999"
AutomationProperties.LabeledBy="{Binding ElementName=UtcYearHeaderTextBlock}"/>
</StackPanel>
Expand Down Expand Up @@ -221,7 +221,7 @@
SpinButtonPlacementMode="Compact"
SmallChange="1"
LargeChange="10"
Minimum="0"
Minimum="1"
Maximum="9999"
AutomationProperties.LabeledBy="{Binding ElementName=YearHeaderTextBlock}"/>
</StackPanel>
Expand Down

0 comments on commit ff8dd15

Please sign in to comment.