Persist and expose Umbraco system dates as UTC#19705
Conversation
…t when not specified is true).
… date constraints.
…or and outputting in delivery API.
…em-dates-database-dates-persist-utc
…ocument history, so we can control the UTC setting on the retrieved persisted dates.
…lished for variant content.
…em-dates-database-dates-persist-utc
There was a problem hiding this comment.
Pull Request Overview
This PR systematically replaces DateTime.Now with DateTime.UtcNow across test files to align with Umbraco's move to using UTC for system dates. This change ensures test data uses UTC timestamps consistently, supporting the broader goal of making Umbraco's datetime handling more predictable by standardizing on UTC throughout the system.
- Replace all test builders and mock objects to use UTC timestamps instead of local time
- Update property references from UTC-suffixed names to standard names (e.g.,
LastLoginDateUtc→LastLoginDate) - Remove large commented-out test files that were disabled since Umbraco 15
Reviewed Changes
Copilot reviewed 169 out of 169 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| Multiple test builder files | Updated to use DateTime.UtcNow instead of DateTime.Now for consistent UTC timestamps |
| MemberUserStoreTests.cs | Updated property references and assertions to use non-UTC-suffixed property names |
| AuditEntryServiceTests.cs | Updated audit entry date property reference from EventDateUtc to EventDate |
| PublishedRouterTests.cs | Updated mock content creation to use UTC timestamps |
| Multiple PublishedCache test files | Removed entirely commented-out test files to clean up codebase |
Comments suppressed due to low confidence (2)
tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberUserStoreTests.cs:195
- The test assertion on line 195 directly compares DateTime values without considering potential precision differences that could occur during database round-trips or serialization. Consider using a tolerance-based comparison or ensuring both values use the same precision.
Assert.AreEqual(fakeUser.LastPasswordChangeDate, mockMember.LastPasswordChangeDate);
tests/Umbraco.Tests.UnitTests/Umbraco.Infrastructure/Security/MemberUserStoreTests.cs:256
- Similar to the previous assertion, this direct DateTime comparison could be brittle if there are precision differences between the expected and actual values. Consider using Assert.That with a tolerance or ensuring consistent DateTime precision.
Assert.AreEqual(fakeUser.LastLoginDate.Value, mockMember.LastLoginDate);
lauraneto
left a comment
There was a problem hiding this comment.
Still testing, but already noticed a few things from reviewing the changes.
src/Umbraco.Infrastructure/Persistence/Factories/ServerRegistrationFactory.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/SqlSyntax/SqlSyntaxProviderBase.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentVersionRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/DocumentVersionRepository.cs
Show resolved
Hide resolved
src/Umbraco.Infrastructure/Persistence/Dtos/DocumentPublishedReadOnlyDto.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
* Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
Prerequisites
Description
This is a first PR toward making Umbraco's handling of system dates more consistent by using UTC throughout. It's targeted at
v17/feature/utc-system-dates, a feature branch taken fromv17/dev, with the idea that we create a few, smaller PRs into the feature branch and then finally merge it intov17/devwhen we are happy with it.When merged, this PR will make the following changes:
SpecifyKindto ensure we have local server time when we have otherwise retrieved UTC. Again UTC is what we want now.DateTime.Nowto useDateTime.UtcNow.ALTER TABLEsupport, the existing constraints aren't correct, and we don't rely on them in application code).Kind, and it's the same when retrieving from the hybrid cache. Used this in factory methods to ensure entities and published content are built with datetimes as UTC.Kindwe ensure anywhere downstream gets this too.Note that the following is not part of this PR, so isn't expected to work correctly:
Note also that I've removed some tests from files that were completely commented out (since Umbraco 15 I believe, with the introduction of the Hybrid cache). Most of them used dates and would get out of sync if we kept them, so I thought it was better now to remove.
Testing
Date Persistence
Easiest for testing to start with is to start off with a clean database and install Umbraco. Using database queries like the following you can verify that the default data installed uses UTC:
Then make some updates that will affect dates, e.g.:
Date Presentation
Management API
Verify management API endpoints are returning the expected dates, e.g.:
Should return datetime responses like:
Backoffice
Verify that the backoffice is displaying the dates in the user's local time. I haven't touched this, but seems they "just work" due to the use of localized dates, so long as we are providing a date with timezone information from the management API, which we are.
Delivery API
Verify delivery API endpoints are returning the expected dates, e.g.:
Should return datetime responses like:
Templates
Verify that Razor rendering makes available the system dates as UTC, e.g. with:
Migration of Database DateTime Default Constraints
For this one take an Umbraco database for Umbraco 16, and verify for example that the
umbracoNode.createDatetable has a default value ofgetdate().Then connect the database to the code in this PR and restart so the migration runs.
You should find that the default is now
getutcdate().