NyTime class

NyTime - Time manipulation utilities for testing.

This class allows you to freeze time, travel to specific dates, and control the flow of time in your tests.

Example:

NyTime.setTestNow(DateTime(2025, 1, 1));
expect(NyTime.now().year, 2025);
NyTime.reset();

Constructors

NyTime()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

isFrozen bool
Checks if test time is currently active.
no setter
testNow DateTime?
Get the current test time or null if using real time.
no setter

Static Methods

advanceBy(Duration duration) → void
Travel forward in time by a duration.
freeze() → void
Freeze time at the current moment.
now() DateTime
Returns the current DateTime. If a test time is set, returns that instead of the real time.
reset() → void
Resets the time to use the real system time.
rewindBy(Duration duration) → void
Travel backward in time by a duration.
setTestNow(DateTime date) → void
Sets a fixed time for testing. All subsequent calls to now() will return this time.
travelToEndOfDay() → void
Travel to end of current day.
travelToEndOfMonth() → void
Travel to end of current month.
travelToEndOfYear() → void
Travel to end of current year.
travelToStartOfDay() → void
Travel to start of current day.
travelToStartOfMonth() → void
Travel to start of current month.
travelToStartOfYear() → void
Travel to start of current year.
withFrozenTime<T>(DateTime time, Future<T> callback()) Future<T>
Execute a callback with a specific frozen time, then restore.
withFrozenTimeSync<T>(DateTime time, T callback()) → T
Synchronous version of withFrozenTime.