temporal

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 10 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 temporal implements the time values of stdlib.md's Time namespace after JavaScript's Temporal: an Instant on the time line, a ZonedDateTime in a zone, the plain wall-clock types, zones, and durations, with nanosecond precision and Temporal's range of 100 million days each way from the epoch. It depends on nothing of the interpreter; the Time namespace wraps it. Every type is immutable and compared by value, and renders as its RFC 9557 string.

Index

Constants

This section is empty.

Variables

View Source
var ErrRange = errors.New("outside the range of time values")

ErrRange is the error of a value outside Temporal's range.

View Source
var UTC = mustZone("UTC")

UTC is the zone UTC.

Functions

func CompareDurations

func CompareDurations(a, b Duration, rel RelativeTo) (int, error)

CompareDurations orders two durations by length: with a zoned point for any date unit, with a date for years, months or weeks, and days as 24 hours otherwise.

func TzdataVersion

func TzdataVersion() string

TzdataVersion is the version of the IANA database the zone table was generated from; the rules themselves are the host's or Go's.

Types

type Dated

type Dated interface{ Date() PlainDate }

Dated is a value with a date: a ZonedDateTime, a PlainDate or a PlainDateTime.

type Disambiguation

type Disambiguation int

Disambiguation says which instant a wall-clock time in a gap or an overlap means.

const (
	Compatible Disambiguation = iota // the later instant in a gap, the earlier in an overlap
	Earlier
	Later
)

type Duration

type Duration struct {
	Years, Months, Weeks, Days              int64
	Hours, Minutes, Seconds                 int64
	Milliseconds, Microseconds, Nanoseconds int64
}

Duration is years to nanoseconds, all of one sign, kept as given.

func AddDurations

func AddDurations(a, b Duration) (Duration, error)

AddDurations sums two durations with no years, months or weeks, days at 24 hours, balanced up to the larger of their largest units.

func DurationLiteral added in v0.2.0

func DurationLiteral(s string) (Duration, error)

DurationLiteral is the value of a duration literal as written in source, underscores included: one or more components of a decimal number and a unit of h, m, s, ms, us or ns, the units each once and in descending order, a fraction only in the last component, and a fraction that comes out as a whole number of nanoseconds. The error says what is wrong, for a syntax error.

func FromGoDuration added in v0.3.3

func FromGoDuration(d time.Duration) Duration

FromGoDuration is a Go duration as hours, minutes, seconds, milliseconds, microseconds and nanoseconds, each below the next unit, as Go prints one. Every Go duration is in range.

func NewDuration

func NewDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds int64) (Duration, error)

NewDuration makes a duration, rejecting mixed signs and the fields Temporal's range excludes: years, months, weeks and days at or beyond 2^32, and a time part, days included at 24 hours, at or beyond 2^53 seconds.

func ParseDuration

func ParseDuration(s string) (Duration, error)

ParseDuration reads ISO 8601 when the first character after an optional sign is P, and Go's time.ParseDuration notation otherwise.

func (Duration) Abs

func (d Duration) Abs() (Duration, error)

Abs makes every field non-negative, an error where Negate is.

func (Duration) Exact

func (d Duration) Exact() (time.Duration, bool)

Exact is the duration as a Go duration, taken as Time.total takes one without a relativeTo: hours and smaller units exact and a day 24 hours. It is false when the duration has years, months or weeks, which have no fixed length. A duration beyond Go's range, about 292 years either way, is taken as that range.

func (Duration) Key

func (d Duration) Key() string

Key is the canonical form == compares: the ten fields as given.

func (Duration) Negate

func (d Duration) Negate() (Duration, error)

Negate flips every field; a field of -2^63, which a subsecond field may hold, has no positive counterpart that fits an int, and is an error.

func (Duration) Part

func (d Duration) Part(u Unit) int64

Part is the field of a unit, as given.

func (Duration) Round

func (d Duration) Round(smallest Unit, hasSmallest bool, largest Unit, hasLargest bool, inc int64, mode RoundingMode, rel RelativeTo) (Duration, error)

Round balances the duration up to largest and rounds it to a multiple of inc of smallest; hasSmallest and hasLargest say which were given, and at least one must be.

func (Duration) Sign

func (d Duration) Sign() int

Sign is -1, 0 or 1.

func (Duration) String

func (d Duration) String() string

String is the ISO 8601 form, the fields as given and the subsecond fields folded into the seconds: P1Y2M3DT4H5M6.5S, -PT1H, PT0S.

func (Duration) TimeType

func (Duration) TimeType() string

TimeType is the name of the type's tag.

func (Duration) Total

func (d Duration) Total(unit Unit, rel RelativeTo) (float64, error)

Total is the whole duration in a unit, fraction included.

type Fields

type Fields struct {
	Year, Month, Day                     *int
	Hour, Minute, Second                 *int
	Millisecond, Microsecond, Nanosecond *int
}

Fields are the fields with may replace; a nil pointer keeps the value's own.

type Instant

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

Instant is an exact moment: nanoseconds since the Unix epoch, held as seconds and the nanosecond within the second, which spans the whole range.

func FromEpochMillis

func FromEpochMillis(ms int64) (Instant, error)

FromEpochMillis makes an instant from milliseconds since the epoch.

func FromEpochNanos

func FromEpochNanos(ns int64) Instant

FromEpochNanos makes an instant from nanoseconds since the epoch; every int64 is in range.

func FromGoTime added in v0.3.0

func FromGoTime(t time.Time) Instant

FromGoTime is the moment of a time Go's clock read, such as a timer's, on the clock Now reads.

func NewInstant

func NewInstant(sec, nsec int64) (Instant, error)

NewInstant makes an instant from seconds and nanoseconds since the epoch, the nanoseconds of any sign and size, and reports ErrRange beyond the range.

func Now

func Now() Instant

Now is the current moment.

func ParseInstant

func ParseInstant(s string) (Instant, error)

ParseInstant reads a date, a time and Z or an offset; a bracketed zone is ignored.

func PossibleInstants

func PossibleInstants(dt PlainDateTime, z *Zone) ([]Instant, error)

PossibleInstants is the instants a wall-clock time has in a zone: none in a gap, two in an overlap, one otherwise, in order. It is Temporal's GetPossibleEpochNanoseconds: the offsets in force a day before and a day after the time, each tried.

func (Instant) Add

func (i Instant) Add(dur Duration) (Instant, error)

Add moves the instant by the time part; a date unit is an error, since an instant has no calendar.

func (Instant) Compare

func (i Instant) Compare(j Instant) int

Compare orders instants: -1, 0 or 1.

func (Instant) EpochMillis

func (i Instant) EpochMillis() int64

EpochMillis is the milliseconds since the epoch, rounded down.

func (Instant) EpochNanos

func (i Instant) EpochNanos() (int64, error)

EpochNanos is the nanoseconds since the epoch, or ErrRange when they do not fit an int64, which is outside 1677-09-21 to 2262-04-11.

func (Instant) GoTime

func (i Instant) GoTime() time.Time

GoTime is i as a Go time, in UTC.

func (Instant) InZone

func (i Instant) InZone(z *Zone) ZonedDateTime

InZone is the instant seen in a zone.

func (Instant) Key

func (i Instant) Key() string

Key is the canonical form == compares.

func (Instant) Round

func (i Instant) Round(unit Unit, inc int64, mode RoundingMode) (Instant, error)

Round rounds the nanoseconds since the epoch.

func (Instant) String

func (i Instant) String() string

String is the RFC 9557 form in UTC: 2026-10-22T14:03:05.5Z.

func (Instant) TimeType

func (Instant) TimeType() string

TimeType is the name of the type's tag.

func (Instant) Until

func (i Instant) Until(o Instant, largest Unit) (Duration, error)

Until is the duration from i to o, balanced up to largest, which is Hour or smaller.

type PlainDate

type PlainDate struct {
	Year, Month, Day int
}

PlainDate is a calendar date with no zone.

func NewPlainDate

func NewPlainDate(year, month, day int) (PlainDate, error)

NewPlainDate makes a date, or reports an error for a month or day that is not there, or a date beyond the range, which is the range of instants and a day more each way.

func ParsePlainDate

func ParsePlainDate(s string) (PlainDate, error)

ParsePlainDate reads a date, alone or at the start of a longer string.

func (PlainDate) Add

func (d PlainDate) Add(dur Duration) (PlainDate, error)

Add moves the date by the duration's date part, the time part counted as whole days of 24 hours with the remainder dropped.

func (PlainDate) Compare

func (d PlainDate) Compare(e PlainDate) int

Compare orders dates.

func (PlainDate) Date

func (d PlainDate) Date() PlainDate

Date returns the date itself; it makes a PlainDate a Dated.

func (PlainDate) DayOfWeek

func (d PlainDate) DayOfWeek() int

DayOfWeek is 1 Monday to 7 Sunday.

func (PlainDate) DayOfYear

func (d PlainDate) DayOfYear() int

DayOfYear is 1 to 366.

func (PlainDate) DaysInMonth

func (d PlainDate) DaysInMonth() int

DaysInMonth is 28 to 31.

func (PlainDate) DaysInYear

func (d PlainDate) DaysInYear() int

DaysInYear is 365 or 366.

func (PlainDate) InLeapYear

func (d PlainDate) InLeapYear() bool

InLeapYear reports a leap year.

func (PlainDate) Key

func (d PlainDate) Key() string

Key is the canonical form == compares.

func (PlainDate) String

func (d PlainDate) String() string

String is 2026-10-22.

func (PlainDate) TimeType

func (PlainDate) TimeType() string

TimeType is the name of the type's tag.

func (PlainDate) Until

func (d PlainDate) Until(o PlainDate, largest Unit) (Duration, error)

Until is the duration from d to o, in units of Day or larger, counted from d.

func (PlainDate) WeekOfYear

func (d PlainDate) WeekOfYear() int

WeekOfYear is the ISO week, 1 to 53.

func (PlainDate) With

func (d PlainDate) With(f Fields) (PlainDate, error)

With replaces the given date fields, constrained; a time field is an error, and so is a month or a day below 1, which Temporal's "constrain" rejects rather than clamps.

func (PlainDate) YearOfWeek

func (d PlainDate) YearOfWeek() int

YearOfWeek is the year of the ISO week, which differs from Year around New Year.

type PlainDateTime

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

PlainDateTime is a date and a time with no zone.

func NewPlainDateTime

func NewPlainDateTime(d PlainDate, t PlainTime) PlainDateTime

NewPlainDateTime joins a date and a time.

func ParsePlainDateTime

func ParsePlainDateTime(s string) (PlainDateTime, error)

ParsePlainDateTime reads a date and a time.

func (PlainDateTime) Add

func (dt PlainDateTime) Add(dur Duration) (PlainDateTime, error)

Add adds the time part to the clock, carrying days, then the date part to the date.

func (PlainDateTime) Clock

func (dt PlainDateTime) Clock() PlainTime

Clock is the time part; it makes a PlainDateTime a Timed.

func (PlainDateTime) Compare

func (dt PlainDateTime) Compare(e PlainDateTime) int

Compare orders date-times.

func (PlainDateTime) Date

func (dt PlainDateTime) Date() PlainDate

Date is the date part; it makes a PlainDateTime a Dated.

func (PlainDateTime) Key

func (dt PlainDateTime) Key() string

Key is the canonical form == compares.

func (PlainDateTime) Round

func (dt PlainDateTime) Round(unit Unit, inc int64, mode RoundingMode) (PlainDateTime, error)

Round rounds the time of day, carrying into the date.

func (PlainDateTime) String

func (dt PlainDateTime) String() string

String is 2026-10-22T14:03:05.

func (PlainDateTime) TimeType

func (PlainDateTime) TimeType() string

TimeType is the name of the type's tag.

func (PlainDateTime) Until

func (dt PlainDateTime) Until(o PlainDateTime, largest Unit) (Duration, error)

Until is the duration from dt to o, with days of 24 hours.

func (PlainDateTime) With

func (dt PlainDateTime) With(f Fields) (PlainDateTime, error)

With replaces the given fields, constrained.

type PlainTime

type PlainTime struct {
	Hour, Minute, Second, Nano int
}

PlainTime is a wall-clock time with no date; Nano is the nanosecond within the second.

func NewPlainTime

func NewPlainTime(hour, minute, second, millisecond, microsecond, nanosecond int) (PlainTime, error)

NewPlainTime makes a time from its six fields, each checked against its range.

func ParsePlainTime

func ParsePlainTime(s string) (PlainTime, error)

ParsePlainTime reads a time, alone or after a date.

func (PlainTime) Add

func (t PlainTime) Add(dur Duration) (PlainTime, error)

Add moves the time around the clock; the date part is ignored.

func (PlainTime) Clock

func (t PlainTime) Clock() PlainTime

Clock returns the time itself; it makes a PlainTime a Timed.

func (PlainTime) Compare

func (t PlainTime) Compare(u PlainTime) int

Compare orders times.

func (PlainTime) Key

func (t PlainTime) Key() string

Key is the canonical form == compares.

func (PlainTime) Microsecond

func (t PlainTime) Microsecond() int

func (PlainTime) Millisecond

func (t PlainTime) Millisecond() int

Millisecond, Microsecond and Nanosecond are the three parts of Nano.

func (PlainTime) Nanosecond

func (t PlainTime) Nanosecond() int

func (PlainTime) Round

func (t PlainTime) Round(unit Unit, inc int64, mode RoundingMode) (PlainTime, error)

Round rounds the time of day; a result of 24:00 wraps to 00:00.

func (PlainTime) String

func (t PlainTime) String() string

String is 14:03:05.123.

func (PlainTime) TimeType

func (PlainTime) TimeType() string

TimeType is the name of the type's tag.

func (PlainTime) Until

func (t PlainTime) Until(o PlainTime, largest Unit) (Duration, error)

Until is the duration from t to o around the clock, balanced up to largest, Hour or smaller.

func (PlainTime) With

func (t PlainTime) With(f Fields) (PlainTime, error)

With replaces the given time fields, each clamped to its range; a date field is an error.

type RelativeTo

type RelativeTo interface {
	// contains filtered or unexported methods
}

RelativeTo is the point a calendar unit is measured from: a ZonedDateTime, whose days are real days, or a PlainDate, whose days are 24 hours; nil is none.

type RoundingMode

type RoundingMode int

RoundingMode is one of Temporal's nine.

const (
	Ceil RoundingMode = iota
	Floor
	Expand
	Trunc
	HalfCeil
	HalfFloor
	HalfExpand
	HalfTrunc
	HalfEven
)

type Timed

type Timed interface{ Clock() PlainTime }

Timed is a value with a time: a ZonedDateTime, a PlainTime or a PlainDateTime.

type Unit

type Unit int

Unit is a unit of time, largest first.

const (
	Year Unit = iota
	Month
	Week
	Day
	Hour
	Minute
	Second
	Millisecond
	Microsecond
	Nanosecond
)

func (Unit) String

func (u Unit) String() string

type Zone

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

Zone is a zone of the IANA database, or a fixed offset.

func FixedZone

func FixedZone(offset int) *Zone

FixedZone is a zone at a fixed offset, in seconds east of UTC.

func LoadZone

func LoadZone(id string) (*Zone, error)

LoadZone resolves a name of the IANA database in any letter case, an alias such as Asia/Calcutta included, or an offset ±HH, ±HH:MM or ±HHMM. Anything else is an error, the empty string and Go's "Local" included. The rules are loaded under the name itself.

func LocalZone

func LocalZone() *Zone

LocalZone is the zone the host's configuration names: the TZ environment variable first, with a leading ':' dropped as POSIX allows, the system's setting second, and when neither names a zone of the database, the fixed offset the host reports at the moment of the call. Only names count: POSIX reads an offset in TZ, such as +05:30, west of UTC, the opposite of an offset zone.

func (*Zone) Fixed

func (z *Zone) Fixed() bool

Fixed reports an offset zone.

func (*Zone) ID

func (z *Zone) ID() string

ID is the zone's identifier as given to LoadZone, in the database's spelling.

func (*Zone) Key

func (z *Zone) Key() string

Key is the canonical form == compares: the first name of the zone's CLDR alias group, so Asia/Calcutta equals Asia/Kolkata and Europe/Stockholm stays apart from Europe/Berlin; an offset zone never equals a named one.

func (*Zone) String

func (z *Zone) String() string

String is the identifier.

func (*Zone) TimeType

func (*Zone) TimeType() string

TimeType is the name of the type's tag.

type ZonedDateTime

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

ZonedDateTime is an instant seen in a zone.

func ParseZoned

func ParseZoned(s string, d Disambiguation) (ZonedDateTime, error)

ParseZoned reads a date, a time, a bracketed zone, and an offset or Z. Without a time it is the start of the day; without an offset the wall-clock time is resolved by d; Z gives the exact moment, shown in the zone; an offset that is not the zone's at that time, rounded to the minute as the string renders it, is an error.

func ToZoned

func ToZoned(dt PlainDateTime, z *Zone, d Disambiguation) (ZonedDateTime, error)

ToZoned places a wall-clock time in a zone, resolving a gap or an overlap by d. In a gap the time is moved by the gap's length, back for Earlier and forward otherwise, as Temporal's DisambiguatePossibleEpochNanoseconds has it.

func (ZonedDateTime) Add

func (z ZonedDateTime) Add(dur Duration) (ZonedDateTime, error)

Add adds the date part to the wall clock, places the result in the zone as Compatible, and adds the time part to that instant; a duration with no date part moves the instant alone.

func (ZonedDateTime) Clock

func (z ZonedDateTime) Clock() PlainTime

Clock is the wall-clock time.

func (ZonedDateTime) Compare

func (z ZonedDateTime) Compare(o ZonedDateTime) int

Compare orders by instant alone.

func (ZonedDateTime) Date

func (z ZonedDateTime) Date() PlainDate

Date is the wall-clock date.

func (ZonedDateTime) GoTime added in v0.3.3

func (z ZonedDateTime) GoTime() time.Time

GoTime is the moment as a Go time, in the zone's location.

func (ZonedDateTime) HoursInDay

func (z ZonedDateTime) HoursInDay() (float64, error)

HoursInDay is the length of the value's date in its zone: 24, 23 or 25 around a daylight-saving change, or 23.5 where the change is half an hour.

func (ZonedDateTime) Instant

func (z ZonedDateTime) Instant() Instant

Instant is the exact moment.

func (ZonedDateTime) Key

func (z ZonedDateTime) Key() string

Key is the canonical form == compares: the instant and the zone.

func (ZonedDateTime) NextTransition

func (z ZonedDateTime) NextTransition() (ZonedDateTime, bool)

NextTransition is the first instant after z at which the zone's offset changes, if any; a change of abbreviation alone is skipped.

func (ZonedDateTime) Offset

func (z ZonedDateTime) Offset() string

Offset is the offset as ±HH:MM.

func (ZonedDateTime) OffsetSeconds

func (z ZonedDateTime) OffsetSeconds() int

OffsetSeconds is the zone's offset from UTC at the instant.

func (ZonedDateTime) PlainDateTime

func (z ZonedDateTime) PlainDateTime() PlainDateTime

PlainDateTime is the wall-clock date and time.

func (ZonedDateTime) PreviousTransition

func (z ZonedDateTime) PreviousTransition() (ZonedDateTime, bool)

PreviousTransition is the last instant before z at which the offset changed, if any.

func (ZonedDateTime) Round

func (z ZonedDateTime) Round(unit Unit, inc int64, mode RoundingMode) (ZonedDateTime, error)

Round rounds to a day by the fraction of the real day that has passed, choosing the start of this date or the next; to a smaller unit on the wall clock, placed in the zone with the offset kept when the zone has it.

func (ZonedDateTime) StartOfDay

func (z ZonedDateTime) StartOfDay() (ZonedDateTime, error)

StartOfDay is the first instant of the value's date in its zone, which is not midnight on a day whose midnight falls in a gap.

func (ZonedDateTime) String

func (z ZonedDateTime) String() string

String is 2024-11-03T01:00:00-04:00[America/New_York]: the offset rounded to the minute, since RFC 9557 has no seconds in it, while Offset keeps them.

func (ZonedDateTime) TimeType

func (ZonedDateTime) TimeType() string

TimeType is the name of the type's tag.

func (ZonedDateTime) Until

func (z ZonedDateTime) Until(o ZonedDateTime, largest Unit) (Duration, error)

Until is the duration from z to o: an exact difference for Hour and smaller, and for Day and larger the hybrid difference of Temporal, whose days are the zone's real days, which needs both in one zone.

func (ZonedDateTime) With

With replaces the given wall-clock fields, constrained, and places the result in the zone with the value's own offset when the zone has it at the new time, and by d otherwise.

func (ZonedDateTime) WithZone

func (z ZonedDateTime) WithZone(nz *Zone) ZonedDateTime

WithZone is the same instant in another zone.

func (ZonedDateTime) Zone

func (z ZonedDateTime) Zone() *Zone

Zone is the zone.

Directories

Path Synopsis
Command gen writes zones_gen.go, the zone table of package temporal, from three published files: tzdata.zi, the compact form of the IANA time zone database that systems ship under zoneinfo (its Z lines are the zones and its L lines the links to them), which gives every name; CLDR's bcp47/timezone.xml, whose alias groups say which names are one zone, as Temporal's implementations canonicalize, so that Asia/Calcutta equals Asia/Kolkata while Europe/Stockholm stays apart from Europe/Berlin although the IANA database has merged their rules since 1970; and CLDR's windowsZones.xml, which maps Windows zone names to IANA names.
Command gen writes zones_gen.go, the zone table of package temporal, from three published files: tzdata.zi, the compact form of the IANA time zone database that systems ship under zoneinfo (its Z lines are the zones and its L lines the links to them), which gives every name; CLDR's bcp47/timezone.xml, whose alias groups say which names are one zone, as Temporal's implementations canonicalize, so that Asia/Calcutta equals Asia/Kolkata while Europe/Stockholm stays apart from Europe/Berlin although the IANA database has merged their rules since 1970; and CLDR's windowsZones.xml, which maps Windows zone names to IANA names.

Jump to

Keyboard shortcuts

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