ntp

package
v0.0.0-...-29b0027 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2020 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

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 NtpHost = "40.83.72.70"

const NtpHost = "0.beevik-ntp.pool.ntp.org"

View Source
const NtpHost1 = "pool.ntp.org"
View Source
const NtpHost2 = "cn.ntp.org.cn"
View Source
const NtpHost3 = "time.windows.com"
View Source
const NtpHost4 = "time.pool.aliyun.com"
View Source
const NtpHost5 = "ntp1.aliyun.com"
View Source
const NtpHost6 = "ntp2.aliyun.com"

Variables

This section is empty.

Functions

func CheckLocalTimeIsNtp

func CheckLocalTimeIsNtp() error

try 10 times again

func SetNtpToLocal

func SetNtpToLocal(ntpTimeStamp int64) error

func Time

func Time(host string) (time.Time, error)

Time returns the current time using information from a remote NTP server. It uses version 4 of the NTP protocol. On error, it returns the local system time.

func TimeV deprecated

func TimeV(host string, version int) (time.Time, error)

TimeV returns the current time using information from a remote NTP server. On error, it returns the local system time. The version may be 2, 3, or 4.

Deprecated: TimeV is deprecated. Use QueryWithOptions instead.

Types

type Clock

type Clock interface {
	After(d time.Duration) <-chan time.Time
	AfterFunc(d time.Duration, f func()) *Timer
	Now() time.Time
	Since(t time.Time) time.Duration
	Sleep(d time.Duration)
	Tick(d time.Duration) <-chan time.Time
	Ticker(d time.Duration) *Ticker
	Timer(d time.Duration) *Timer
}

Clock represents an interface to the functions in the standard library time package. Two implementations are available in the clock package. The first is a real-time clock which simply wraps the time package's functions. The second is a mock clock which will only make forward progress when programmatically adjusted.

func New

func New() Clock

New returns an instance of a real-time clock.

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 Mock

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

Mock represents a mock clock that only moves forward programmically. It can be preferable to a real-time clock when testing time-based functionality.

func NewMock

func NewMock() *Mock

NewMock returns an instance of a mock clock. The current time of the mock clock on initialization is the Unix epoch.

func NewNtpClock

func NewNtpClock() (*Mock, error)

ntp clock

func (*Mock) Add

func (m *Mock) Add(d time.Duration)

Add moves the current time of the mock clock forward by the duration. This should only be called from a single goroutine at a time.

func (*Mock) After

func (m *Mock) After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel.

Example
// Create a new mock clock.
clock := NewMock()
count := 0

ready := make(chan struct{})
// Create a channel to execute after 10 mock seconds.
go func() {
	ch := clock.After(10 * time.Second)
	close(ready)
	<-ch
	count = 100
}()
<-ready

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

// Move the clock forward 5 seconds and print the value again.
clock.Add(5 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

// Move the clock forward 5 seconds to the tick time and check the value.
clock.Add(5 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Output:

1970-01-01 00:00:00 +0000 UTC: 0
1970-01-01 00:00:05 +0000 UTC: 0
1970-01-01 00:00:10 +0000 UTC: 100

func (*Mock) AfterFunc

func (m *Mock) AfterFunc(d time.Duration, f func()) *Timer

AfterFunc waits for the duration to elapse and then executes a function. A Timer is returned that can be stopped.

Example
// Create a new mock clock.
clock := NewMock()
count := 0

// Execute a function after 10 mock seconds.
clock.AfterFunc(10*time.Second, func() {
	count = 100
})
gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Output:

1970-01-01 00:00:00 +0000 UTC: 0
1970-01-01 00:00:10 +0000 UTC: 100

func (*Mock) Now

func (m *Mock) Now() time.Time

Now returns the current wall time on the mock clock.

func (*Mock) Set

func (m *Mock) Set(t time.Time)

Set sets the current time of the mock clock to a specific one. This should only be called from a single goroutine at a time.

func (*Mock) Since

func (m *Mock) Since(t time.Time) time.Duration

Since returns time since the mock clocks wall time.

func (*Mock) Sleep

func (m *Mock) Sleep(d time.Duration)

Sleep pauses the goroutine for the given duration on the mock clock. The clock must be moved forward in a separate goroutine.

Example
// Create a new mock clock.
clock := NewMock()
count := 0

// Execute a function after 10 mock seconds.
go func() {
	clock.Sleep(10 * time.Second)
	count = 100
}()
gosched()

// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
Output:

1970-01-01 00:00:00 +0000 UTC: 0
1970-01-01 00:00:10 +0000 UTC: 100

func (*Mock) Tick

func (m *Mock) Tick(d time.Duration) <-chan time.Time

Tick is a convenience function for Ticker(). It will return a ticker channel that cannot be stopped.

func (*Mock) Ticker

func (m *Mock) Ticker(d time.Duration) *Ticker

Ticker creates a new instance of Ticker.

Example
// Create a new mock clock.
clock := NewMock()
count := 0

ready := make(chan struct{})
// Increment count every mock second.
go func() {
	ticker := clock.Ticker(1 * time.Second)
	close(ready)
	for {
		<-ticker.C
		count++
	}
}()
<-ready

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("Count is %d after 10 seconds\n", count)

// Move the clock forward 5 more seconds and print the new value.
clock.Add(5 * time.Second)
fmt.Printf("Count is %d after 15 seconds\n", count)
Output:

Count is 10 after 10 seconds
Count is 15 after 15 seconds

func (*Mock) Timer

func (m *Mock) Timer(d time.Duration) *Timer

Timer creates a new instance of Timer.

Example
// Create a new mock clock.
clock := NewMock()
count := 0

ready := make(chan struct{})
// Increment count after a mock second.
go func() {
	timer := clock.Timer(1 * time.Second)
	close(ready)
	<-timer.C
	count++
}()
<-ready

// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("Count is %d after 10 seconds\n", count)
Output:

Count is 1 after 10 seconds

type QueryOptions

type QueryOptions struct {
	Timeout      time.Duration // defaults to 5 seconds
	Version      int           // NTP protocol version, defaults to 4
	LocalAddress string        // IP address to use for the client address
	Port         int           // Server port, defaults to 123
	TTL          int           // IP TTL to use, defaults to system default
}

QueryOptions contains the list of configurable options that may be used with the QueryWithOptions function.

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 Query

func Query(host string) (*Response, error)

Query returns a response from the remote NTP server host. It contains the time at which the server transmitted the response as well as other useful information about the time and the remote server.

func QueryWithOptions

func QueryWithOptions(host string, opt QueryOptions) (*Response, error)

QueryWithOptions performs the same function as Query but allows for the customization of several query options.

func (*Response) Validate

func (r *Response) Validate() error

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

type Ticker

type Ticker struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

Ticker holds a channel that receives "ticks" at regular intervals.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off the ticker.

type Timer

type Timer struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

Timer represents a single event. The current time will be sent on C, unless the timer was created by AfterFunc.

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

Reset changes the expiry time of the timer

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop turns off the ticker.

Jump to

Keyboard shortcuts

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