Class FakeClock
Clock which can be constructed with an initial instant, and then advanced programmatically (and optionally,
automatically advanced on each read).
This class is designed to be used when testing classes which take an IClock as a dependency.
Since 1.0.x
Availability net45, netstandard1.3
Inheritance
Object
FakeClock
Implements
Inherited Members
Object.ToString()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Namespace: NodaTime.Testing
Assembly: NodaTime.Testing.dll
Syntax
public sealed class FakeClock : IClock
Remarks
This class is somewhere between a fake and a stub, depending on how it's used - if it's set to
AutoAdvance then time will pass, but in a pretty odd way (i.e. dependent on how
often it's consulted).
Constructors
FakeClock(Instant)
Creates a fake clock initially set to the given instant, with no auto-advance.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public FakeClock(Instant initial)
Parameters
Type | Name | Description |
---|---|---|
Instant | initial | The initial instant. |
FakeClock(Instant, Duration)
Creates a fake clock initially set to the given instant. The clock will advance by the given duration on
each read.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public FakeClock(Instant initial, Duration autoAdvance)
Parameters
Type | Name | Description |
---|---|---|
Instant | initial | The initial instant. |
Duration | autoAdvance | The duration to advance the clock on each read. |
See Also
Properties
AutoAdvance
Gets the amount of time to advance the clock by on each call to read the current time.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public Duration AutoAdvance { get; set; }
Property Value
Type | Description |
---|---|
Duration | The amount of time to advance the clock by on each call to read the current time. |
Remarks
This defaults to zero, with the exception of the FakeClock(Instant, Duration) constructor, which takes the initial value directly. If this is zero, the current time as reported by this clock will not change other than by calls to Reset(Instant) or to one of the Advance(Duration) methods.
The value could even be negative, to simulate particularly odd system clock effects.
See Also
Methods
Advance(Duration)
Advances the clock by the given duration.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void Advance(Duration duration)
Parameters
Type | Name | Description |
---|---|---|
Duration | duration | The duration to advance the clock by (or if negative, the duration to move it back by). |
AdvanceDays(Int32)
Advances the clock by the given number of standard (24-hour) days.
Since 1.4.x
Availability net45, netstandard1.3
Declaration
public void AdvanceDays(int days)
Parameters
Type | Name | Description |
---|---|---|
Int32 | days | The number of days to advance the clock by (or if negative, the number to move it back by). |
AdvanceHours(Int32)
Advances the clock by the given number of hours.
Since 1.4.x
Availability net45, netstandard1.3
Declaration
public void AdvanceHours(int hours)
Parameters
Type | Name | Description |
---|---|---|
Int32 | hours | The number of hours to advance the clock by (or if negative, the number to move it back by). |
AdvanceMilliseconds(Int64)
Advances the clock by the given number of milliseconds.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void AdvanceMilliseconds(long milliseconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | milliseconds | The number of milliseconds to advance the clock by (or if negative, the number to move it back by). |
AdvanceMinutes(Int64)
Advances the clock by the given number of minutes.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void AdvanceMinutes(long minutes)
Parameters
Type | Name | Description |
---|---|---|
Int64 | minutes | The number of minutes to advance the clock by (or if negative, the number to move it back by). |
AdvanceNanoseconds(Int64)
Advances the clock by the given number of nanoseconds.
Since 2.0.x
Availability net45, netstandard1.3
Declaration
public void AdvanceNanoseconds(long nanoseconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | nanoseconds | The number of nanoseconds to advance the clock by (or if negative, the number to move it back by). |
AdvanceSeconds(Int64)
Advances the clock by the given number of seconds.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void AdvanceSeconds(long seconds)
Parameters
Type | Name | Description |
---|---|---|
Int64 | seconds | The number of seconds to advance the clock by (or if negative, the number to move it back by). |
AdvanceTicks(Int64)
Advances the clock by the given number of ticks.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void AdvanceTicks(long ticks)
Parameters
Type | Name | Description |
---|---|---|
Int64 | ticks | The number of ticks to advance the clock by (or if negative, the number to move it back by). |
FromUtc(Int32, Int32, Int32)
Returns a fake clock initially set to midnight of the given year/month/day in UTC in the ISO calendar.
The value of the AutoAdvance property will be initialised to zero.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public static FakeClock FromUtc(int year, int monthOfYear, int dayOfMonth)
Parameters
Type | Name | Description |
---|---|---|
Int32 | year | The year. This is the "absolute year", so a value of 0 means 1 BC, for example. |
Int32 | monthOfYear | The month of year. |
Int32 | dayOfMonth | The day of month. |
Returns
Type | Description |
---|---|
FakeClock | A FakeClock initialised to the given instant, with no auto-advance. |
FromUtc(Int32, Int32, Int32, Int32, Int32, Int32)
Returns a fake clock initially set to the given year/month/day/time in UTC in the ISO calendar.
The value of the AutoAdvance property will be initialised to zero.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public static FakeClock FromUtc(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute)
Parameters
Type | Name | Description |
---|---|---|
Int32 | year | The year. This is the "absolute year", so a value of 0 means 1 BC, for example. |
Int32 | monthOfYear | The month of year. |
Int32 | dayOfMonth | The day of month. |
Int32 | hourOfDay | The hour. |
Int32 | minuteOfHour | The minute. |
Int32 | secondOfMinute | The second. |
Returns
Type | Description |
---|---|
FakeClock | A FakeClock initialised to the given instant, with no auto-advance. |
GetCurrentInstant()
Returns the "current time" for this clock. Unlike a normal clock, this
property may return the same value from repeated calls until one of the methods
to change the time is called.
Since 2.0.x
Availability net45, netstandard1.3
Declaration
public Instant GetCurrentInstant()
Returns
Type | Description |
---|---|
Instant | The "current time" from this (fake) clock. |
Remarks
If the value of the AutoAdvance property is non-zero, then every
call to this method will advance the current time by that value.
Reset(Instant)
Resets the clock to the given instant.
The value of the AutoAdvance property will be unchanged.
Since 1.0.x
Availability net45, netstandard1.3
Declaration
public void Reset(Instant instant)
Parameters
Type | Name | Description |
---|---|---|
Instant | instant | The instant to set the clock to. |