/flutter-kronos

Flutter Kronos

Primary LanguageDartApache License 2.0Apache-2.0

Kronos - NTP plugin for Flutter

pub package pub package pub package

Unlike the device clock, the time reported by Kronos is unaffected when the local time is changed while your app is running. Instead, Kronos stores accurate time along with a delta between the NTP time and the system uptime. Since uptime increases monotonically, Kronos isn't affected by device time changes. Accessing KronosClock.getCurrentTimeMs() will return the local time based on the last known accurate time + delta since last sync.

Since it relies on system uptime, Kronos detects and requires a new sync after each reboot.

This plugin is based on Kronos in iOS and Kronos-Android in android for the whole SNTP protocol implementation.

Android Example

Installation

Just add flutter_kronos as a dependency in your pubspec.yaml file.

Android

Add the following permission in your Android Manifest file:

<uses-permission android:name="android.permission.INTERNET"/>

Usage

Sync clock using a pool of NTP servers

Calling FlutterKronos.sync will fire a bunch of NTP requests to up to 5 of the servers on the given NTP pool. As soon as we get the first response, the given closure is called but the FlutterKronos will keep trying to get a more accurate response.

FlutterKronos.sync();

Get an NTP time

int? FlutterKronos.getCurrentTimeMs(); //return time from the fallback clock if Kronos has not yet been synced
int? FlutterKronos.getCurrentNtpTimeMs(); //return null if Kronos has not yet been synced
DateTime? FlutterKronos.getDateTime(); //return null if Kronos has not yet been synced
DateTime? FlutterKronos.getNtpDateTime(); //return null if Kronos has not yet been synced