npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ntp-time-sync

v0.4.1

Published

Fetches the current time from NTP servers and returns offset information

Downloads

6,494

Readme

NPM downloads Build Status NPM version

ntp-time-sync

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

:information_source: 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")
  }
};