From 1720692d3ddd89feba6fe93d2c7c1f9d854250eb Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Tue, 1 Apr 2025 09:06:24 +0200 Subject: [PATCH] Ensures date comparisons in schedule integration tests are made only on the datetime part to the second (#18894) * Ensures date comparisons in schedule integration tests are made only on the date part. * Include time part to the second. * Ensure Kind is retained when truncating a date. * Retain Kind for all truncation levels. --- .../Extensions/DateTimeExtensions.cs | 21 +++++++++++++------ .../Services/ContentPublishingServiceTests.cs | 5 ++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/Extensions/DateTimeExtensions.cs b/src/Umbraco.Core/Extensions/DateTimeExtensions.cs index 35c9f600e5..00191e5a76 100644 --- a/src/Umbraco.Core/Extensions/DateTimeExtensions.cs +++ b/src/Umbraco.Core/Extensions/DateTimeExtensions.cs @@ -7,6 +7,9 @@ namespace Umbraco.Extensions; public static class DateTimeExtensions { + /// + /// Defines the levels to truncate a date to. + /// public enum DateTruncate { Year, @@ -25,33 +28,39 @@ public static class DateTimeExtensions public static string ToIsoString(this DateTime dt) => dt.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); + /// + /// Truncates the date to the specified level, i.e. if you pass in DateTruncate.Hour it will truncate the date to the hour. + /// + /// The date. + /// The level to truncate the date to. + /// The truncated date. public static DateTime TruncateTo(this DateTime dt, DateTruncate truncateTo) { if (truncateTo == DateTruncate.Year) { - return new DateTime(dt.Year, 1, 1); + return new DateTime(dt.Year, 1, 1, 0, 0, 0, dt.Kind); } if (truncateTo == DateTruncate.Month) { - return new DateTime(dt.Year, dt.Month, 1); + return new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind); } if (truncateTo == DateTruncate.Day) { - return new DateTime(dt.Year, dt.Month, dt.Day); + return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, dt.Kind); } if (truncateTo == DateTruncate.Hour) { - return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0); + return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind); } if (truncateTo == DateTruncate.Minute) { - return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0); + return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0, dt.Kind); } - return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); + return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Kind); } } diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentPublishingServiceTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentPublishingServiceTests.cs index 0853bca8c5..050b08582c 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentPublishingServiceTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentPublishingServiceTests.cs @@ -1,4 +1,3 @@ -using Bogus.DataSets; using NUnit.Framework; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Models; @@ -23,8 +22,8 @@ public class ContentPublishingServiceTests : UmbracoIntegrationTestWithContent { private const string UnknownCulture = "ke-Ke"; - private readonly DateTime _schedulePublishDate = DateTime.UtcNow.AddDays(1); - private readonly DateTime _scheduleUnPublishDate = DateTime.UtcNow.AddDays(2); + private readonly DateTime _schedulePublishDate = DateTime.UtcNow.AddDays(1).TruncateTo(DateTimeExtensions.DateTruncate.Second); + private readonly DateTime _scheduleUnPublishDate = DateTime.UtcNow.AddDays(2).TruncateTo(DateTimeExtensions.DateTruncate.Second); [SetUp] public new void Setup() => ContentRepositoryBase.ThrowOnWarning = true;