ntp_dart 1.1.1 copy "ntp_dart: ^1.1.1" to clipboard
ntp_dart: ^1.1.1 copied to clipboard

Lightweight Dart package to fetch accurate UTC time from NTP servers with no external dependencies.

example/main.dart

import 'package:ntp_dart/ntp_dart.dart';

void main() async {
  // Optional: reduce the sync interval for demonstration purposes
  AccurateTime.setSyncInterval(Duration(seconds: 5));

  print('LOCAL SYSTEM TIME: ${DateTime.now().toUtc()}');
  print('INITIAL SYNC TIME: ${AccurateTime.nowSync()}');
  print('----------------------------------------------------');

  // Fetch the current UTC time directly from the NTP server.
  final ntpNow = await NtpClient().now();
  print('NTP CLIENT TIME:   $ntpNow');
  print('----------------------------------------------------');

  // Demonstrate cached AccurateTime over multiple calls
  for (int i = 1; i <= 3; i++) {
    final accurateNow = await AccurateTime.now();
    print('ACCURATE TIME [$i]: $accurateNow');
    await Future.delayed(Duration(seconds: 1));
  }

  print('----------------------------------------------------');
  print('Waiting to exceed sync interval...');
  await Future.delayed(Duration(seconds: 6));

  // After sync interval, AccurateTime will re-sync NTP
  final accurateResync = await AccurateTime.now();
  print('ACCURATE TIME (resynced): $accurateResync');
}
2
likes
150
points
272
downloads

Publisher

verified publisherenzodesimone.dev

Weekly Downloads

Lightweight Dart package to fetch accurate UTC time from NTP servers with no external dependencies.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

http, intl

More

Packages that depend on ntp_dart