Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fetches the current time from NTP servers and returns detailed offset information

License

NotificationsYou must be signed in to change notification settings

buffcode/ntp-time-sync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM downloadsBuild StatusNPM 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 togetTime 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;consttimeSync=NtpTimeSync.getInstance();// request 1timeSync.getTime().then(function(result){console.log("current system time",newDate());console.log("real time",result.now);console.log("offset in milliseconds",result.offset);})// request 2, will use cached offset from previous requesttimeSync.getTime().then(function(result){console.log("current system time",newDate());console.log("real time",result.now);console.log("offset in milliseconds",result.offset);})// ES2017 styleconstresult=awaittimeSync.getTime();console.log("real time",result.now);

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

PropertyDescription
nowCurrent NTP time ("real time")
offsetCalculated offset between local system time and NTP time

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

Options

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

constdefaultOptions={// 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 timesampleCount:8,// amount of time in milliseconds to wait for a single NTP responsereplyTimeout:3000,// defaults as of RFC5905ntpDefaults:{port:123,version:4,tolerance:15e-6,minPoll:4,maxPoll:17,maxDispersion:16,minDispersion:0.005,maxDistance:1,maxStratum:16,precision:-18,referenceDate:newDate("Jan 01 1900 GMT")}};

About

Fetches the current time from NTP servers and returns detailed offset information

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp