Skip to content

Commit

Permalink
"EssentialsDate.TryParse" method should invariant culture if not a cu…
Browse files Browse the repository at this point in the history
…lture isn't explicitly specified
  • Loading branch information
abjerner committed Feb 14, 2022
1 parent 32860ca commit 1733f2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Skybrud.Essentials/Time/EssentialsDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ public static bool TryParse(string input, out EssentialsDate result) {
if (string.IsNullOrWhiteSpace(input)) return false;

// Attempt to parse the date
if (DateTime.TryParse(input, out DateTime dt)) {
if (DateTime.TryParse(input, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dt)) {
result = new EssentialsDate(dt);
}

Expand Down
23 changes: 23 additions & 0 deletions src/UnitTestProject1/Time/EssentialsDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ public void YearAndDay() {

}

[TestMethod]
public void TryParse1() {

bool success1 = EssentialsDate.TryParse("2019-08-17", out EssentialsDate result1);
bool success2 = EssentialsDate.TryParse("08/17/2019", out EssentialsDate result2);
bool success3 = EssentialsDate.TryParse("17/08/2019", out EssentialsDate result3);

Assert.AreEqual(true, success1, "#1");
Assert.AreEqual(true, success2, "#2");
Assert.AreEqual(false, success3, "#3");

Assert.AreEqual(2019, result1.Year);
Assert.AreEqual(8, result1.Month);
Assert.AreEqual(17, result1.Day);

Assert.AreEqual(2019, result2.Year);
Assert.AreEqual(8, result2.Month);
Assert.AreEqual(17, result2.Day);

Assert.IsNull(result3);

}

[TestMethod]
public void TryParse() {

Expand Down

0 comments on commit 1733f2f

Please sign in to comment.