From 85316d9e84060efa35fb1abb19153b5bf085d7cf Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Fri, 19 Apr 2024 20:10:41 +0200 Subject: [PATCH] Added fallback format when no formating is specified for the EssentialsDate.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. --- src/Skybrud.Essentials/Time/EssentialsDate.cs | 2 +- src/Skybrud.Essentials/Time/EssentialsTime.cs | 2 +- src/TestProject1/Time/EssentialsDateTests.cs | 5 +++-- src/TestProject1/Time/EssentialsTimeTests.cs | 3 ++- src/UnitTestProject1/Time/Time/EssentialsDateTests.cs | 11 +++++++++++ 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Skybrud.Essentials/Time/EssentialsDate.cs b/src/Skybrud.Essentials/Time/EssentialsDate.cs index d227163..222ed4f 100644 --- a/src/Skybrud.Essentials/Time/EssentialsDate.cs +++ b/src/Skybrud.Essentials/Time/EssentialsDate.cs @@ -439,7 +439,7 @@ public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] stri /// An object that supplies culture-specific formatting information. /// A string representation of value of the current object as specified by and . 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 diff --git a/src/Skybrud.Essentials/Time/EssentialsTime.cs b/src/Skybrud.Essentials/Time/EssentialsTime.cs index 802d144..ff0a18c 100644 --- a/src/Skybrud.Essentials/Time/EssentialsTime.cs +++ b/src/Skybrud.Essentials/Time/EssentialsTime.cs @@ -791,7 +791,7 @@ public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] stri /// An object that supplies culture-specific formatting information. /// A string representation of value of the current object as specified by and . public string ToString([StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, IFormatProvider? provider) { - return DateTimeOffset.ToString(format, provider); + return string.IsNullOrWhiteSpace(format) ? Iso8601 : DateTimeOffset.ToString(format, provider); } /// diff --git a/src/TestProject1/Time/EssentialsDateTests.cs b/src/TestProject1/Time/EssentialsDateTests.cs index 532066d..18e6ac8 100644 --- a/src/TestProject1/Time/EssentialsDateTests.cs +++ b/src/TestProject1/Time/EssentialsDateTests.cs @@ -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"); } diff --git a/src/TestProject1/Time/EssentialsTimeTests.cs b/src/TestProject1/Time/EssentialsTimeTests.cs index 9a54ec2..1053887 100644 --- a/src/TestProject1/Time/EssentialsTimeTests.cs +++ b/src/TestProject1/Time/EssentialsTimeTests.cs @@ -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"); } diff --git a/src/UnitTestProject1/Time/Time/EssentialsDateTests.cs b/src/UnitTestProject1/Time/Time/EssentialsDateTests.cs index 23db8f2..55543de 100644 --- a/src/UnitTestProject1/Time/Time/EssentialsDateTests.cs +++ b/src/UnitTestProject1/Time/Time/EssentialsDateTests.cs @@ -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}"); + + } + } } \ No newline at end of file