diff --git a/PKHeX.Core/MysteryGifts/PGF.cs b/PKHeX.Core/MysteryGifts/PGF.cs index 7f8d7555a..bf69b9882 100644 --- a/PKHeX.Core/MysteryGifts/PGF.cs +++ b/PKHeX.Core/MysteryGifts/PGF.cs @@ -106,7 +106,7 @@ public override string CardTitle get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!DateUtil.IsDateValid(Year, Month, Day)) return null; return new DateTime(Year, Month, Day); diff --git a/PKHeX.Core/MysteryGifts/WB7.cs b/PKHeX.Core/MysteryGifts/WB7.cs index 575e1eb7a..a78132a9d 100644 --- a/PKHeX.Core/MysteryGifts/WB7.cs +++ b/PKHeX.Core/MysteryGifts/WB7.cs @@ -79,7 +79,7 @@ private uint Day get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!DateUtil.IsDateValid(Year, Month, Day)) return null; return new DateTime((int)Year, (int)Month, (int)Day); diff --git a/PKHeX.Core/MysteryGifts/WC6.cs b/PKHeX.Core/MysteryGifts/WC6.cs index 7a95bcec2..91a5099b7 100644 --- a/PKHeX.Core/MysteryGifts/WC6.cs +++ b/PKHeX.Core/MysteryGifts/WC6.cs @@ -78,7 +78,7 @@ private uint Day get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!DateUtil.IsDateValid(Year, Month, Day)) return null; return new DateTime((int)Year, (int)Month, (int)Day); diff --git a/PKHeX.Core/MysteryGifts/WC7.cs b/PKHeX.Core/MysteryGifts/WC7.cs index 5b8220691..efa224218 100644 --- a/PKHeX.Core/MysteryGifts/WC7.cs +++ b/PKHeX.Core/MysteryGifts/WC7.cs @@ -77,7 +77,7 @@ private uint Day get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!DateUtil.IsDateValid(Year, Month, Day)) return null; return new DateTime((int)Year, (int)Month, (int)Day); diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index f5e7c9981..9049ed12d 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -155,7 +155,7 @@ private byte[] Write() get { // Check to see if date is valid - if (!Util.IsDateValid(2000 + Met_Year, Met_Month, Met_Day)) + if (!DateUtil.IsDateValid(2000 + Met_Year, Met_Month, Met_Day)) return null; return new DateTime(2000 + Met_Year, Met_Month, Met_Day); } @@ -198,7 +198,7 @@ private byte[] Write() get { // Check to see if date is valid - if (!Util.IsDateValid(2000 + Egg_Year, Egg_Month, Egg_Day)) + if (!DateUtil.IsDateValid(2000 + Egg_Year, Egg_Month, Egg_Day)) return null; return new DateTime(2000 + Egg_Year, Egg_Month, Egg_Day); } diff --git a/PKHeX.Core/Saves/Substructures/Battle Videos/BV6.cs b/PKHeX.Core/Saves/Substructures/Battle Videos/BV6.cs index 73ce1a7cb..4bb8f320b 100644 --- a/PKHeX.Core/Saves/Substructures/Battle Videos/BV6.cs +++ b/PKHeX.Core/Saves/Substructures/Battle Videos/BV6.cs @@ -133,7 +133,7 @@ public void SetTeam(IReadOnlyList team, int t) { get { - if (!Util.IsDateValid(MatchYear, MatchMonth, MatchDay)) + if (!DateUtil.IsDateValid(MatchYear, MatchMonth, MatchDay)) return null; return new DateTime(MatchYear, MatchMonth, MatchDay, MatchHour, MatchMinute, MatchSecond); } @@ -159,7 +159,7 @@ public void SetTeam(IReadOnlyList team, int t) { get { - if (!Util.IsDateValid(UploadYear, UploadMonth, UploadDay)) + if (!DateUtil.IsDateValid(UploadYear, UploadMonth, UploadDay)) return null; return new DateTime(UploadYear, UploadMonth, UploadDay, UploadHour, UploadMinute, UploadSecond); } diff --git a/PKHeX.Core/Saves/Substructures/Battle Videos/BV7.cs b/PKHeX.Core/Saves/Substructures/Battle Videos/BV7.cs index f3f37f5be..394d0def8 100644 --- a/PKHeX.Core/Saves/Substructures/Battle Videos/BV7.cs +++ b/PKHeX.Core/Saves/Substructures/Battle Videos/BV7.cs @@ -93,7 +93,7 @@ public void SetPlayerNames(IReadOnlyList value) { get { - if (!Util.IsDateValid(MatchYear, MatchMonth, MatchDay)) + if (!DateUtil.IsDateValid(MatchYear, MatchMonth, MatchDay)) return null; return new DateTime(MatchYear, MatchMonth, MatchDay, MatchHour, MatchMinute, MatchSecond); } diff --git a/PKHeX.Core/Saves/Substructures/Gen6/PlayTime6.cs b/PKHeX.Core/Saves/Substructures/Gen6/PlayTime6.cs index a5cf8abe4..0749c60ee 100644 --- a/PKHeX.Core/Saves/Substructures/Gen6/PlayTime6.cs +++ b/PKHeX.Core/Saves/Substructures/Gen6/PlayTime6.cs @@ -34,7 +34,7 @@ public int PlayedSeconds public DateTime? LastSavedDate { - get => !Util.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) + get => !DateUtil.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) ? (DateTime?)null : new DateTime(LastSavedYear, LastSavedMonth, LastSavedDay, LastSavedHour, LastSavedMinute, 0); set diff --git a/PKHeX.Core/Saves/Substructures/Gen7/JoinFesta7.cs b/PKHeX.Core/Saves/Substructures/Gen7/JoinFesta7.cs index fa5454a28..e95a2be7b 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/JoinFesta7.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/JoinFesta7.cs @@ -59,7 +59,7 @@ public void SetFestaPhraseUnlocked(int index, bool value) public DateTime? FestaDate { - get => FestaYear >= 0 && FestaMonth > 0 && FestaDay > 0 && FestaHour >= 0 && FestaMinute >= 0 && FestaSecond >= 0 && Util.IsDateValid(FestaYear, FestaMonth, FestaDay) + get => FestaYear >= 0 && FestaMonth > 0 && FestaDay > 0 && FestaHour >= 0 && FestaMinute >= 0 && FestaSecond >= 0 && DateUtil.IsDateValid(FestaYear, FestaMonth, FestaDay) ? new DateTime(FestaYear, FestaMonth, FestaDay, FestaHour, FestaMinute, FestaSecond) : (DateTime?)null; set diff --git a/PKHeX.Core/Saves/Substructures/Gen7/PlayTime7b.cs b/PKHeX.Core/Saves/Substructures/Gen7/PlayTime7b.cs index cdda93e92..a4e6a3c4d 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/PlayTime7b.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/PlayTime7b.cs @@ -34,7 +34,7 @@ public int PlayedSeconds public DateTime? LastSavedDate { - get => !Util.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) + get => !DateUtil.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) ? (DateTime?)null : new DateTime(LastSavedYear, LastSavedMonth, LastSavedDay, LastSavedHour, LastSavedMinute, 0); set diff --git a/PKHeX.Core/Saves/Substructures/Gen8/PlayTime8.cs b/PKHeX.Core/Saves/Substructures/Gen8/PlayTime8.cs index df5cad4c8..b0f40e5ee 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/PlayTime8.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/PlayTime8.cs @@ -35,7 +35,7 @@ public int PlayedSeconds public DateTime? LastSavedDate { - get => !Util.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) + get => !DateUtil.IsDateValid(LastSavedYear, LastSavedMonth, LastSavedDay) ? (DateTime?)null : new DateTime(LastSavedYear, LastSavedMonth, LastSavedDay, LastSavedHour, LastSavedMinute, 0); set diff --git a/PKHeX.Core/Util/DateUtil.cs b/PKHeX.Core/Util/DateUtil.cs index 6fd87ed24..6c89c877f 100644 --- a/PKHeX.Core/Util/DateUtil.cs +++ b/PKHeX.Core/Util/DateUtil.cs @@ -2,7 +2,7 @@ namespace PKHeX.Core { - public static partial class Util + public static class DateUtil { /// /// Determines whether or not the given date components are valid. @@ -13,7 +13,14 @@ public static partial class Util /// A boolean indicating whether or not the date is valid. public static bool IsDateValid(int year, int month, int day) { - return !(year <= 0 || year > DateTime.MaxValue.Year || month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month)); + if (year is <= 0 or > 9999) + return false; + if (month is < 1 or > 12) + return false; + if (day < 1 || day > DateTime.DaysInMonth(year, month)) + return false; + + return true; } /// @@ -47,13 +54,13 @@ public static void GetDateTime2000(uint seconds, out DateTime date, out DateTime public static string ConvertDateValueToString(int value, int secondsBias = -1) { - string tip = string.Empty; + var sb = new System.Text.StringBuilder(); if (value >= SecondsPerDay) - tip += (value / SecondsPerDay) + "d "; - tip += new DateTime(0).AddSeconds(value).ToString("HH:mm:ss"); + sb.Append(value / SecondsPerDay).Append("d "); + sb.Append(new DateTime(0).AddSeconds(value).ToString("HH:mm:ss")); if (secondsBias >= 0) - tip += Environment.NewLine + $"Date: {Epoch2000.AddSeconds(value + secondsBias)}"; - return tip; + sb.Append(Environment.NewLine).Append("Date: ").Append(Epoch2000.AddSeconds(value + secondsBias)); + return sb.ToString(); } } } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Trainer.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Trainer.cs index 48499f01b..2c404abec 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Trainer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Trainer.cs @@ -187,11 +187,11 @@ private void GetTextBoxes() L_LastSaved.Visible = CAL_LastSavedDate.Visible = CAL_LastSavedTime.Visible = false; } - Util.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); + DateUtil.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); CAL_AdventureStartDate.Value = date; CAL_AdventureStartTime.Value = time; - Util.GetDateTime2000(SAV.SecondsToFame, out date, out time); + DateUtil.GetDateTime2000(SAV.SecondsToFame, out date, out time); CAL_HoFDate.Value = date; CAL_HoFTime.Value = time; } @@ -267,8 +267,8 @@ private void Save() // Vivillon SAV.Vivillon = CB_Vivillon.SelectedIndex; - SAV.SecondsToStart = (uint)Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - SAV.SecondsToFame = (uint)Util.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); + SAV.SecondsToStart = (uint)DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + SAV.SecondsToFame = (uint)DateUtil.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); if (SAV.Played.LastSavedDate.HasValue) SAV.Played.LastSavedDate = new DateTime(CAL_LastSavedDate.Value.Year, CAL_LastSavedDate.Value.Month, CAL_LastSavedDate.Value.Day, CAL_LastSavedTime.Value.Hour, CAL_LastSavedTime.Value.Minute, 0); @@ -358,8 +358,8 @@ private void CB_Multi_SelectedIndexChanged(object sender, EventArgs e) switch (index) { case 2: // Storyline Completed Time - var seconds = Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - return Util.ConvertDateValueToString(SAV.GetRecord(index), seconds); + var seconds = DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + return DateUtil.ConvertDateValueToString(SAV.GetRecord(index), seconds); default: return null; } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs index a850bec96..437e4528b 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs @@ -151,11 +151,11 @@ private void GetTextBoxes() L_LastSaved.Visible = CAL_LastSavedDate.Visible = CAL_LastSavedTime.Visible = false; } - Util.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); + DateUtil.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); CAL_AdventureStartDate.Value = date; CAL_AdventureStartTime.Value = time; - Util.GetDateTime2000(SAV.SecondsToFame, out date, out time); + DateUtil.GetDateTime2000(SAV.SecondsToFame, out date, out time); CAL_HoFDate.Value = date; CAL_HoFTime.Value = time; @@ -354,8 +354,8 @@ private void SaveTrainerInfo() SAV.PlayedMinutes = ushort.Parse(MT_Minutes.Text)%60; SAV.PlayedSeconds = ushort.Parse(MT_Seconds.Text)%60; - SAV.SecondsToStart = (uint)Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - SAV.SecondsToFame = (uint)Util.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); + SAV.SecondsToStart = (uint)DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + SAV.SecondsToFame = (uint)DateUtil.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); if (SAV.Played.LastSavedDate.HasValue) SAV.Played.LastSavedDate = new DateTime(CAL_LastSavedDate.Value.Year, CAL_LastSavedDate.Value.Month, CAL_LastSavedDate.Value.Day, CAL_LastSavedTime.Value.Hour, CAL_LastSavedTime.Value.Minute, 0); @@ -560,8 +560,8 @@ private void B_Fashion_Click(object sender, EventArgs e) switch (index) { case 2: // Storyline Completed Time - var seconds = Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - return Util.ConvertDateValueToString(SAV.GetRecord(index), seconds); + var seconds = DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + return DateUtil.ConvertDateValueToString(SAV.GetRecord(index), seconds); default: return null; } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs index 57a75f2a7..06caa1aea 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs @@ -96,11 +96,11 @@ private void GetTextBoxes() L_Started.Visible = CAL_AdventureStartDate.Visible = CAL_AdventureStartTime.Visible = false; L_Fame.Visible = CAL_HoFDate.Visible = CAL_HoFTime.Visible = false; - // Util.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); + // DateUtil.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); // CAL_AdventureStartDate.Value = date; // CAL_AdventureStartTime.Value = time; // - // Util.GetDateTime2000(SAV.SecondsToFame, out date, out time); + // DateUtil.GetDateTime2000(SAV.SecondsToFame, out date, out time); // CAL_HoFDate.Value = date; // CAL_HoFTime.Value = time; } @@ -168,8 +168,8 @@ private void SaveTrainerInfo() SAV.PlayedMinutes = ushort.Parse(MT_Minutes.Text)%60; SAV.PlayedSeconds = ushort.Parse(MT_Seconds.Text)%60; - //SAV.SecondsToStart = (uint)Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - //SAV.SecondsToFame = (uint)Util.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); + //SAV.SecondsToStart = (uint)DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + //SAV.SecondsToFame = (uint)DateUtil.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); // //if (SAV.Played.LastSavedDate.HasValue) // SAV.Played.LastSavedDate = new DateTime(CAL_LastSavedDate.Value.Year, CAL_LastSavedDate.Value.Month, CAL_LastSavedDate.Value.Day, CAL_LastSavedTime.Value.Hour, CAL_LastSavedTime.Value.Minute, 0); @@ -243,8 +243,8 @@ private void B_CopyFromPartyToTitleScreen_Click(object sender, EventArgs e) // switch (index) // { // case 2: // Storyline Completed Time - // var seconds = Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - // return Util.ConvertDateValueToString(SAV.GetRecord(index), seconds); + // var seconds = DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + // return DateUtil.ConvertDateValueToString(SAV.GetRecord(index), seconds); // default: // return null; // } diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs index 212ef13f9..d1a3c671f 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs @@ -153,11 +153,11 @@ public SAV_SimpleTrainer(SaveFile sav) cba[i].Checked = (badgeval & 1 << i) != 0; } - Util.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); + DateUtil.GetDateTime2000(SAV.SecondsToStart, out var date, out var time); CAL_AdventureStartDate.Value = date; CAL_AdventureStartTime.Value = time; - Util.GetDateTime2000(SAV.SecondsToFame, out date, out time); + DateUtil.GetDateTime2000(SAV.SecondsToFame, out date, out time); CAL_HoFDate.Value = date; CAL_HoFTime.Value = time; @@ -258,8 +258,8 @@ private void B_Save_Click(object sender, EventArgs e) s.BattleSubway.BP = (ushort)Math.Min(Util.ToUInt32(MT_Coins.Text), SAV.MaxCoins); } - SAV.SecondsToStart = (uint)Util.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); - SAV.SecondsToFame = (uint)Util.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); + SAV.SecondsToStart = (uint)DateUtil.GetSecondsFrom2000(CAL_AdventureStartDate.Value, CAL_AdventureStartTime.Value); + SAV.SecondsToFame = (uint)DateUtil.GetSecondsFrom2000(CAL_HoFDate.Value, CAL_HoFTime.Value); Origin.CopyChangesFrom(SAV); Close(); diff --git a/Tests/PKHeX.Core.Tests/Util/DateUtilTests.cs b/Tests/PKHeX.Core.Tests/Util/DateUtilTests.cs index 031f9a6f9..05841774c 100644 --- a/Tests/PKHeX.Core.Tests/Util/DateUtilTests.cs +++ b/Tests/PKHeX.Core.Tests/Util/DateUtilTests.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using PKHeX.Core; using Xunit; namespace PKHeX.Tests.Util @@ -10,7 +11,7 @@ public class DateUtilTests [InlineData(2001, 1, 31)] public void RecognizesCorrectDates(int year, int month, int day) { - Assert.True(Core.Util.IsDateValid(year, month, day), $"Failed to recognize {year}/{month}/{day}"); + Assert.True(DateUtil.IsDateValid(year, month, day), $"Failed to recognize {year}/{month}/{day}"); } [Theory] @@ -28,13 +29,13 @@ public void RecognizesCorrectDates(int year, int month, int day) [InlineData(2016, 12, 31)] public void RecognizesValidMonthBoundaries(int year, int month, int day) { - Assert.True(Core.Util.IsDateValid(year, month, day), $"Incorrect month boundary for {year}/{month}/{day}"); + Assert.True(DateUtil.IsDateValid(year, month, day), $"Incorrect month boundary for {year}/{month}/{day}"); } [Fact] public void RecognizeCorrectLeapYear() { - Assert.True(Core.Util.IsDateValid(2004, 2, 29)); + Assert.True(DateUtil.IsDateValid(2004, 2, 29)); } [Theory] @@ -50,7 +51,7 @@ public void RecognizeCorrectLeapYear() [InlineData(uint.MaxValue, uint.MaxValue, uint.MaxValue, false, "Failed with uint.MaxValue, negative")] public void CheckDate(uint year, uint month, uint day, bool cmp, string because) { - var result = Core.Util.IsDateValid(year, month, day); + var result = DateUtil.IsDateValid(year, month, day); result.Should().Be(cmp, because); } }