temporal

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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)

func ParseDate

func ParseDate(text string) (Date, error)

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)

func (Date) Day

func (d Date) Day() int

func (Date) IsZero

func (d Date) IsZero() bool

func (Date) MarshalText

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

func (Date) Month

func (d Date) Month() time.Month

func (Date) String

func (d Date) String() string

func (*Date) UnmarshalJSON

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

func (*Date) UnmarshalText

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

func (Date) Year

func (d Date) Year() int

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)

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)

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)

func (DateTime) DateIn

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

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

func (DateTime) LocalIn

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

func (DateTime) MarshalText

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

func (DateTime) String

func (v DateTime) String() string

func (DateTime) UTC

func (v DateTime) UTC() time.Time

func (DateTime) UnixMicro

func (v DateTime) UnixMicro() int64

func (DateTime) UnixMilli

func (v DateTime) UnixMilli() int64

func (*DateTime) UnmarshalJSON

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

func (*DateTime) UnmarshalText

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

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

func (Interval) Elapsed

func (v Interval) Elapsed() time.Duration

func (Interval) MarshalText

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

func (Interval) Months

func (v Interval) Months() int32

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

func (*Interval) UnmarshalText

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

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)

func ParseLocalDateTime

func ParseLocalDateTime(text string) (LocalDateTime, error)

func (LocalDateTime) Add

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

func (LocalDateTime) Date

func (v LocalDateTime) Date() Date

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

func (LocalDateTime) MarshalText

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

func (LocalDateTime) String

func (v LocalDateTime) String() string

func (LocalDateTime) Time

func (v LocalDateTime) Time() Time

func (*LocalDateTime) UnmarshalJSON

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

func (*LocalDateTime) UnmarshalText

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

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)

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)

func (Service) Format

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

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)

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)

func (Service) Parse

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

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

func (Service) Today

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

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)

func ParseTime

func ParseTime(text string) (Time, error)

func (Time) Hour

func (v Time) Hour() int

func (Time) MarshalText

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

func (Time) Minute

func (v Time) Minute() int

func (Time) Nanosecond

func (v Time) Nanosecond() int

func (Time) Second

func (v Time) Second() int

func (Time) String

func (v Time) String() string

func (*Time) UnmarshalJSON

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

func (*Time) UnmarshalText

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

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