Help us shape the future of Go! Take the 2026 Go Developer Survey

timeutil

package
v1.21.2 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package timeutil provides centralized timezone handling for the application. Call LoadProcessLocation once during initialization to set the canonical timezone, then use Now() and Location() for business times and database values instead of time.Now() and time.Local. time.Now stays right for what no zone affects: durations, deadlines and Unix timestamps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadLocation

func LoadLocation(tzName string) error

LoadLocation loads the timezone by name (e.g., "Asia/Shanghai", "UTC"). Must be called once at startup before any other function in this package. An unknown name is an error and leaves the current timezone in place.

func LoadProcessLocation added in v1.21.0

func LoadProcessLocation(tzName string) error

LoadProcessLocation loads the timezone like LoadLocation and makes it the process's local timezone as well, so time.Now, time.Unix and every library reading time.Local (GORM's automatic timestamps among them) keep a single clock with Now. It matters for PostgreSQL: a zone-less timestamp column stores the written value's wall clock, so values written in two zones stop comparing. Call it once at startup, before other goroutines read the time.

func Location

func Location() *time.Location

Location returns the canonical application timezone. Falls back to time.Local if LoadLocation was never called.

func LocationName

func LocationName() string

LocationName returns the configured timezone name, or "Local" when LoadLocation was never called.

func Now

func Now() time.Time

Now returns the current time in the application timezone. Falls back to time.Now() if LoadLocation was never called.

Types

This section is empty.

Jump to

Keyboard shortcuts

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