timezone

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 9 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 timezone is the timezone behavior: the timezone of the host.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"timezone", "The timezone of the host",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Config),
	behavior.CLI,
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.Read("get", Get).
		Describe("Get the timezone of the host").
		Explain("Reads the IANA name of the timezone and its offset from UTC now: the "+
			"zone that /etc/localtime links to, or /etc/timezone on Linux.").
		Example("A Linux host on UTC", GetInput{}, Entry{Timezone: "UTC", UTCOffset: "+00:00"},
			behavior.OnPlatform(behavior.PlatformLinux),
			behavior.Exists("/etc/timezone", "UTC\n"),
		).
		Example("A Mac on UTC", GetInput{}, Entry{Timezone: "UTC", UTCOffset: "+00:00"},
			behavior.OnPlatform(behavior.PlatformDarwin),
			behavior.Links("/etc/localtime", "/var/db/timezone/zoneinfo/UTC"),
		),
	behavior.Write("update", Update).
		On(behavior.PlatformDebian).
		Runs("timedatectl").
		Sudo("timedatectl").
		Describe("Set the timezone of the host").
		Explain("Sets the timezone with timedatectl set-timezone. A host that already "+
			"has the timezone does not change and reports changed: false. It runs on "+
			"the Debian family only, and elsewhere reports skipped.").
		Example("Set a new timezone", UpdateInput{Timezone: "America/New_York"},
			Result{Timezone: "America/New_York", Changed: true},
			behavior.Prints(show, "UTC\n"),
			behavior.PrintsAfter("timedatectl set-timezone America/New_York", show,
				"America/New_York\n"),
			behavior.Ran("timedatectl", "set-timezone", "America/New_York"),
			behavior.Reads(GetInput{}, Entry{Timezone: "America/New_York"}),
		).
		Example("A host that already has the timezone", UpdateInput{Timezone: "UTC"},
			Result{Timezone: "UTC"},
			behavior.After(UpdateInput{Timezone: "UTC"}),
			behavior.Prints(show, "America/New_York\n"),
			behavior.PrintsAfter("timedatectl set-timezone UTC", show, "UTC\n"),
			behavior.Reads(GetInput{}, Entry{Timezone: "UTC"}),
		).
		Fails("A name that is not a timezone", UpdateInput{Timezone: "../../etc/passwd"},
			"not a valid timezone name").
		Fails("A name that starts with a dash", UpdateInput{Timezone: "-UTC"},
			"not a valid timezone name").
		Fails("A name longer than 64 characters",
			UpdateInput{Timezone: strings.Repeat("a", 65)}, "timezone"),
))

Behavior is the timezone of the host. gohai reads it on every platform; timedatectl sets it on the Debian family.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Timezone  string `json:"timezone"   doc:"The IANA name of the timezone, such as America/New_York"`
	UTCOffset string `json:"utc_offset" doc:"The offset from UTC now, such as -05:00"`
}

Entry is the timezone of the host.

func Get

func Get(
	ctx context.Context,
	p Provider,
	_ GetInput,
) (Entry, error)

Get is the timezone of the host.

type GetInput

type GetInput struct{}

GetInput takes nothing: the timezone is of the whole host.

type Provider

type Provider interface {
	// Timezone is the timezone of the host now.
	Timezone(ctx context.Context) (Entry, error)
	// SetTimezone sets the timezone, and reports whether it changed.
	SetTimezone(ctx context.Context, name string) (bool, error)
}

Provider reads and sets the timezone of the host.

type Result

type Result struct {
	Timezone string `json:"timezone" doc:"The timezone of the host"`
	Changed  bool   `json:"changed"  doc:"Whether the host changed"`
}

Result is the timezone after the update.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update sets the timezone of the host.

type UpdateInput

type UpdateInput struct {
	Timezone string `` /* 142-byte string literal not displayed */
}

UpdateInput is the timezone to set.

Directories

Path Synopsis
Package mocks holds the generated double of the timezone provider, for the methods' tests.
Package mocks holds the generated double of the timezone provider, for the methods' tests.

Jump to

Keyboard shortcuts

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