ntp 2.0.0 copy "ntp: ^2.0.0" to clipboard
ntp: ^2.0.0 copied to clipboard

Fetch accurate time or current DateTime object from (NTP) Network Time Protocol server.

example/main.dart

import 'package:ntp/ntp.dart';

Future<void> main() async {
  [
    'time.google.com',
    'time.facebook.com',
    'time.euro.apple.com',
    'pool.ntp.org',
  ].forEach(_checkTime);
}

Future<void> _checkTime(String lookupAddress) async {
  DateTime _myTime;
  DateTime _ntpTime;

  /// Or you could get NTP current (It will call DateTime.now() and add NTP offset to it)
  _myTime = DateTime.now();

  /// Or get NTP offset (in milliseconds) and add it yourself
  final int offset =
      await NTP.getNtpOffset(localTime: _myTime, lookUpAddress: lookupAddress);

  _ntpTime = _myTime.add(Duration(milliseconds: offset));

  print('\n==== $lookupAddress ====');
  print('My time: $_myTime');
  print('NTP time: $_ntpTime');
  print('Difference: ${_myTime.difference(_ntpTime).inMilliseconds}ms');

  return;
}
236
likes
150
points
75.6k
downloads

Publisher

verified publishersquare-pear.com

Weekly Downloads

Fetch accurate time or current DateTime object from (NTP) Network Time Protocol server.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on ntp