ntp 1.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 88

pub package

ntp

Plugin that allows you to get precise time from Network Time Protocol (NTP). Whole NTP protocol is implemented in dart.

This is useful for time-based events since DateTime.now() returns time of the device. Users sometimes change their internal clock and using DateTime.now() can give really 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.getNtpTime(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> getNtpTime({
    String lookUpAddress: 'pool.ntp.org',
    int port: 123,
    DateTime localTime,
  });
  Future<DateTime> now();

[1.0.1] - 22.07.2018.

  • Added example
  • Updated README.md

[1.0.0] - 18.07.2018.

  • Methods in NTP are now static.

  • Added now() for returning current DateTime object with calculated NTP offset

  • getNtpTime was renamed to getNtpOffset (since it is returning int offset in milliseconds)

[0.0.1] - 16.04.2018.

  • Get NTP clock offset

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:ntp/ntp.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DateTime _currentTime;
  DateTime _ntpTime;
  int _ntpOffset;

  @override
  void initState() {
    super.initState();

    _updateTime();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Example of NTP library'),
        ),
        body: Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              _showData('Current time:', '$_currentTime'),
              _showData('NTP offset:', '$_ntpOffset ms'),
              _showData('NTP time:', '$_ntpTime'),
            ],
          ),
        ),
        floatingActionButton: new FloatingActionButton(
          tooltip: 'Update time',
          child: const Icon(Icons.timer),
          onPressed: _updateTime,
        ),
      ),
    );
  }

  Widget _showData(String title, String value) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          new Text(title,
              style: Theme
                  .of(context)
                  .textTheme
                  .title
                  .copyWith(fontWeight: FontWeight.w500, fontSize: 16.0)),
          new Text(value,
              style: Theme
                  .of(context)
                  .textTheme
                  .title
                  .copyWith(fontWeight: FontWeight.w300, fontSize: 16.0)),
        ],
      ),
    );
  }

  void _updateTime() async {
    _currentTime = DateTime.now();

    NTP.getNtpOffset().then((int value) {
      setState(() {
        _ntpOffset = value;
        _ntpTime = _currentTime.add(Duration(milliseconds: _ntpOffset));
      });
    });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ntp: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ntp/ntp.dart';
  
Version Uploaded Documentation Archive
1.0.1 Jul 22, 2018 Go to the documentation of ntp 1.0.1 Download ntp 1.0.1 archive
1.0.0 Jul 18, 2018 Go to the documentation of ntp 1.0.0 Download ntp 1.0.0 archive
0.0.2 Jun 8, 2018 Go to the documentation of ntp 0.0.2 Download ntp 0.0.2 archive
0.0.1 Apr 16, 2018 Go to the documentation of ntp 0.0.1 Download ntp 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
77
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8
  • Flutter: 0.5.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Format lib/ntp/ntp.dart.

Run flutter format to format lib/ntp/ntp.dart.

Format lib/ntp/ntp_message.dart.

Run flutter format to format lib/ntp/ntp_message.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.11
meta 1.1.5 1.1.6
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.8
Dev dependencies
flutter_test