Documentation
¶
Overview ¶
Package ntp implements a clock.Clock backed by an NTP-calibrated time source.
It periodically (or on demand) queries an NTP server to estimate the offset between the local system clock and the server's clock, and reports a corrected time via Now. Timer and Ticker behave the same as clock.System.
Index ¶
Examples ¶
Constants ¶
const DefaultServer = "pool.ntp.org"
DefaultServer is the NTP server used when Options.Server is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock is a clock.Clock that corrects the local time using an offset estimated from an NTP server.
It is safe for concurrent use.
func New ¶
New creates a network Clock and performs the initial synchronization.
Example ¶
package main
import (
"fmt"
"github.com/gotd/td/clock"
"github.com/gotd/td/clock/ntp"
"github.com/gotd/td/telegram"
)
func main() {
// Create an NTP-calibrated clock.
c, err := ntp.New(ntp.Options{
Server: ntp.DefaultServer,
})
if err != nil {
panic(err)
}
fmt.Printf("clock offset: %s\n", c.Offset())
// Use it as the time source for the client.
_ = telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{
Clock: c,
})
// Re-calibrate periodically if the process runs for a long time.
_ = c.Sync()
var _ clock.Clock = c
}
Output:
func (*Clock) Offset ¶
Offset returns the last recorded offset between the NTP server clock and the local clock.
type Options ¶
type Options struct {
// Server is the NTP server address to query.
//
// Defaults to DefaultServer.
Server string
// Now is the local time source the offset is applied to.
//
// Defaults to time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
Options configures the network Clock.