ntp

package
v0.6.0-alpha.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2024 License: GPL-2.0 Imports: 15 Imported by: 58

Documentation

Overview

Package ntp provides an implementation of a Simple NTP (SNTP) client capable of querying the current time from a remote NTP server. See RFC5905 (https://tools.ietf.org/html/rfc5905) for more details.

This approach grew out of a go-nuts post by Michael Hofmann: https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/FlcdMU5fkLQ

Index

Constants

View Source
const (
	// LeapNoWarning indicates no impending leap second.
	LeapNoWarning LeapIndicator = 0

	// LeapAddSecond indicates the last minute of the day has 61 seconds.
	LeapAddSecond = 1

	// LeapDelSecond indicates the last minute of the day has 59 seconds.
	LeapDelSecond = 2

	// LeapNotInSync indicates an unsynchronized leap second.
	LeapNotInSync = 3
)
View Source
const TimeLayout = "2006-01-02 15:04:05 -0700"

Variables

This section is empty.

Functions

func SetSystemTime added in v0.4.0

func SetSystemTime(nowTime time.Time) error

func TimeFuncFromContext added in v0.2.3

func TimeFuncFromContext(ctx context.Context) func() time.Time

Types

type LeapIndicator

type LeapIndicator uint8

The LeapIndicator is used to warn if a leap second should be inserted or deleted in the last minute of the current month.

type Options added in v0.2.3

type Options struct {
	Context       context.Context
	Dialer        N.Dialer
	Logger        logger.Logger
	Server        M.Socksaddr
	Interval      time.Duration
	Timeout       time.Duration
	WriteToSystem bool
}

type Response

type Response struct {
	// Time is the transmit time reported by the server just before it
	// responded to the client's NTP query.
	Time time.Time

	// ClockOffset is the estimated offset of the client clock relative to
	// the server. Add this to the client's system clock time to obtain a
	// more accurate time.
	ClockOffset time.Duration

	// RTT is the measured round-trip-time delay estimate between the client
	// and the server.
	RTT time.Duration

	// Precision is the reported precision of the server's clock.
	Precision time.Duration

	// Stratum is the "stratum level" of the server. The smaller the number,
	// the closer the server is to the reference clock. Stratum 1 servers are
	// attached directly to the reference clock. A stratum value of 0
	// indicates the "kiss of death," which typically occurs when the client
	// issues too many requests to the server in a short period of time.
	Stratum uint8

	// ReferenceID is a 32-bit identifier identifying the server or
	// reference clock.
	ReferenceID uint32

	// ReferenceTime is the time when the server's system clock was last
	// set or corrected.
	ReferenceTime time.Time

	// RootDelay is the server's estimated aggregate round-trip-time delay to
	// the stratum 1 server.
	RootDelay time.Duration

	// RootDispersion is the server's estimated maximum measurement error
	// relative to the stratum 1 server.
	RootDispersion time.Duration

	// RootDistance is an estimate of the total synchronization distance
	// between the client and the stratum 1 server.
	RootDistance time.Duration

	// Leap indicates whether a leap second should be added or removed from
	// the current month's last minute.
	Leap LeapIndicator

	// MinError is a lower bound on the error between the client and server
	// clocks. When the client and server are not synchronized to the same
	// clock, the reported timestamps may appear to violate the principle of
	// causality. In other words, the NTP server's response may indicate
	// that a message was received before it was sent. In such cases, the
	// minimum error may be useful.
	MinError time.Duration

	// KissCode is a 4-character string describing the reason for a
	// "kiss of death" response (stratum = 0). For a list of standard kiss
	// codes, see https://tools.ietf.org/html/rfc5905#section-7.4.
	KissCode string

	// Poll is the maximum interval between successive NTP polling messages.
	// It is not relevant for simple NTP clients like this one.
	Poll time.Duration
}

A Response contains time data, some of which is returned by the NTP server and some of which is calculated by the client.

func Exchange

func Exchange(ctx context.Context, dialer N.Dialer, serverAddress M.Socksaddr) (*Response, error)

func (*Response) Validate

func (r *Response) Validate() error

Validate checks if the response is valid for the purposes of time synchronization.

type Service added in v0.2.3

type Service struct {
	// contains filtered or unexported fields
}

func NewService added in v0.2.3

func NewService(options Options) *Service

func (*Service) Close added in v0.2.3

func (s *Service) Close() error

func (*Service) Start added in v0.2.3

func (s *Service) Start() error

func (*Service) TimeFunc added in v0.2.3

func (s *Service) TimeFunc() func() time.Time

type TimeService added in v0.2.3

type TimeService interface {
	TimeFunc() func() time.Time
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL