diff --git a/src/Cloudflare/Extensions/DateTimeExtensions.cs b/src/Cloudflare/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..cbf7d88 --- /dev/null +++ b/src/Cloudflare/Extensions/DateTimeExtensions.cs @@ -0,0 +1,34 @@ +namespace AMWD.Net.Api.Cloudflare +{ + /// + /// Extension methods for s. + /// + public static class DateTimeExtensions + { + private const string Iso8601Format = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + /// + /// Converts the specified to its ISO 8601 string representation. + /// + /// + /// This method ensures that the resulting string conforms to the ISO 8601 standard, which is + /// commonly used for representing dates and times in a machine-readable format. + /// + /// The instance to convert. + /// + /// A string representing the in ISO 8601 format. + ///
+ /// If the property is , the value is first converted to . + ///
+ /// If the is , it is treated as . + ///
+ public static string ToIso8601Format(this DateTime dateTime) + { + if (dateTime.Kind == DateTimeKind.Local) + return dateTime.ToUniversalTime().ToString(Iso8601Format); + + // DateTimeKind.Unspecified is treated as UTC + return dateTime.ToString(Iso8601Format); + } + } +} diff --git a/src/Extensions/Cloudflare.Zones/Internals/InternalRemoveZoneHoldFilter.cs b/src/Extensions/Cloudflare.Zones/Internals/InternalRemoveZoneHoldFilter.cs index 51b97b7..c98755f 100644 --- a/src/Extensions/Cloudflare.Zones/Internals/InternalRemoveZoneHoldFilter.cs +++ b/src/Extensions/Cloudflare.Zones/Internals/InternalRemoveZoneHoldFilter.cs @@ -9,7 +9,7 @@ var dict = new Dictionary(); if (HoldAfter.HasValue) - dict.Add("hold_after", HoldAfter.Value.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss'Z'")); + dict.Add("hold_after", HoldAfter.Value.ToIso8601Format()); return dict; } diff --git a/test/Cloudflare.Tests/Extensions/DateTimeExtensionsTest.cs b/test/Cloudflare.Tests/Extensions/DateTimeExtensionsTest.cs new file mode 100644 index 0000000..f666c6e --- /dev/null +++ b/test/Cloudflare.Tests/Extensions/DateTimeExtensionsTest.cs @@ -0,0 +1,50 @@ +using AMWD.Net.Api.Cloudflare; + +namespace Cloudflare.Tests.Extensions +{ + [TestClass] + public class DateTimeExtensionsTest + { + private const string Format = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + [TestMethod] + public void ShouldReturnIsoStringForUtc() + { + // Arrange + var dateTime = new DateTime(2025, 10, 10, 1, 2, 3, DateTimeKind.Utc); + + // Act + string isoString = dateTime.ToIso8601Format(); + + // Assert + Assert.AreEqual(dateTime.ToString(Format), isoString); + } + + [TestMethod] + public void ShouldReturnIsoStringForUnspecified() + { + // Arrange + var dateTime = new DateTime(2025, 10, 10, 1, 2, 3, DateTimeKind.Unspecified); + + // Act + string isoString = dateTime.ToIso8601Format(); + + // Assert + Assert.AreEqual(dateTime.ToString(Format), isoString); + } + + [TestMethod] + public void ShouldReturnIsoStringForLocal() + { + // Arrange + var dateTime = new DateTime(2025, 10, 10, 1, 2, 3, DateTimeKind.Local); + var offset = TimeZoneInfo.Local.GetUtcOffset(dateTime); + + // Act + string isoString = dateTime.ToIso8601Format(); + + // Assert + Assert.AreEqual(dateTime.Subtract(offset).ToString(Format), isoString); + } + } +}