Skip to content

Commit

Permalink
Added fallback format when no formating is specified for the Essentia…
Browse files Browse the repository at this point in the history
…lsDate.ToString and EssentialsTime.ToString methods

Normally when an object is used in string interpolation, the object's parameterless ToString method is called for getting a string representation fo the object. But if the object implements the IFormattable interface - as both EssentialsDate and EssentialsTime do now - the ToString(string? format, IFormatProvider? provider) method will be called instead, but with the "format" parameter being null. In order to not introduce any breaking changes, the EssentialsDate.ToString(string? format, IFormatProvider? provider) and EssentialsTime.ToString(string? format, IFormatProvider? provider) methods have now been updated to return the same value as the parameterless EssentialsDate.ToString() and EssentialsTime.ToString() methods, which are an ISO 8601 formatted date and timestamp respectively.
  • Loading branch information
abjerner committed Apr 19, 2024
1 parent 782e932 commit 85316d9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Skybrud.Essentials/Time/EssentialsDate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] stri
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current <see cref="EssentialsDate"/> object as specified by <paramref name="format"/> and <paramref name="provider"/>.</returns>
public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, IFormatProvider? provider) {
return _dateTime.ToString(format, provider);
return _dateTime.ToString(string.IsNullOrWhiteSpace(format) ? "yyyy-MM-dd" : format, provider);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Essentials/Time/EssentialsTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] stri
/// <param name="provider">An object that supplies culture-specific formatting information.</param>
/// <returns>A string representation of value of the current <see cref="EssentialsTime"/> object as specified by <paramref name="format"/> and <paramref name="provider"/>.</returns>
public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, IFormatProvider? provider) {
return DateTimeOffset.ToString(format, provider);
return string.IsNullOrWhiteSpace(format) ? Iso8601 : DateTimeOffset.ToString(format, provider);
}

/// <summary>
Expand Down
5 changes: 3 additions & 2 deletions src/TestProject1/Time/EssentialsDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public void ToStringDefault() {

EssentialsDate date = new(2024, 3, 27);

Assert.AreEqual("2024-03-27", date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
Assert.AreEqual("2024-03-27", date.ToString("yyyy-MM-dd", CultureInfo.GetCultureInfo("da-DK")));
Assert.AreEqual("2024-03-27", date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), "#1");
Assert.AreEqual("2024-03-27", date.ToString("yyyy-MM-dd", CultureInfo.GetCultureInfo("da-DK")), "#2");
Assert.AreEqual("2024-03-27", $"{date}", "#3");

}

Expand Down
3 changes: 2 additions & 1 deletion src/TestProject1/Time/EssentialsTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public void ToStringDefault() {

EssentialsTime time = new(2024, 3, 27, 12, 0, 0, TimeZoneInfo.Utc);

Assert.AreEqual("2024-03-27T12:00:00.000Z", time.ToString());
Assert.AreEqual("2024-03-27T12:00:00.000Z", time.ToString(), "#1");
Assert.AreEqual("2024-03-27T12:00:00.000Z", $"{time}", "#2");

}

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

}

[TestMethod]
public void ToStringDefault() {

EssentialsDate date = new EssentialsDate(2024, 4, 7);

Assert.AreEqual("2024-04-07", date.ToString());
Assert.AreEqual("2024-04-07", $"{date}");
Assert.AreEqual("2024-04-07", $"{date:yyyy-MM-dd}");

}

}

}

0 comments on commit 85316d9

Please sign in to comment.