From 35d4edf85df04284e1f874290b359c484a0c0fdc Mon Sep 17 00:00:00 2001 From: Evan Dixon Date: Wed, 10 Aug 2016 19:54:12 -0500 Subject: [PATCH] Fixed crash in DateUtil when Year is 0 --- PKHeX/MysteryGifts/PGF.cs | 2 +- PKHeX/MysteryGifts/WC6.cs | 2 +- PKHeX/PKM/PKM.cs | 4 ++-- PKHeX/Util/DateUtil.cs | 2 +- Tests/PKHeX.Tests/PKM/PKMTests.cs | 8 +++++++- Tests/PKHeX.Tests/Util/DateUtilTests.cs | 7 +++++++ 6 files changed, 19 insertions(+), 6 deletions(-) diff --git a/PKHeX/MysteryGifts/PGF.cs b/PKHeX/MysteryGifts/PGF.cs index 06bab3020..7645a89bc 100644 --- a/PKHeX/MysteryGifts/PGF.cs +++ b/PKHeX/MysteryGifts/PGF.cs @@ -102,7 +102,7 @@ public override string CardTitle get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!Util.IsDateValid(2000 + Year, Month, Day)) { return null; } diff --git a/PKHeX/MysteryGifts/WC6.cs b/PKHeX/MysteryGifts/WC6.cs index 7ce7850ce..3774d5d78 100644 --- a/PKHeX/MysteryGifts/WC6.cs +++ b/PKHeX/MysteryGifts/WC6.cs @@ -52,7 +52,7 @@ public WC6(byte[] data = null) get { // Check to see if date is valid - if (!Util.IsDateValid(Year, Month, Day)) + if (!Util.IsDateValid(2000 + Year, Month, Day)) { return null; } diff --git a/PKHeX/PKM/PKM.cs b/PKHeX/PKM/PKM.cs index e94d84d39..3008f6e80 100644 --- a/PKHeX/PKM/PKM.cs +++ b/PKHeX/PKM/PKM.cs @@ -141,7 +141,7 @@ public byte[] Write() get { // Check to see if date is valid - if (!Util.IsDateValid(Met_Year, Met_Month, Met_Day)) + if (!Util.IsDateValid(2000 + Met_Year, Met_Month, Met_Day)) { return null; } @@ -186,7 +186,7 @@ public byte[] Write() get { // Check to see if date is valid - if (!Util.IsDateValid(Egg_Year, Egg_Month, Egg_Day)) + if (!Util.IsDateValid(2000 + Egg_Year, Egg_Month, Egg_Day)) { return null; } diff --git a/PKHeX/Util/DateUtil.cs b/PKHeX/Util/DateUtil.cs index c7edda0ae..0cff93c7f 100644 --- a/PKHeX/Util/DateUtil.cs +++ b/PKHeX/Util/DateUtil.cs @@ -16,7 +16,7 @@ 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)); + return !(year <= 0 || year > DateTime.MaxValue.Year || month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth(year, month)); } /// diff --git a/Tests/PKHeX.Tests/PKM/PKMTests.cs b/Tests/PKHeX.Tests/PKM/PKMTests.cs index 37f394d7a..172511e7b 100644 --- a/Tests/PKHeX.Tests/PKM/PKMTests.cs +++ b/Tests/PKHeX.Tests/PKM/PKMTests.cs @@ -22,13 +22,19 @@ public void MetDateGetterTest() pk.MetDay = 0; pk.MetMonth = 0; pk.MetYear = 0; - Assert.IsFalse(pk.MetDate.HasValue, "MetDate should be null when date components are all 0."); + Assert.IsFalse(pk.MetDate.HasValue, "MetDate should be null when date components are all 0."); // Ensure MetDate gives correct date pk.MetDay = 10; pk.MetMonth = 8; pk.MetYear = 16; Assert.AreEqual(new DateTime(2016, 8, 10).Date, pk.MetDate.Value.Date, "Met date does not return correct date."); + + // Ensure 0 year is calculated correctly + pk.MetDay = 1; + pk.MetMonth = 1; + pk.MetYear = 0; + Assert.AreEqual(2000, pk.MetDate.Value.Date.Year, "Year is not calculated correctly."); } [TestMethod] diff --git a/Tests/PKHeX.Tests/Util/DateUtilTests.cs b/Tests/PKHeX.Tests/Util/DateUtilTests.cs index 4356003de..002bfb1dd 100644 --- a/Tests/PKHeX.Tests/Util/DateUtilTests.cs +++ b/Tests/PKHeX.Tests/Util/DateUtilTests.cs @@ -101,6 +101,13 @@ public void FailsWithZeroMonth() Assert.IsFalse(PKHeX.Util.IsDateValid(2000, 0, 1)); } + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithZeroYear() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(0, 1, 1)); + } + [TestMethod] [TestCategory(DateUtilCategory)] public void TestUIntOverload()