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 ¶
- Variables
- func CompareDurations(a, b Duration, rel RelativeTo) (int, error)
- func TzdataVersion() string
- type Dated
- type Disambiguation
- type Duration
- func AddDurations(a, b Duration) (Duration, error)
- func DurationLiteral(s string) (Duration, error)
- func FromGoDuration(d time.Duration) Duration
- func NewDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, ... int64) (Duration, error)
- func ParseDuration(s string) (Duration, error)
- func (d Duration) Abs() (Duration, error)
- func (d Duration) Exact() (time.Duration, bool)
- func (d Duration) Key() string
- func (d Duration) Negate() (Duration, error)
- func (d Duration) Part(u Unit) int64
- func (d Duration) Round(smallest Unit, hasSmallest bool, largest Unit, hasLargest bool, inc int64, ...) (Duration, error)
- func (d Duration) Sign() int
- func (d Duration) String() string
- func (Duration) TimeType() string
- func (d Duration) Total(unit Unit, rel RelativeTo) (float64, error)
- type Fields
- type Instant
- func FromEpochMillis(ms int64) (Instant, error)
- func FromEpochNanos(ns int64) Instant
- func FromGoTime(t time.Time) Instant
- func NewInstant(sec, nsec int64) (Instant, error)
- func Now() Instant
- func ParseInstant(s string) (Instant, error)
- func PossibleInstants(dt PlainDateTime, z *Zone) ([]Instant, error)
- func (i Instant) Add(dur Duration) (Instant, error)
- func (i Instant) Compare(j Instant) int
- func (i Instant) EpochMillis() int64
- func (i Instant) EpochNanos() (int64, error)
- func (i Instant) GoTime() time.Time
- func (i Instant) InZone(z *Zone) ZonedDateTime
- func (i Instant) Key() string
- func (i Instant) Round(unit Unit, inc int64, mode RoundingMode) (Instant, error)
- func (i Instant) String() string
- func (Instant) TimeType() string
- func (i Instant) Until(o Instant, largest Unit) (Duration, error)
- type PlainDate
- func (d PlainDate) Add(dur Duration) (PlainDate, error)
- func (d PlainDate) Compare(e PlainDate) int
- func (d PlainDate) Date() PlainDate
- func (d PlainDate) DayOfWeek() int
- func (d PlainDate) DayOfYear() int
- func (d PlainDate) DaysInMonth() int
- func (d PlainDate) DaysInYear() int
- func (d PlainDate) InLeapYear() bool
- func (d PlainDate) Key() string
- func (d PlainDate) String() string
- func (PlainDate) TimeType() string
- func (d PlainDate) Until(o PlainDate, largest Unit) (Duration, error)
- func (d PlainDate) WeekOfYear() int
- func (d PlainDate) With(f Fields) (PlainDate, error)
- func (d PlainDate) YearOfWeek() int
- type PlainDateTime
- func (dt PlainDateTime) Add(dur Duration) (PlainDateTime, error)
- func (dt PlainDateTime) Clock() PlainTime
- func (dt PlainDateTime) Compare(e PlainDateTime) int
- func (dt PlainDateTime) Date() PlainDate
- func (dt PlainDateTime) Key() string
- func (dt PlainDateTime) Round(unit Unit, inc int64, mode RoundingMode) (PlainDateTime, error)
- func (dt PlainDateTime) String() string
- func (PlainDateTime) TimeType() string
- func (dt PlainDateTime) Until(o PlainDateTime, largest Unit) (Duration, error)
- func (dt PlainDateTime) With(f Fields) (PlainDateTime, error)
- type PlainTime
- func (t PlainTime) Add(dur Duration) (PlainTime, error)
- func (t PlainTime) Clock() PlainTime
- func (t PlainTime) Compare(u PlainTime) int
- func (t PlainTime) Key() string
- func (t PlainTime) Microsecond() int
- func (t PlainTime) Millisecond() int
- func (t PlainTime) Nanosecond() int
- func (t PlainTime) Round(unit Unit, inc int64, mode RoundingMode) (PlainTime, error)
- func (t PlainTime) String() string
- func (PlainTime) TimeType() string
- func (t PlainTime) Until(o PlainTime, largest Unit) (Duration, error)
- func (t PlainTime) With(f Fields) (PlainTime, error)
- type RelativeTo
- type RoundingMode
- type Timed
- type Unit
- type Zone
- type ZonedDateTime
- func (z ZonedDateTime) Add(dur Duration) (ZonedDateTime, error)
- func (z ZonedDateTime) Clock() PlainTime
- func (z ZonedDateTime) Compare(o ZonedDateTime) int
- func (z ZonedDateTime) Date() PlainDate
- func (z ZonedDateTime) GoTime() time.Time
- func (z ZonedDateTime) HoursInDay() (float64, error)
- func (z ZonedDateTime) Instant() Instant
- func (z ZonedDateTime) Key() string
- func (z ZonedDateTime) NextTransition() (ZonedDateTime, bool)
- func (z ZonedDateTime) Offset() string
- func (z ZonedDateTime) OffsetSeconds() int
- func (z ZonedDateTime) PlainDateTime() PlainDateTime
- func (z ZonedDateTime) PreviousTransition() (ZonedDateTime, bool)
- func (z ZonedDateTime) Round(unit Unit, inc int64, mode RoundingMode) (ZonedDateTime, error)
- func (z ZonedDateTime) StartOfDay() (ZonedDateTime, error)
- func (z ZonedDateTime) String() string
- func (ZonedDateTime) TimeType() string
- func (z ZonedDateTime) Until(o ZonedDateTime, largest Unit) (Duration, error)
- func (z ZonedDateTime) With(f Fields, d Disambiguation) (ZonedDateTime, error)
- func (z ZonedDateTime) WithZone(nz *Zone) ZonedDateTime
- func (z ZonedDateTime) Zone() *Zone
Constants ¶
This section is empty.
Variables ¶
var ErrRange = errors.New("outside the range of time values")
ErrRange is the error of a value outside Temporal's range.
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 ¶
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
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
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 ¶
ParseDuration reads ISO 8601 when the first character after an optional sign is P, and Go's time.ParseDuration notation otherwise.
func (Duration) Exact ¶
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) Negate ¶
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) 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) String ¶
String is the ISO 8601 form, the fields as given and the subsecond fields folded into the seconds: P1Y2M3DT4H5M6.5S, -PT1H, PT0S.
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 ¶
FromEpochMillis makes an instant from milliseconds since the epoch.
func FromEpochNanos ¶
FromEpochNanos makes an instant from nanoseconds since the epoch; every int64 is in range.
func FromGoTime ¶ added in v0.3.0
FromGoTime is the moment of a time Go's clock read, such as a timer's, on the clock Now reads.
func NewInstant ¶
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 ParseInstant ¶
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 ¶
Add moves the instant by the time part; a date unit is an error, since an instant has no calendar.
func (Instant) EpochMillis ¶
EpochMillis is the milliseconds since the epoch, rounded down.
func (Instant) EpochNanos ¶
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) InZone ¶
func (i Instant) InZone(z *Zone) ZonedDateTime
InZone is the instant seen in a zone.
type PlainDate ¶
type PlainDate struct {
Year, Month, Day int
}
PlainDate is a calendar date with no zone.
func NewPlainDate ¶
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 ¶
ParsePlainDate reads a date, alone or at the start of a longer string.
func (PlainDate) Add ¶
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) Until ¶
Until is the duration from d to o, in units of Day or larger, counted from d.
func (PlainDate) WeekOfYear ¶
WeekOfYear is the ISO week, 1 to 53.
func (PlainDate) With ¶
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 ¶
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 ¶
ParsePlainTime reads a time, alone or after a date.
func (PlainTime) Microsecond ¶
func (PlainTime) Millisecond ¶
Millisecond, Microsecond and Nanosecond are the three parts of Nano.
func (PlainTime) Nanosecond ¶
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 Zone ¶
type Zone struct {
// contains filtered or unexported fields
}
Zone is a zone of the IANA database, or a fixed offset.
func LoadZone ¶
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.
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) Compare ¶
func (z ZonedDateTime) Compare(o ZonedDateTime) int
Compare orders by instant alone.
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 ¶
func (z ZonedDateTime) With(f Fields, d Disambiguation) (ZonedDateTime, error)
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.
Source Files
¶
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. |