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

temporal

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package temporal separates UTC instants, calendar dates, wall times, and local date-times. Value operations return new values; decoding replaces the receiver only on success. Application time comes from the clock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadTimeZone

func LoadTimeZone(name string) (*time.Location, error)

LoadTimeZone resolves a bounded, explicit IANA name or UTC. It excludes fixed offsets, empty names and the process-dependent Local value. Use ParseTimeZone when a boundary accepts both named zones and fixed offsets.

func ParseTimeZone

func ParseTimeZone(text string) (*time.Location, error)

ParseTimeZone resolves UTC, an IANA location or a fixed +/-HH:MM offset. Named locations use Go's configured timezone database. Empty and Local are rejected so application behavior does not silently depend on the host zone. The returned standard Go location works directly with DateTime.LocalIn and LocalDateTime.In. Fixed offsets must be smaller than 24 hours.

Types

type Date

type Date struct {
	// contains filtered or unexported fields
}

Date is a calendar date without a time or timezone. The zero value is absent and cannot be serialized. Supported years are 1 through 9999.

func NewDate

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

NewDate validates and constructs a calendar date in years one through 9999.

func ParseDate

func ParseDate(text string) (Date, error)

ParseDate parses a calendar date in YYYY-MM-DD format.

func Today

func Today(source clock.Clock, zone *time.Location) (Date, error)

Today uses calendar boundaries in the requested timezone, including DST.

func (Date) AddDays

func (d Date) AddDays(days int) (Date, error)

AddDays adds calendar days, rejecting absent dates and results outside supported years.

func (Date) Day

func (d Date) Day() int

Day returns the day of the month.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero reports whether the date is absent.

func (Date) MarshalText

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

MarshalText encodes the date as text, rejecting an absent date.

func (Date) Month

func (d Date) Month() time.Month

Month returns the calendar month.

func (Date) String

func (d Date) String() string

String returns the date in YYYY-MM-DD format.

func (*Date) UnmarshalJSON

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

UnmarshalJSON decodes a JSON date, replacing the receiver only on success.

func (*Date) UnmarshalText

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

UnmarshalText parses date text, replacing the receiver only on success.

func (Date) Year

func (d Date) Year() int

Year returns the calendar year.

type DateTime

type DateTime struct {
	// contains filtered or unexported fields
}

DateTime is an immutable UTC instant at microsecond precision, normalized without a monotonic reading. Its zero value is Go's zero time (0001-01-01T00:00:00Z), not database null.

func NewDateTime

func NewDateTime(instant time.Time) (DateTime, error)

NewDateTime converts an instant to UTC at microsecond precision, stripping monotonic time and checking supported years.

func Now

func Now(source clock.Clock) (DateTime, error)

Now reads an explicit application clock. It does not change the system clock or consult a process-global timezone. Callback failure returns no instant.

func ParseDateTime

func ParseDateTime(text string) (DateTime, error)

ParseDateTime parses an RFC3339 date-time with an explicit offset and normalizes it through NewDateTime.

func ParseDateTimeIn

func ParseDateTimeIn(text string, zone *time.Location) (DateTime, error)

ParseDateTimeIn accepts an explicit RFC3339 offset first, otherwise resolves a local wall time using the existing strict gap/overlap policy for zone.

func (DateTime) Add

func (v DateTime) Add(duration time.Duration) (DateTime, error)

Add adds a duration and validates the resulting instant.

func (DateTime) DateIn

func (v DateTime) DateIn(zone *time.Location) (Date, error)

DateIn returns the calendar date in the supplied time zone.

func (DateTime) FormatIn

func (v DateTime) FormatIn(zone *time.Location) (string, error)

FormatIn preserves the selected offset in an RFC3339Nano representation.

func (DateTime) IsZero

func (v DateTime) IsZero() bool

IsZero reports whether the underlying instant is the zero time.

func (DateTime) LocalIn

func (v DateTime) LocalIn(zone *time.Location) (LocalDateTime, error)

LocalIn returns local wall-clock fields in the supplied nonnil time zone.

func (DateTime) MarshalText

func (v DateTime) MarshalText() ([]byte, error)

MarshalText encodes the instant's RFC3339 string as text.

func (DateTime) String

func (v DateTime) String() string

String returns the UTC instant in RFC3339 format with fractional seconds as needed.

func (DateTime) UTC

func (v DateTime) UTC() time.Time

UTC returns the underlying UTC instant.

func (DateTime) UnixMicro

func (v DateTime) UnixMicro() int64

UnixMicro returns microseconds since the Unix epoch.

func (DateTime) UnixMilli

func (v DateTime) UnixMilli() int64

UnixMilli returns milliseconds since the Unix epoch.

func (*DateTime) UnmarshalJSON

func (v *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON date-time, replacing the receiver only on success.

func (*DateTime) UnmarshalText

func (v *DateTime) UnmarshalText(data []byte) error

UnmarshalText parses date-time text, replacing the receiver only on success.

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

Interval keeps calendar months/days separate from elapsed time. A calendar day need not be 24 hours when applied to an instant in a timezone. Zero is a zero interval. Values are immutable and retain signed components.

func Days

func Days(n int32) Interval

Days constructs a signed calendar-day interval, not an elapsed-hour duration.

func Elapsed

func Elapsed(d time.Duration) (Interval, error)

Elapsed constructs an elapsed-time interval with whole microseconds.

func Months

func Months(n int32) Interval

Months constructs a signed calendar-month interval.

func NewInterval

func NewInterval(months, days int32, elapsed time.Duration) (Interval, error)

NewInterval constructs calendar and elapsed components without normalizing them into one another. Sub-microsecond precision is rejected.

func ParseInterval

func ParseInterval(text string) (Interval, error)

ParseInterval accepts String's canonical representation, ISO 8601 designator intervals and PostgreSQL's four output styles. Calendar components are whole numbers; only seconds may be fractional, to whole-microsecond precision. Input is bounded and components are checked without floating-point rounding.

func (Interval) Days

func (v Interval) Days() int32

Days returns the calendar-day component.

func (Interval) Elapsed

func (v Interval) Elapsed() time.Duration

Elapsed returns the elapsed-duration component.

func (Interval) MarshalText

func (v Interval) MarshalText() ([]byte, error)

MarshalText encodes the interval's string representation as text.

func (Interval) Months

func (v Interval) Months() int32

Months returns the calendar-month component.

func (Interval) String

func (v Interval) String() string

String retains every signed component in an unambiguous interval spelling.

func (*Interval) UnmarshalJSON

func (v *Interval) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON interval, replacing the receiver only on success.

func (*Interval) UnmarshalText

func (v *Interval) UnmarshalText(data []byte) error

UnmarshalText parses interval text, replacing the receiver only on success.

type LocalDateTime

type LocalDateTime struct {
	// contains filtered or unexported fields
}

LocalDateTime preserves a calendar date and wall time at microsecond precision without assuming any timezone. Zero is absent. Converting to an instant requires an explicit zone.

func NewLocalDateTime

func NewLocalDateTime(date Date, clock Time) (LocalDateTime, error)

NewLocalDateTime combines a present calendar date with a wall-clock time.

func ParseLocalDateTime

func ParseLocalDateTime(text string) (LocalDateTime, error)

ParseLocalDateTime parses local date and time fields separated by T or a space.

func (LocalDateTime) Add

func (v LocalDateTime) Add(duration time.Duration) (LocalDateTime, error)

Add adds a duration to wall-clock fields, rejecting an absent value or unsupported result year.

func (LocalDateTime) Date

func (v LocalDateTime) Date() Date

Date returns the calendar date component.

func (LocalDateTime) In

func (v LocalDateTime) In(zone *time.Location) (DateTime, error)

In resolves a unique instant. DST gaps fail with Invalid; repeated wall times fail with Conflict. Supply an explicit offset to ParseDateTime to disambiguate. Zone offsets outside +/-24 hours are rejected; standard IANA zones fit within this bound. No machine-local timezone or arbitrary DST choice is used.

func (LocalDateTime) IsZero

func (v LocalDateTime) IsZero() bool

IsZero reports whether the local date-time is absent.

func (LocalDateTime) MarshalText

func (v LocalDateTime) MarshalText() ([]byte, error)

MarshalText encodes the local date-time as text, rejecting an absent value.

func (LocalDateTime) String

func (v LocalDateTime) String() string

String returns date and time fields separated by T without a time zone.

func (LocalDateTime) Time

func (v LocalDateTime) Time() Time

Time returns the wall-clock time component.

func (*LocalDateTime) UnmarshalJSON

func (v *LocalDateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON local date-time, replacing the receiver only on success.

func (*LocalDateTime) UnmarshalText

func (v *LocalDateTime) UnmarshalText(data []byte) error

UnmarshalText parses local date-time text, replacing the receiver only on success.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service binds immutable calendar helpers to one application clock and zone. Retain it in domain constructors. Instants and their JSON remain UTC; local parsing, presentation and calendar arithmetic use this service's timezone.

func NewService

func NewService(source clock.Clock, name ZoneName) (Service, error)

NewService validates a clock and resolves the named time zone for a date service.

func (Service) AddDays

func (s Service) AddDays(instant DateTime, days int) (DateTime, error)

AddDays preserves local wall time across calendar days, which need not be 24 elapsed hours. A resulting DST gap/overlap remains an explicit error.

func (Service) Date

func (s Service) Date(instant DateTime) (Date, error)

Date returns the instant's calendar date in the service's time zone.

func (Service) Format

func (s Service) Format(instant DateTime) (string, error)

Format formats the instant in the service's time zone.

func (Service) In

func (s Service) In(name ZoneName) (Service, error)

In returns another service borrowing the same clock with an explicit override.

func (Service) Local

func (s Service) Local(instant DateTime) (LocalDateTime, error)

Local returns the instant's local fields in the service's time zone.

func (Service) Location

func (s Service) Location() *time.Location

Location returns an owned standard Go location for APIs needing explicit zones.

func (Service) Now

func (s Service) Now() (DateTime, error)

Now reads the service's clock as a DateTime.

func (Service) Parse

func (s Service) Parse(text string) (DateTime, error)

Parse parses date-time text using the service's time zone.

func (Service) StartOfDay

func (s Service) StartOfDay(instant DateTime) (DateTime, error)

StartOfDay resolves local midnight with the existing strict DST policy. Historical midnight gaps/overlaps return errors instead of guessing an instant.

func (Service) TimeZone

func (s Service) TimeZone() ZoneName

TimeZone returns the service's time zone name.

func (Service) Today

func (s Service) Today() (Date, error)

Today returns today's date using the service's clock and time zone.

type Time

type Time struct {
	// contains filtered or unexported fields
}

Time is a wall-clock time at microsecond precision without a date or offset. Zero means midnight. Leap seconds and a 24:00 representation are rejected.

func NewTime

func NewTime(hour, minute, second, nanosecond int) (Time, error)

NewTime validates wall-clock fields and truncates fractional seconds to microsecond precision.

func ParseTime

func ParseTime(text string) (Time, error)

ParseTime parses HH:MM:SS with optional fractional seconds and normalizes through NewTime.

func (Time) Hour

func (v Time) Hour() int

Hour returns the hour of the day.

func (Time) MarshalText

func (v Time) MarshalText() ([]byte, error)

MarshalText encodes the wall-clock time as text.

func (Time) Minute

func (v Time) Minute() int

Minute returns the minute within the hour.

func (Time) Nanosecond

func (v Time) Nanosecond() int

Nanosecond returns the fractional second in nanoseconds.

func (Time) Second

func (v Time) Second() int

Second returns the second within the minute.

func (Time) String

func (v Time) String() string

String returns the wall-clock time with fractional seconds as needed.

func (*Time) UnmarshalJSON

func (v *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a JSON wall-clock time, replacing the receiver only on success.

func (*Time) UnmarshalText

func (v *Time) UnmarshalText(data []byte) error

UnmarshalText parses wall-clock text, replacing the receiver only on success.

type ZoneName

type ZoneName string

ZoneName selects an explicit IANA zone, UTC or a fixed +/-HH:MM offset. It is distinct from a locale or arbitrary configuration string.

const UTC ZoneName = "UTC"

func (ZoneName) Location

func (name ZoneName) Location() (*time.Location, error)

Location validates the name and returns an owned location value. Modifying the returned value cannot change another application's selected timezone.

Jump to

Keyboard shortcuts

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