Repository navigation
Standardise reporting dates and times (#595) #615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StuartFerguson
merged 2 commits into
main
from
task/#595_standardise_date_and_time_handling
Sep 30, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
EstateReportingAPI.BusinessLogic.UnitTests/ReportingDatePolicyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using EstateReportingAPI.BusinessLogic; | ||
| using Shouldly; | ||
|
|
||
| namespace EstateReportingAPI.BusinessLogic.UnitTests; | ||
|
|
||
| public sealed class ReportingDatePolicyTests | ||
| { | ||
| [Fact] | ||
| public void Today_UsesEuropeLondonAtUtcMidnightBoundary() | ||
| { | ||
| ReportingDatePolicy policy = CreatePolicy(new DateTimeOffset(2026, 10, 25, 0, 30, 0, TimeSpan.Zero)); | ||
|
|
||
| policy.Today.ShouldBe(new DateOnly(2026, 10, 25)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetRange_UsesInclusiveStartAndExclusiveDayAfterEnd() | ||
| { | ||
| ReportingDatePolicy policy = CreatePolicy(new DateTimeOffset(2026, 9, 30, 12, 0, 0, TimeSpan.Zero)); | ||
|
|
||
| ReportingDateRange range = policy.GetRange(new DateOnly(2026, 9, 1), new DateOnly(2026, 9, 30)); | ||
|
|
||
| range.StartInclusive.ShouldBe(new DateTime(2026, 9, 1)); | ||
| range.EndExclusive.ShouldBe(new DateTime(2026, 10, 1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Today_UsesBritishSummerTimeWhenUtcDateDiffersFromLocalDate() | ||
| { | ||
| ReportingDatePolicy policy = CreatePolicy(new DateTimeOffset(2026, 6, 30, 23, 30, 0, TimeSpan.Zero)); | ||
|
|
||
| policy.Today.ShouldBe(new DateOnly(2026, 7, 1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToDateTimeOffset_AppliesBritishSummerTimeOffset() | ||
| { | ||
| ReportingDatePolicy policy = CreatePolicy(new DateTimeOffset(2026, 6, 30, 12, 0, 0, TimeSpan.Zero)); | ||
|
|
||
| policy.ToDateTimeOffset(new DateTime(2026, 6, 30, 13, 0, 0)).Offset.ShouldBe(TimeSpan.FromHours(1)); | ||
| } | ||
|
|
||
| private static ReportingDatePolicy CreatePolicy(DateTimeOffset utcNow) | ||
| { | ||
| return new ReportingDatePolicy(new FixedTimeProvider(utcNow)); | ||
| } | ||
|
|
||
| private sealed class FixedTimeProvider(DateTimeOffset utcNow) : TimeProvider | ||
| { | ||
| public override DateTimeOffset GetUtcNow() => utcNow; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace EstateReportingAPI.BusinessLogic; | ||
|
|
||
| public readonly record struct ReportingDateRange(DateTime StartInclusive, DateTime EndExclusive); | ||
|
|
||
| public sealed class ReportingDatePolicy | ||
| { | ||
| private const string IanaTimeZoneId = "Europe/London"; | ||
| private const string WindowsTimeZoneId = "GMT Standard Time"; | ||
|
|
||
| private readonly TimeProvider Clock; | ||
| private readonly TimeZoneInfo TimeZone; | ||
|
|
||
| public ReportingDatePolicy(TimeProvider clock) | ||
| { | ||
| Clock = clock ?? throw new ArgumentNullException(nameof(clock)); | ||
| TimeZone = ResolveTimeZone(); | ||
| } | ||
|
|
||
| public DateOnly Today => DateOnly.FromDateTime(LocalNow); | ||
|
|
||
| public DateTime LocalNow => TimeZoneInfo.ConvertTime(Clock.GetUtcNow(), TimeZone).DateTime; | ||
|
|
||
| public DateTimeOffset ToDateTimeOffset(DateTime localDateTime) | ||
| { | ||
| DateTime unspecified = DateTime.SpecifyKind(localDateTime, DateTimeKind.Unspecified); | ||
| return new DateTimeOffset(unspecified, TimeZone.GetUtcOffset(unspecified)); | ||
| } | ||
|
|
||
| public ReportingDateRange GetRange(DateOnly startDate, DateOnly endDate) | ||
| { | ||
| if (startDate > endDate) | ||
| throw new ArgumentException("The start date must be less than or equal to the end date.", nameof(startDate)); | ||
|
|
||
| DateTime startInclusive = startDate.ToDateTime(TimeOnly.MinValue, DateTimeKind.Unspecified); | ||
| DateTime endExclusive = endDate.AddDays(1).ToDateTime(TimeOnly.MinValue, DateTimeKind.Unspecified); | ||
|
|
||
| return new ReportingDateRange(startInclusive, endExclusive); | ||
| } | ||
|
|
||
| private static TimeZoneInfo ResolveTimeZone() | ||
| { | ||
| try | ||
| { | ||
| return TimeZoneInfo.FindSystemTimeZoneById(IanaTimeZoneId); | ||
| } | ||
| catch (TimeZoneNotFoundException) | ||
| { | ||
| return TimeZoneInfo.FindSystemTimeZoneById(WindowsTimeZoneId); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.