Skip to content

Commit 537d1e1

Browse files
committed
feat: ManualTimeProvider sets local time zone to UTC by default, allow setting during testing
1 parent 12eacfd commit 537d1e1

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
## [1.0.0-preview.2]
10+
11+
- Changed ManualTestProvider sets the local time zone to UTC by default, provides method for overriding during testing.
12+
- Changed `ManualTestProvider` sets the local time zone to UTC by default, provides method for overriding during testing.
913

1014
## [1.0.0-preview.1]
1115

src/TimeProviderExtensions/ManualTimeProvider.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ManualTimeProvider : TimeProvider
2424

2525
private readonly List<ManualTimerScheduledCallback> futureCallbacks = new();
2626
private DateTimeOffset utcNow;
27+
private TimeZoneInfo localTimeZone = TimeZoneInfo.Utc;
2728

2829
/// <summary>
2930
/// Gets the frequency of <see cref="GetTimestamp"/> of high-frequency value per second.
@@ -34,14 +35,21 @@ public class ManualTimeProvider : TimeProvider
3435
/// </remarks>
3536
public override long TimestampFrequency { get; } = 10_000_000;
3637

38+
/// <inheritdoc />
39+
public override TimeZoneInfo LocalTimeZone => localTimeZone;
40+
3741
/// <summary>
3842
/// Creates an instance of the <see cref="ManualTimeProvider"/> with
3943
/// <see cref="DateTimeOffset.UtcNow"/> being the initial value returned by <see cref="GetUtcNow()"/>.
4044
/// </summary>
4145
public ManualTimeProvider()
4246
: this(System.GetUtcNow())
47+
/// <param name="localTimeZone">Optional local time zone to use during testing. Defaults to <see cref="TimeZoneInfo.Utc"/>.</param>
48+
public ManualTimeProvider(TimeZoneInfo? localTimeZone = null)
49+
: this(Epoch)
4350
{
4451

52+
this.localTimeZone = localTimeZone ?? TimeZoneInfo.Utc;
4553
}
4654

4755
/// <summary>
@@ -54,6 +62,18 @@ public ManualTimeProvider(DateTimeOffset startDateTime)
5462
utcNow = startDateTime;
5563
}
5664

65+
/// <summary>
66+
/// Creates an instance of the <see cref="ManualTimeProvider"/> with
67+
/// <paramref name="startDateTime"/> being the initial value returned by <see cref="GetUtcNow()"/>.
68+
/// </summary>
69+
/// <param name="startDateTime">The initial date and time <see cref="GetUtcNow()"/> will return.</param>
70+
/// <param name="localTimeZone">Optional local time zone to use during testing. Defaults to <see cref="TimeZoneInfo.Utc"/>.</param>
71+
public ManualTimeProvider(DateTimeOffset startDateTime, TimeZoneInfo? localTimeZone = null)
72+
{
73+
utcNow = startDateTime;
74+
this.localTimeZone = localTimeZone ?? TimeZoneInfo.Utc;
75+
}
76+
5777
/// <summary>
5878
/// Gets the current high-frequency value designed to measure small time intervals with high accuracy in the timer mechanism.
5979
/// </summary>
@@ -128,6 +148,15 @@ public void ForwardTime(TimeSpan delta)
128148
SetUtcNow(utcNow + delta);
129149
}
130150

151+
/// <summary>
152+
/// Sets the local time zone.
153+
/// </summary>
154+
/// <param name="localTimeZone">The local time zone.</param>
155+
public void SetLocalTimeZone(TimeZoneInfo localTimeZone)
156+
{
157+
this.localTimeZone = localTimeZone;
158+
}
159+
131160
/// <summary>
132161
/// Advance the date and time represented by <see cref="GetUtcNow()"/>
133162
/// by the specified <paramref name="delta"/>, and triggers any

0 commit comments

Comments
 (0)