diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Converters/Timestamp/TimestampToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Converters/Timestamp/TimestampToolViewModel.cs index 351cf0a36c..92f1818628 100644 --- a/src/dev/impl/DevToys/ViewModels/Tools/Converters/Timestamp/TimestampToolViewModel.cs +++ b/src/dev/impl/DevToys/ViewModels/Tools/Converters/Timestamp/TimestampToolViewModel.cs @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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(); @@ -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; + } + } + } } diff --git a/src/dev/impl/DevToys/Views/Tools/Converters/Timestamp/TimestampToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/Converters/Timestamp/TimestampToolPage.xaml index 9faea16b22..3eed8a1577 100644 --- a/src/dev/impl/DevToys/Views/Tools/Converters/Timestamp/TimestampToolPage.xaml +++ b/src/dev/impl/DevToys/Views/Tools/Converters/Timestamp/TimestampToolPage.xaml @@ -100,7 +100,7 @@ SpinButtonPlacementMode="Compact" SmallChange="1" LargeChange="10" - Minimum="0" + Minimum="1" Maximum="9999" AutomationProperties.LabeledBy="{Binding ElementName=UtcYearHeaderTextBlock}"/> @@ -221,7 +221,7 @@ SpinButtonPlacementMode="Compact" SmallChange="1" LargeChange="10" - Minimum="0" + Minimum="1" Maximum="9999" AutomationProperties.LabeledBy="{Binding ElementName=YearHeaderTextBlock}"/>