Skip to content

Commit

Permalink
Fixed a bug in date/time conversion when special input is used. (#54)
Browse files Browse the repository at this point in the history
* fix: Bug fixes in date/time boundary values

* fix: Bug fixes in date/time decimals
  • Loading branch information
yui10 authored Aug 24, 2024
1 parent a4035f0 commit 34f21ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
29 changes: 18 additions & 11 deletions src/DevToys.Tools/Helpers/DateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ internal static ResultInfo<DateTimeOffset> 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)
Expand Down
17 changes: 15 additions & 2 deletions src/DevToys.Tools/Tools/Converters/Date/DateConverterGuiTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateTimeOffset> result = DateHelper.ChangeDateTime(
Convert.ToInt32(value),
parsedValue,
currentTime,
timeZoneInfo,
valueChanged);
Expand All @@ -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<DateTimeOffset> result = DateHelper.ChangeDateTime(
Convert.ToInt32(value),
parsedValue,
epochToUse,
TimeZoneInfo.Utc,
valueChanged);
Expand Down

0 comments on commit 34f21ac

Please sign in to comment.