Code Monkey home page Code Monkey logo

ntp-time-sync's Introduction

NPM downloads Build Status NPM version

ntp-time-sync

Node.JS module to fetch the current time from NTP servers and returns offset information.

ℹī¸ NTP requires UDP which is not available in a browser context!

Installation

# using Yarn
$ yarn add ntp-time-sync

# using NPM
$ npm install ntp-time-sync

Usage

Consider using the library as a singleton, so that not every call to getTime fires new NTP packages. The library itself will manage minimum/maximum poll times.

Several requests to multiple NTP time servers are fired and the responses will be aggregated.

// ES6:
import { NtpTimeSync } from "ntp-time-sync";
// pre-ES6:
// const NtpTimeSync = require("ntp-time-sync").NtpTimeSync;

const timeSync = NtpTimeSync.getInstance();

// request 1
timeSync.getTime().then(function (result) {
  console.log("current system time", new Date());
  console.log("real time", result.now);
  console.log("offset in milliseconds", result.offset);
})

// request 2, will use cached offset from previous request
timeSync.getTime().then(function (result) {
  console.log("current system time", new Date());
  console.log("real time", result.now);
  console.log("offset in milliseconds", result.offset);
})

// ES2017 style
const result = await timeSync.getTime();
console.log("real time", result.now);

<ntpTimeSyncInstance>.getTime() returns a Promise object which will eventually be resolved with a object containing the following information:

Property Description
now Current NTP time ("real time")
offset Calculated offset between local system time and NTP time

<ntpTimeSyncInstance>.now() returns a Date object containing the correct time for the moment when the function was called. In contrast to getTime(), which will return the correct time for the moment the Promise gets resolved.

Options

You can pass custom options to the constructor of NtpTimeSync or NtpTimeSync.getInstance(options). These will be merged with the following defaults:

const defaultOptions = {
  // list of NTP time servers, optionally including a port (defaults to 123)
  servers: [
    "0.pool.ntp.org",
    "1.pool.ntp.org",
    "2.pool.ntp.org",
    "3.pool.ntp.org"
  ],

  // required amount of valid samples in order to calculate the time
  sampleCount: 8,

  // amount of time in milliseconds to wait for a single NTP response
  replyTimeout: 3000,

  // defaults as of RFC5905
  ntpDefaults: {
    port: 123,
    version: 4,
    tolerance: 15e-6,
    minPoll: 4,
    maxPoll: 17,
    maxDispersion: 16,
    minDispersion: 0.005,
    maxDistance: 1,
    maxStratum: 16,
    precision: -18,
    referenceDate: new Date("Jan 01 1900 GMT")
  }
};

ntp-time-sync's People

Contributors

buffcode avatar dependabot[bot] avatar gallynero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ntp-time-sync's Issues

Connection error from past 2 days

I am getting the below error from past 2 days. This module was giving correct result one month back.
Error: Connection error: Unable to get any NTP response after 3 retries
at NtpTimeSync.collectSamples (vendor.bundle.js:119319)

defaultOptions fails

const NtpTimeSync = require("ntp-time-sync").NtpTimeSync;

const defaultOptions = {
  // list of NTP time servers, optionally including a port (defaults to 123)
  servers: [
    "0.pool.ntp.org",
    "1.pool.ntp.org",
    "2.pool.ntp.org",
    "3.pool.ntp.org"
  ],

  // required amount of valid samples in order to calculate the time
  sampleCount: 8,

  // amount of time in milliseconds to wait for a single NTP response
  replyTimeout: 3000,

  // defaults as of RFC5905
  ntpDefaults: {
    port: 123,
    version: 4,
    tolerance: 15e-6,
    minPoll: 4,
    maxPoll: 17,
    maxDispersion: 16,
    minDispersion: 0.005,
    maxDistance: 1,
    maxStratum: 16,
    precision: -18,
    referenceDate: new Date("Jan 01 1900 GMT")
  }
};

const timeSync = NtpTimeSync.getInstance(defaultOptions);

timeSync.getTime().then(function (result) {
  console.log("current system time", new Date());
  console.log("real time", result.now);
  console.log("offset in milliseconds", result.offset);
})

result:

Uncaught Error Error: Connection error: Unable to get any NTP response after 3 retries

After switching to

const timeSync = NtpTimeSync.getInstance();

works, however, I would like to use different NTP servers.

Thanks, very cool library :)

Dependency not found

Hi,

I'm getting the following error:

This dependency was not found:

* dgram in ./node_modules/ntp-time-sync/dist/index.js

To install it, you can run: npm install --save dgram

Same error after installing dgram. Looks like it's related to the fact that dgram is not longer a project on npm.

Thanks

Add support for NodeJS 20.xx LTS

Hey there,

I ran into an issue trying to install this module with the latest LTS version of NodeJS. Here's the error message I got:

error [email protected]: The engine "node" is incompatible with this module. Expected version "^14 || ^16 || ^18". Got "20.10.0"

Any chance we could get support for NodeJS 20?

Thanks a bunch for your work!

package is useless .getInstance is not a function

installed with npm i ntp-time-sync
referenced with import NtpTimeSync from 'ntp-time-sync';
called at const timeSync = NtpTimeSync.getInstance();

Error: index.js:221
const timeSync = NtpTimeSync.getInstance();
^
TypeError: NtpTimeSync.getInstance is not a function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.