timesync

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 7 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 timesync reads a host's clock-synchronization status through an injected exec.Runner.

r, _ := exec.NewRunner(exec.Direct)
m, err := timesync.New(timesync.Chrony, r)
if err != nil { ... }
st, _ := m.Status(ctx)
fmt.Println(st.Synchronized, st.Source, st.OffsetSeconds)

Two backends are implemented: Timedatectl (systemd; reports synchronized + service-enabled) and Chrony (chronyc; reports synchronized + reference source + estimated offset). Reads are unprivileged. Detect lists the tools on PATH; the consumer picks one and passes it to New (no auto-detection, no globals).

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrUnknownBackend = errors.New("timesync: unknown backend")

ErrUnknownBackend is returned by New for the zero value or any unimplemented backend.

Functions

This section is empty.

Types

type Backend

type Backend int

Backend selects the time-sync daemon to query. The zero value is invalid; only implemented backends exist (ntpd/openntpd are intentionally absent).

const (
	// Timedatectl queries systemd via `timedatectl show`.
	Timedatectl Backend = iota + 1
	// Chrony queries chrony via `chronyc -c tracking`.
	Chrony
)

func Detect

func Detect(ctx context.Context) []Backend

Detect reports the time-sync backends usable on THIS host: Timedatectl when timedatectl is on PATH, Chrony when chronyc is. It lists; it never picks. The consumer reads the list, picks one, and passes it to New.

The ctx is accepted for signature uniformity; the probe is a pure PATH lookup.

func (Backend) String

func (b Backend) String() string

String renders the backend as its canonical tool name.

type Manager

type Manager interface {
	// Status reads the current synchronization state.
	Status(ctx context.Context) (Status, error)
}

Manager is the time-sync read surface.

func New

func New(b Backend, runner exec.Runner) (Manager, error)

New returns a Manager for the named backend, driven by runner. Pure: validates the backend; does not probe (use Detect). Nil runner and unknown backend are rejected.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/manchtools/cadestro/sdk/sys/exec"
	"github.com/manchtools/cadestro/sdk/sys/timesync"
)

func main() {
	r, err := exec.NewRunner(exec.Direct)
	if err != nil {
		log.Fatal(err)
	}
	m, err := timesync.New(timesync.Chrony, r)
	if err != nil {
		log.Fatal(err)
	}
	st, err := m.Status(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("synchronized=%v source=%s offset=%.6fs\n", st.Synchronized, st.Source, st.OffsetSeconds)
}

type Status

type Status struct {
	Synchronized  bool    // the clock is currently disciplined to a source
	Enabled       bool    // a time-sync service is enabled/running
	Source        string  // reference source (server name/IP)
	OffsetSeconds float64 // estimated offset from true time (signed)
}

Status is a snapshot of the host clock's synchronization state. Fields a backend cannot report are left at their zero value (Timedatectl does not expose source/offset; both are populated by Chrony).

Jump to

Keyboard shortcuts

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