ntp 1.0.7

Dart native
Flutter Android iOS

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

pub package CodeFactor

NTP #

Plugin that allows you to get precise time from Network Time Protocol (NTP). It implements whole NTP protocol in dart.

This is useful for time-based events since DateTime.now() returns the time of the device. Users sometimes change their internal clock and using DateTime.now() can give wrong result. You can just get clock offset [NTP.getNtpTime] and apply it manually to DateTime.now() object when needed (just add offset as milliseconds duration), or you can get already formatted [DateTime] object from [NTP.now].

By default lookup address for NTP is: pool.ntp.org

For example on how to use look in github library repository example/ folder.

How it works #

Using int offset from getNtpTime()

  • default localTime is DateTime.now()
  • default lookUpAddress is 'pool.ntp.org'
  • default port is 123
  DateTime startDate = new DateTime().now().toLocal();
  int offset = await NTP.getNtpOffset(localTime: startDate);
  print('NTP DateTime offset align: ${startDate.add(new Duration(milliseconds: offset))}');

Using DateTime from now

  DateTime startDate = await NTP.now();
  print('NTP DateTime: ${startDate}');

NTP Functions #

  Future<int> getNtpOffset({
    String lookUpAddress: 'pool.ntp.org',
    int port: 123,
    DateTime localTime,
  });
  Future<DateTime> now();
18
likes
90
pub points
92%
popularity

Publisher

square-pear.com

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD (LICENSE)

Dependencies

universal_io

More

Packages that depend on ntp