Documentation
¶
Overview ¶
Package tzinfo is a pure-Go (CGO-free), MRI-faithful reimplementation of the Ruby TZInfo library (the `tzinfo` gem). It embeds a complete offline copy of the compiled IANA time zone database (the TZif files the gem's ZoneinfoDataSource reads from the system zoneinfo) and exposes the TZInfo API on top of it: resolving a zone by identifier, converting between UTC and local time, computing the TimezonePeriod (offset, abbreviation, DST flag, transition boundaries) in force at an instant, and handling the spring-forward gap (PeriodNotFound) and fall-back overlap (AmbiguousTime) exactly as the gem does.
Relationship to the gem ¶
Ruby (tzinfo gem) Go (this package)
----------------- -----------------
TZInfo::Timezone.get("America/…") tzinfo.Get("America/…")
TZInfo::Timezone.all_identifiers tzinfo.AllIdentifiers()
tz.utc_to_local(t) tz.UTCToLocal(t)
tz.local_to_utc(t) tz.LocalToUTC(t)
tz.period_for_utc(t) tz.PeriodForUTC(t)
tz.period_for_local(t) tz.PeriodForLocal(t)
tz.transitions_up_to(to, from) tz.TransitionsUpTo(to, from)
tz.offsets_up_to(to, from) tz.OffsetsUpTo(to, from)
tz.current_period / .now tz.CurrentPeriod() / tz.Now()
tz.canonical_identifier tz.CanonicalIdentifier()
TimezonePeriod / TimezoneOffset TimezonePeriod / TimezoneOffset
TZInfo::Country.get("US") tzinfo.GetCountry("US")
Every timestamp crossing the API is a Go time.Time; UTC-to-local conversions return a time.Time carrying a *time.Location fixed to the period's offset and abbreviation, mirroring how TZInfo tags a Time with the resolved offset.
Index ¶
- func AllCountryCodes() []string
- func AllDataZoneIdentifiers() ([]string, error)
- func AllIdentifiers() ([]string, error)
- type AmbiguousTime
- type Country
- type InvalidCountryCode
- type InvalidTimezoneIdentifier
- type PeriodNotFound
- type Timezone
- func (tz *Timezone) Abbreviation(t time.Time) string
- func (tz *Timezone) CanonicalIdentifier() string
- func (tz *Timezone) CurrentPeriod() TimezonePeriod
- func (tz *Timezone) DST(t time.Time) bool
- func (tz *Timezone) Identifier() string
- func (tz *Timezone) LocalToUTC(t time.Time, dst ...bool) (time.Time, error)
- func (tz *Timezone) Now() time.Time
- func (tz *Timezone) OffsetsUpTo(to time.Time, from ...time.Time) []TimezoneOffset
- func (tz *Timezone) PeriodForLocal(t time.Time, dst ...bool) (TimezonePeriod, error)
- func (tz *Timezone) PeriodForUTC(t time.Time) TimezonePeriod
- func (tz *Timezone) String() string
- func (tz *Timezone) TransitionsUpTo(to time.Time, from ...time.Time) []TimezoneTransition
- func (tz *Timezone) UTCOffset() int
- func (tz *Timezone) UTCToLocal(t time.Time) time.Time
- type TimezoneOffset
- type TimezonePeriod
- func (p TimezonePeriod) Abbreviation() string
- func (p TimezonePeriod) BaseUTCOffset() int
- func (p TimezonePeriod) DST() bool
- func (p TimezonePeriod) EndTransition() (t time.Time, ok bool)
- func (p TimezonePeriod) STDOffset() int
- func (p TimezonePeriod) StartTransition() (t time.Time, ok bool)
- func (p TimezonePeriod) UTCTotalOffset() int
- type TimezoneTransition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllCountryCodes ¶
func AllCountryCodes() []string
AllCountryCodes returns every known ISO-3166 code, sorted (TZInfo::Country.all_codes).
func AllDataZoneIdentifiers ¶
AllDataZoneIdentifiers returns only the canonical (data) zone identifiers, sorted (TZInfo::Timezone.all_data_zone_identifiers).
func AllIdentifiers ¶
AllIdentifiers returns every timezone identifier, sorted (TZInfo::Timezone.all_identifiers). Because every backward-link name is a full data zone, this equals AllDataZoneIdentifiers.
Types ¶
type AmbiguousTime ¶
AmbiguousTime is returned by PeriodForLocal when the local time occurs twice because of a fall-back overlap and no disambiguation was supplied (mirrors TZInfo::AmbiguousTime).
func (*AmbiguousTime) Error ¶
func (e *AmbiguousTime) Error() string
type Country ¶
type Country struct {
// contains filtered or unexported fields
}
Country is an ISO-3166 country and the timezone identifiers observed within it (TZInfo::Country). The data derives from the IANA zone1970.tab / iso3166.tab.
func GetCountry ¶
GetCountry resolves a country by its ISO-3166 alpha-2 code (TZInfo::Country.get). The lookup is case-insensitive on input but the gem's codes are upper-case. An unknown code returns *InvalidCountryCode.
func (*Country) ZoneIdentifiers ¶
ZoneIdentifiers returns the timezone identifiers for the country in the IANA data's order (zone_identifiers).
type InvalidCountryCode ¶
type InvalidCountryCode struct {
Code string
}
InvalidCountryCode is returned by GetCountry for an unknown ISO-3166 code (mirrors TZInfo::InvalidCountryCode).
func (*InvalidCountryCode) Error ¶
func (e *InvalidCountryCode) Error() string
type InvalidTimezoneIdentifier ¶
type InvalidTimezoneIdentifier struct {
Identifier string
}
InvalidTimezoneIdentifier is returned by Get when the identifier names no zone in the database (mirrors TZInfo::InvalidTimezoneIdentifier).
func (*InvalidTimezoneIdentifier) Error ¶
func (e *InvalidTimezoneIdentifier) Error() string
type PeriodNotFound ¶
PeriodNotFound is returned by PeriodForLocal when the local time falls in a spring-forward gap that does not occur in the zone (mirrors TZInfo::PeriodNotFound).
func (*PeriodNotFound) Error ¶
func (e *PeriodNotFound) Error() string
type Timezone ¶
type Timezone struct {
// contains filtered or unexported fields
}
Timezone is a resolved IANA time zone. It mirrors TZInfo::Timezone (specifically a DataTimezone); linked identifiers resolve to their canonical zone's data while remembering the identifier they were fetched with.
func Get ¶
Get resolves a zone by IANA identifier (TZInfo::Timezone.get). The embedded database (matching the gem's ZoneinfoDataSource) materialises every IANA backward-link name — "US/Eastern", "GB", "Zulu", … — as a full data zone, so every valid identifier is its own canonical zone; there are no link indirections at this layer. An unknown identifier returns *InvalidTimezoneIdentifier.
func (*Timezone) Abbreviation ¶
Abbreviation returns the abbreviation in force at UTC instant t (abbreviation).
func (*Timezone) CanonicalIdentifier ¶
CanonicalIdentifier returns the identifier of the underlying data zone (canonical_identifier in the gem). For an unlinked zone it equals Identifier.
func (*Timezone) CurrentPeriod ¶
func (tz *Timezone) CurrentPeriod() TimezonePeriod
CurrentPeriod returns the period in force at the current instant (current_period).
func (*Timezone) DST ¶
DST reports whether daylight-saving time is in force at UTC instant t (dst?).
func (*Timezone) Identifier ¶
Identifier returns the identifier this Timezone was fetched with (identifier in the gem).
func (*Timezone) LocalToUTC ¶
LocalToUTC converts a local wall-clock time to the corresponding UTC instant (local_to_utc). The wall-clock fields of t are read as local time in this zone; t's own Location is ignored for the wall value. When t falls in a spring-forward gap it returns *PeriodNotFound; when it is ambiguous (fall-back overlap) it returns *AmbiguousTime unless dst disambiguates (see PeriodForLocal).
func (*Timezone) Now ¶
Now returns the current local time in this zone (now), a time.Time in a FixedZone for the current period.
func (*Timezone) OffsetsUpTo ¶
OffsetsUpTo returns the distinct offsets observed in the half-open UTC window [from, to) (offsets_up_to). The offset in force at the start of the window is included.
func (*Timezone) PeriodForLocal ¶
PeriodForLocal returns the TimezonePeriod for a local wall-clock time (period_for_local). The wall-clock fields of t are read as local time in this zone. A gap yields *PeriodNotFound; an overlap yields *AmbiguousTime unless a single dst preference is supplied to select the daylight (true) or standard (false) period.
func (*Timezone) PeriodForUTC ¶
func (tz *Timezone) PeriodForUTC(t time.Time) TimezonePeriod
PeriodForUTC returns the TimezonePeriod in force at the given UTC instant (period_for_utc). t is interpreted as an absolute instant regardless of its Location.
func (*Timezone) String ¶
String returns the fetched identifier (to_s in the gem returns the identifier).
func (*Timezone) TransitionsUpTo ¶
TransitionsUpTo returns the transitions occurring in the half-open UTC window [from, to), in chronological order (transitions_up_to). When from is the zero time, the window is open at the start.
type TimezoneOffset ¶
type TimezoneOffset struct {
// BaseUTCOffset is the standard-time offset east of UTC in seconds
// (base_utc_offset / utc_offset in the gem).
BaseUTCOffset int
// STDOffset is the additional offset applied during daylight-saving time in
// seconds (std_offset in the gem); zero outside DST.
STDOffset int
// Abbreviation is the local abbreviation, e.g. "EST", "EDT", "LMT", "IST".
Abbreviation string
}
TimezoneOffset describes the offset from UTC in force during a period: the base (standard) offset, the additional DST amount, the combined total, and the abbreviation. It mirrors TZInfo::TimezoneOffset.
func (TimezoneOffset) DST ¶
func (o TimezoneOffset) DST() bool
DST reports whether daylight-saving time is in effect for this offset (dst? in the gem).
func (TimezoneOffset) UTCTotalOffset ¶
func (o TimezoneOffset) UTCTotalOffset() int
UTCTotalOffset is the combined offset east of UTC in seconds (utc_total_offset in the gem).
type TimezonePeriod ¶
type TimezonePeriod struct {
// Offset is the offset that applies throughout the period.
Offset TimezoneOffset
// contains filtered or unexported fields
}
TimezonePeriod is the interval during which a single TimezoneOffset is in force, bounded (where known) by the transitions that begin and end it. It mirrors TZInfo::TimezonePeriod.
func (TimezonePeriod) Abbreviation ¶
func (p TimezonePeriod) Abbreviation() string
Abbreviation returns the period's abbreviation (abbreviation / abbr / zone_identifier in the gem).
func (TimezonePeriod) BaseUTCOffset ¶
func (p TimezonePeriod) BaseUTCOffset() int
BaseUTCOffset returns the standard-time offset in seconds.
func (TimezonePeriod) DST ¶
func (p TimezonePeriod) DST() bool
DST reports whether the period is daylight-saving.
func (TimezonePeriod) EndTransition ¶
func (p TimezonePeriod) EndTransition() (t time.Time, ok bool)
EndTransition returns the UTC instant the period ends and whether it is bounded in the future. When ok is false the period is the current, open-ended period.
func (TimezonePeriod) STDOffset ¶
func (p TimezonePeriod) STDOffset() int
STDOffset returns the DST component of the offset in seconds.
func (TimezonePeriod) StartTransition ¶
func (p TimezonePeriod) StartTransition() (t time.Time, ok bool)
StartTransition returns the UTC instant the period begins and whether it is bounded in the past. When ok is false the period extends to the beginning of time.
func (TimezonePeriod) UTCTotalOffset ¶
func (p TimezonePeriod) UTCTotalOffset() int
UTCTotalOffset returns the combined UTC offset in seconds.
type TimezoneTransition ¶
type TimezoneTransition struct {
// At is the UTC instant of the transition.
At time.Time
// Offset is the offset that comes into force at and after At.
Offset TimezoneOffset
}
TimezoneTransition is a single transition instant and the offset that takes effect at it, as returned by TransitionsUpTo (mirrors TZInfo::TimezoneTransition).
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
tzdata
Package tzdata embeds a complete, offline copy of the compiled IANA time zone database (the TZif files the tzinfo gem's ZoneinfoDataSource reads) and parses the TZif (zoneinfo) v2/v3 format into transition tables.
|
Package tzdata embeds a complete, offline copy of the compiled IANA time zone database (the TZif files the tzinfo gem's ZoneinfoDataSource reads) and parses the TZif (zoneinfo) v2/v3 format into transition tables. |
