Split DateUtil from Util class

This commit is contained in:
Kurt
2021-05-14 16:46:48 -07:00
parent 3ac1882151
commit acdbda4e12
17 changed files with 54 additions and 46 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -133,7 +133,7 @@ public void SetTeam(IReadOnlyList<PKM> 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<PKM> 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);
}

View File

@@ -93,7 +93,7 @@ public void SetPlayerNames(IReadOnlyList<string> 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);
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -2,7 +2,7 @@
namespace PKHeX.Core
{
public static partial class Util
public static class DateUtil
{
/// <summary>
/// Determines whether or not the given date components are valid.
@@ -13,7 +13,14 @@ public static partial class Util
/// <returns>A boolean indicating whether or not the date is valid.</returns>
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;
}
/// <summary>
@@ -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();
}
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
// }

View File

@@ -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();

View File

@@ -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);
}
}