AccurateTime class

A static class to manage accurate UTC time using HTTP/UDP synchronization and local caching.

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

cachedOffset → Duration?
Returns the current accurate cached offset (server time - local time).
no setter
isTampered → bool
Returns true if the local device wall-clock has diverged from the monotonic clock by more than 2 seconds since the last successful sync.
no setter
lastFailedAttempt → DateTime?
Returns the local time when a synchronization attempt last failed.
no setter
lastSyncDetails → NtpResponse?
Returns the full NtpResponse telemetry from the last successful sync.
no setter
lastSyncTime → DateTime?
Returns the local time when the last successful synchronization occurred.
no setter
lastSyncTimeOrNull → DateTime
Returns the local time when the last successful synchronization occurred.
no setter
ntpClientFactory ↔ NtpClient Function({bool isUtc, int port, String server, int timeout})?
Factory function to create an NtpClient instance. Primarily used to inject mock/stub clients during unit testing.
getter/setter pair
onClockTampered → Stream<Duration>
Broadcast stream that emits the detected clock skew Duration whenever a manual system clock change or jump (> 2s) is detected.
no setter
retryCooldown → Duration
The cooldown duration before retrying after a failed sync.
no setter
syncInterval → Duration
Interval between scheduled resynchronizations.
no setter

Static Methods

clearCache() → void
Clears the cached NTP offset and synchronization time.
now({bool isUtc = false, NtpServer server = NtpServer.google, String? customServer, List<NtpServer>? fallbackServers, int port = 123, int timeout = 5, int samples = 1, bool forceRefresh = false, bool allowFallback = true}) → Future<DateTime>
Returns the current accurate time.
nowSync({bool isUtc = false, NtpServer server = NtpServer.google, String? customServer, List<NtpServer>? fallbackServers, int port = 123, int timeout = 5}) → DateTime
Returns the current accurate time synchronously using the cached offset.
nowToIsoString({bool isUtc = true, NtpServer server = NtpServer.google, String? customServer, List<NtpServer>? fallbackServers, int port = 123, int timeout = 5, int samples = 1, bool forceRefresh = false, bool allowFallback = true}) → Future<String>
Returns the current accurate time as an ISO 8601 string.
query({bool isUtc = false, NtpServer server = NtpServer.google, String? customServer, List<NtpServer>? fallbackServers, int port = 123, int timeout = 5, int samples = 1, bool forceRefresh = false}) → Future<NtpResponse>
Queries accurate time and returns a full NtpResponse with telemetry (offset, round-trip delay, stratum, referenceId, etc.).
restoreCache({required Duration offset, DateTime? syncTime}) → void
Restores a previously saved offset (e.g. from SharedPreferences or secure storage) to enable offline-first accuracy on application startup.
setRetryCooldown(Duration duration) → void
Updates the cooldown duration before retrying after a failed sync.
setSyncInterval(Duration newInterval) → void
Updates the duration used to determine when to resync the time.
stream({Duration interval = const Duration(seconds: 1), bool isUtc = false, NtpServer server = NtpServer.google, String? customServer, int port = 123, int timeout = 5}) → Stream<DateTime>
Emits a continuous stream of accurate DateTime values at interval.