ntp

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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/iamxvbaba/td/clock"
	"github.com/iamxvbaba/td/clock/ntp"
	"github.com/iamxvbaba/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