Skip to content

Commit

Permalink
Merge pull request #133 from TetsuOtter/130-json-affectdate-format
Browse files Browse the repository at this point in the history
AffectDateのフォーマット実装ミスを修正
  • Loading branch information
TetsuOtter authored May 20, 2024
2 parents 55728c3 + e779e3e commit 64fff1c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
27 changes: 27 additions & 0 deletions TRViS.IO.Tests/Utils/StringToDateOnly.tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace TRViS.IO.Tests;

public class StringToDateOnlyTests
{
[Test]
public void EmptyStringTest()
{
Assert.That(Utils.TryStringToDateOnly("", out DateOnly date), Is.False);
Assert.That(date, Is.EqualTo(default(DateOnly)));
}

[Test]
public void NullStringTest()
{
Assert.That(Utils.TryStringToDateOnly(null, out DateOnly date), Is.False);
Assert.That(date, Is.EqualTo(default(DateOnly)));
}

[TestCase("20230318")]
[TestCase("2023-03-18")]
[TestCase("2023-3-18")]
public void ValidStringTest(string input)
{
Assert.That(Utils.TryStringToDateOnly(input, out DateOnly date), Is.True);
Assert.That(date, Is.EqualTo(new DateOnly(2023, 3, 18)));
}
}
2 changes: 1 addition & 1 deletion TRViS.IO/Loaders/LoaderJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Dispose()

return new Models.TrainData(
WorkName: r.Work.Name,
AffectDate: DateOnly.TryParse(r.Work.AffectDate, out DateOnly date) ? date : null,
AffectDate: Utils.StringToDateOnlyOrNull(r.Work.AffectDate),
TrainNumber: r.Train.TrainNumber,
MaxSpeed: t.MaxSpeed,
SpeedType: t.SpeedType,
Expand Down
2 changes: 1 addition & 1 deletion TRViS.IO/Loaders/LoaderSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ on n.Id equals t.WorkId
on t.WorkId equals w.Id
select new TrainData(
WorkName: w.Name,
AffectDate: DateOnly.TryParse(w.AffectDate, out DateOnly date) ? date : null,
AffectDate: Utils.StringToDateOnlyOrNull(w.AffectDate),
TrainNumber: t.TrainNumber,
MaxSpeed: t.MaxSpeed,
SpeedType: t.SpeedType,
Expand Down
27 changes: 27 additions & 0 deletions TRViS.IO/Utils/StringToDateOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace TRViS.IO;

public static partial class Utils
{
public static bool TryStringToDateOnly(string? value, out DateOnly date)
{
if (string.IsNullOrEmpty(value))
{
date = default;
return false;
}

if (value.Length == 8 && value.All(char.IsDigit))
{
int year = int.Parse(value[..4]);
int month = int.Parse(value[4..6]);
int day = int.Parse(value[6..]);
date = new DateOnly(year, month, day);
return true;
}

return DateOnly.TryParse(value, out date);
}

public static DateOnly? StringToDateOnlyOrNull(string? value)
=> TryStringToDateOnly(value, out DateOnly date) ? date : null;
}

0 comments on commit 64fff1c

Please sign in to comment.