-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from TetsuOtter/130-json-affectdate-format
AffectDateのフォーマット実装ミスを修正
- Loading branch information
Showing
4 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |