ntp

package
v0.157.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 5 Imported by: 0

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.

See https://core.telegram.org/mtproto#time-synchronization.

Index

Examples

Constants

View Source
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

func New(opts Options) (*Clock, error)

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
}

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns the corrected current time.

func (*Clock) Offset

func (c *Clock) Offset() time.Duration

Offset returns the last recorded offset between the NTP server clock and the local clock.

func (*Clock) Sync

func (c *Clock) Sync() error

Sync queries the NTP server and updates the recorded clock offset.

func (*Clock) Ticker

func (c *Clock) Ticker(d time.Duration) clock.Ticker

Ticker returns a ticker, behaving the same as clock.System.

func (*Clock) Timer

func (c *Clock) Timer(d time.Duration) clock.Timer

Timer returns a timer, behaving the same as clock.System.

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.

Jump to

Keyboard shortcuts

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