From e779e3e8af4a7a0547d21eaeeb37daac8dc300d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BC=B4=EF=BC=B2?= <31824852+TetsuOtter@users.noreply.github.com> Date: Mon, 20 May 2024 22:05:19 +0900 Subject: [PATCH] =?UTF-8?q?AffectDate=E3=81=AE=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=9E=E3=83=83=E3=83=88=E5=AE=9F=E8=A3=85=E3=83=9F=E3=82=B9?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `20240531` のような8文字での指定もできるようにしたつもりだったが、できていなかった --- .../Utils/StringToDateOnly.tests.cs | 27 +++++++++++++++++++ TRViS.IO/Loaders/LoaderJson.cs | 2 +- TRViS.IO/Loaders/LoaderSQL.cs | 2 +- TRViS.IO/Utils/StringToDateOnly.cs | 27 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 TRViS.IO.Tests/Utils/StringToDateOnly.tests.cs create mode 100644 TRViS.IO/Utils/StringToDateOnly.cs diff --git a/TRViS.IO.Tests/Utils/StringToDateOnly.tests.cs b/TRViS.IO.Tests/Utils/StringToDateOnly.tests.cs new file mode 100644 index 00000000..029c2bb7 --- /dev/null +++ b/TRViS.IO.Tests/Utils/StringToDateOnly.tests.cs @@ -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))); + } +} diff --git a/TRViS.IO/Loaders/LoaderJson.cs b/TRViS.IO/Loaders/LoaderJson.cs index 94789d59..f7b2b2a7 100644 --- a/TRViS.IO/Loaders/LoaderJson.cs +++ b/TRViS.IO/Loaders/LoaderJson.cs @@ -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, diff --git a/TRViS.IO/Loaders/LoaderSQL.cs b/TRViS.IO/Loaders/LoaderSQL.cs index 28c76b17..fd4098c6 100644 --- a/TRViS.IO/Loaders/LoaderSQL.cs +++ b/TRViS.IO/Loaders/LoaderSQL.cs @@ -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, diff --git a/TRViS.IO/Utils/StringToDateOnly.cs b/TRViS.IO/Utils/StringToDateOnly.cs new file mode 100644 index 00000000..270170a8 --- /dev/null +++ b/TRViS.IO/Utils/StringToDateOnly.cs @@ -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; +}