timeutil

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 8 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 timeutil provides helpers for parsing timestamp strings used throughout the platform's data layer. All functions expect the ISO 8601 / RFC 3339 format YYYY-MM-DDTHH:MM:SSZ (UTC, no fractional seconds). This is the canonical format returned by MySQL's DATETIME columns when serialized to JSON or gRPC string fields.

Index

Constants

View Source
const DateLayout = "2006-01-02"

DateLayout is the wire format of a Date: a calendar day with no time or zone.

View Source
const FanOutReserve = 1500 * time.Millisecond

FanOutReserve is the headroom BudgetedContext leaves between the end of a fan-out and the caller's own deadline: enough to assemble and serialize a response, and to survive the gap between when a goroutine observes cancellation and when the handler actually returns.

Variables

View Source
var ErrInvalidDate = errors.New("expected YYYY-MM-DD")

ErrInvalidDate is wrapped by every error parsing a Date, so a caller can tell a malformed date from other bad input.

Functions

func BudgetedContext

func BudgetedContext(ctx context.Context, reserve time.Duration) (context.Context, context.CancelFunc)

BudgetedContext derives a context that expires far enough before ctx's own deadline for the caller to still return a response.

It exists for fan-outs over slow dependencies — a live carrier-rating round trip, a per-type search RPC — where an individual participant failing is already handled by dropping its contribution. Without a budget those fan-outs inherit the request deadline exactly, so one stalled participant spends the whole of it and the caller returns a 504 with nothing in it, discarding every result that did arrive. Cutting the fan-out short instead turns a total failure into a partial one.

reserve is the headroom to leave; pass FanOutReserve unless the assembly step after the fan-out is unusually expensive. When ctx carries no deadline, or when less than reserve remains (the request is already over budget and about to be abandoned), ctx is returned with a no-op cancel and the caller runs unbounded — a fan-out that cannot be shortened usefully should not be truncated to nothing.

func LookupZone

func LookupZone(country, state string) (string, bool)

LookupZone reports the zone a country and subdivision keep time in, and whether one is known.

The subdivision is consulted only for the countries that genuinely span zones. Everywhere else the country answer is exact, which is why most of the table is keyed on country alone.

func TimestampToTime

func TimestampToTime(timestamp string) time.Time

TimestampToTime parses a UTC timestamp string (YYYY-MM-DDTHH:MM:SSZ) into a time.Time value. Returns the zero time (time.Time{}) if the string does not match the expected format. Use TimestampToTimePtr when you need to distinguish between "missing" (nil) and "zero time".

func TimestampToTimePtr

func TimestampToTimePtr(timestamp string) *time.Time

TimestampToTimePtr parses a UTC timestamp string (YYYY-MM-DDTHH:MM:SSZ) into a *time.Time. Returns nil if the string does not match the expected format, making it safe to use directly in struct assignments where a nil pointer represents "not set" (e.g. optional processed_at or expires_at fields).

func ZoneFor

func ZoneFor(stored *string, country, state, fallback string) *time.Location

ZoneFor resolves the zone a place keeps time in, most trustworthy source first.

A stored zone wins outright. It is the only source a person can correct, and the derivation below is deliberately approximate — thirteen US states span two zones, and the majority answer is wrong for the minority of addresses in them. Persisting the derived value and letting it be edited is what makes those addresses fixable at all.

The fallback chain ends at the account's own zone rather than UTC, because an account's addresses cluster near it: a Denver plant shipping to an unrecognised state is far better served by Denver time than by UTC.

Types

type Date

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

Date is a business day, the value of an `_on` field. It names a day on the account's calendar rather than an instant, so it carries no time zone; turn it into an instant with Start in the account's location.

The zero Date is not a valid day. Use a pointer or field.Optional for a date that may be absent.

func DateOf

func DateOf(t time.Time) Date

DateOf returns the calendar day t falls on in t's own location. Convert t with In first to get the day in another zone.

func NewDate

func NewDate(year int, month time.Month, day int) Date

NewDate returns the date, normalizing out-of-range months and days the way time.Date does (January 32 is February 1).

func ParseDate

func ParseDate(s string) (Date, error)

ParseDate parses a YYYY-MM-DD string. It rejects days that do not exist, such as 2025-02-30.

func (Date) AddDays

func (d Date) AddDays(n int) Date

AddDays returns the date n calendar days later, or earlier when n is negative.

func (Date) After

func (d Date) After(other Date) bool

After reports whether d is a later day than other.

func (Date) Before

func (d Date) Before(other Date) bool

Before reports whether d is an earlier day than other.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether d is the zero Date.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON encodes the date as a "YYYY-MM-DD" string.

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

MarshalText encodes the date as YYYY-MM-DD, which also covers query parameters and map keys.

func (*Date) Scan

func (d *Date) Scan(src any) error

Scan reads a DATE column, which drivers return as a time.Time (with parseTime) or as text.

func (Date) Start

func (d Date) Start(loc *time.Location) time.Time

Start returns the instant the day begins in loc.

func (Date) String

func (d Date) String() string

String returns the date as YYYY-MM-DD.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

UnmarshalJSON parses a "YYYY-MM-DD" string. A JSON null leaves d unchanged, matching encoding/json's handling of other values.

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(b []byte) error

UnmarshalText parses a YYYY-MM-DD date.

func (Date) Value

func (d Date) Value() (driver.Value, error)

Value stores the date in a DATE column.

Jump to

Keyboard shortcuts

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