Skip to content

Commit 1720692

Browse files
AndyButlandiOvergaard
authored andcommitted
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.
1 parent fc815db commit 1720692

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/Umbraco.Core/Extensions/DateTimeExtensions.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace Umbraco.Extensions;
77

88
public static class DateTimeExtensions
99
{
10+
/// <summary>
11+
/// Defines the levels to truncate a date to.
12+
/// </summary>
1013
public enum DateTruncate
1114
{
1215
Year,
@@ -25,33 +28,39 @@ public enum DateTruncate
2528
public static string ToIsoString(this DateTime dt) =>
2629
dt.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
2730

31+
/// <summary>
32+
/// Truncates the date to the specified level, i.e. if you pass in DateTruncate.Hour it will truncate the date to the hour.
33+
/// </summary>
34+
/// <param name="dt">The date.</param>
35+
/// <param name="truncateTo">The level to truncate the date to.</param>
36+
/// <returns>The truncated date.</returns>
2837
public static DateTime TruncateTo(this DateTime dt, DateTruncate truncateTo)
2938
{
3039
if (truncateTo == DateTruncate.Year)
3140
{
32-
return new DateTime(dt.Year, 1, 1);
41+
return new DateTime(dt.Year, 1, 1, 0, 0, 0, dt.Kind);
3342
}
3443

3544
if (truncateTo == DateTruncate.Month)
3645
{
37-
return new DateTime(dt.Year, dt.Month, 1);
46+
return new DateTime(dt.Year, dt.Month, 1, 0, 0, 0, dt.Kind);
3847
}
3948

4049
if (truncateTo == DateTruncate.Day)
4150
{
42-
return new DateTime(dt.Year, dt.Month, dt.Day);
51+
return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, dt.Kind);
4352
}
4453

4554
if (truncateTo == DateTruncate.Hour)
4655
{
47-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0);
56+
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0, dt.Kind);
4857
}
4958

5059
if (truncateTo == DateTruncate.Minute)
5160
{
52-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
61+
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0, dt.Kind);
5362
}
5463

55-
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
64+
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Kind);
5665
}
5766
}

tests/Umbraco.Tests.Integration/Umbraco.Core/Services/ContentPublishingServiceTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Bogus.DataSets;
21
using NUnit.Framework;
32
using Umbraco.Cms.Core;
43
using Umbraco.Cms.Core.Models;
@@ -23,8 +22,8 @@ public class ContentPublishingServiceTests : UmbracoIntegrationTestWithContent
2322
{
2423
private const string UnknownCulture = "ke-Ke";
2524

26-
private readonly DateTime _schedulePublishDate = DateTime.UtcNow.AddDays(1);
27-
private readonly DateTime _scheduleUnPublishDate = DateTime.UtcNow.AddDays(2);
25+
private readonly DateTime _schedulePublishDate = DateTime.UtcNow.AddDays(1).TruncateTo(DateTimeExtensions.DateTruncate.Second);
26+
private readonly DateTime _scheduleUnPublishDate = DateTime.UtcNow.AddDays(2).TruncateTo(DateTimeExtensions.DateTruncate.Second);
2827

2928
[SetUp]
3029
public new void Setup() => ContentRepositoryBase.ThrowOnWarning = true;

0 commit comments

Comments
 (0)