From 34f21acef61ea2fd65725b1f55b07f6b212f59f2 Mon Sep 17 00:00:00 2001 From: yui10 <79691301+yui10@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:43:40 +0900 Subject: [PATCH] Fixed a bug in date/time conversion when special input is used. (#54) * fix: Bug fixes in date/time boundary values * fix: Bug fixes in date/time decimals --- src/DevToys.Tools/Helpers/DateHelper.cs | 29 ++++++++++++------- .../Converters/Date/DateConverterGuiTool.cs | 17 +++++++++-- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/DevToys.Tools/Helpers/DateHelper.cs b/src/DevToys.Tools/Helpers/DateHelper.cs index 49bcbc7198..fac3f2e970 100644 --- a/src/DevToys.Tools/Helpers/DateHelper.cs +++ b/src/DevToys.Tools/Helpers/DateHelper.cs @@ -55,18 +55,25 @@ internal static ResultInfo ChangeDateTime( DateValueType valueChanged) { target = TimeZoneInfo.ConvertTime(target, timeZoneInfo); - DateTimeOffset result = valueChanged switch + try { - DateValueType.Year => ChangeYear(target, value), - DateValueType.Month => target.AddMonths(value - target.Month), - DateValueType.Day => target.AddDays(value - target.Day), - DateValueType.Hour => target.AddHours(value - target.Hour), - DateValueType.Minute => target.AddMinutes(value - target.Minute), - DateValueType.Second => target.AddSeconds(value - target.Second), - DateValueType.Millisecond => target.AddMilliseconds(value - target.Millisecond), - _ => throw new NotImplementedException(), - }; - return new(result, true); + DateTimeOffset result = valueChanged switch + { + DateValueType.Year => ChangeYear(target, value), + DateValueType.Month => target.AddMonths(value - target.Month), + DateValueType.Day => target.AddDays(value - target.Day), + DateValueType.Hour => target.AddHours(value - target.Hour), + DateValueType.Minute => target.AddMinutes(value - target.Minute), + DateValueType.Second => target.AddSeconds(value - target.Second), + DateValueType.Millisecond => target.AddMilliseconds(value - target.Millisecond), + _ => throw new NotImplementedException(), + }; + return new(result, true); + } + catch (ArgumentOutOfRangeException) + { + return new(target, false); + } } private static DateTimeOffset ChangeYear(DateTimeOffset target, int value) diff --git a/src/DevToys.Tools/Tools/Converters/Date/DateConverterGuiTool.cs b/src/DevToys.Tools/Tools/Converters/Date/DateConverterGuiTool.cs index ce8781203c..3cf040ddb0 100644 --- a/src/DevToys.Tools/Tools/Converters/Date/DateConverterGuiTool.cs +++ b/src/DevToys.Tools/Tools/Converters/Date/DateConverterGuiTool.cs @@ -311,9 +311,15 @@ private void OnTimeChanged(string value, DateValueType valueChanged) TimeZoneInfo timeZoneInfo = GetSelectedTimeZone(); DateTimeOffset currentTime = _settingsProvider.GetSetting(DateConverterGuiTool.currentTimeSettings); + if (!int.TryParse(value, out int parsedValue)) + { + _errorInfoBar.Description(DateConverter.InvalidValue); + _errorInfoBar.Open(); + return; + } ResultInfo result = DateHelper.ChangeDateTime( - Convert.ToInt32(value), + parsedValue, currentTime, timeZoneInfo, valueChanged); @@ -338,8 +344,15 @@ private void OnEpochChanged(string value, DateValueType valueChanged) DateTimeOffset epochToUse = _settingsProvider.GetSetting(DateConverterGuiTool.customEpochSettings); + if (!int.TryParse(value, out int parsedValue)) + { + _errorInfoBar.Description(DateConverter.InvalidValue); + _errorInfoBar.Open(); + return; + } + ResultInfo result = DateHelper.ChangeDateTime( - Convert.ToInt32(value), + parsedValue, epochToUse, TimeZoneInfo.Utc, valueChanged);