Documentation
¶
Overview ¶
Package tzatziki converts between Go's time.Location and the VTIMEZONE components of iCalendar (RFC 5545).
FromLocation writes a VTIMEZONE with one observance for each real clock change in a range, so no recurrence rule has to be inferred. ToLocation reads a VTIMEZONE, from any sender, back into a real time.Location, so times in it keep their wall clock across clock changes just like in a named zone. Resolver resolves the TZID of a property the way invites need: a Windows zone name, then an IANA name, then the definition the calendar carries.
IANA names are loaded with time.LoadLocation. A program that runs where there may be no zone files can import time/tzdata to carry its own.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRuleTooExpensive = errors.New("time zone rule repeats too often to expand")
ErrRuleTooExpensive means an observance's RRULE repeats too often to expand, such as every second.
var ErrUnknownZone = errors.New("unknown time zone")
ErrUnknownZone means a TZID is no zone name this package knows, and no definition was found for it.
Functions ¶
func FromLocation ¶
FromLocation returns the VTIMEZONE for loc that RFC 5545 asks for beside any TZID. Its TZID is loc's name. It lists the zone's real clock changes from from up to until, one observance each, so no recurrence rule has to be inferred. The first observance only fixes the offset in force at from, which is cut to the second.
A reader that has only this definition keeps the last offset after until. Choose a range that covers every time written in the zone.
Example ¶
package main
import (
"fmt"
"log"
"time"
"github.com/emersion/go-ical"
"github.com/robinvdvleuten/tzatziki"
)
func main() {
amsterdam, err := time.LoadLocation("Europe/Amsterdam")
if err != nil {
log.Fatal(err)
}
from := time.Date(2026, time.January, 1, 0, 0, 0, 0, amsterdam)
vtimezone, err := tzatziki.FromLocation(amsterdam, from, from.AddDate(1, 0, 0))
if err != nil {
log.Fatal(err)
}
fmt.Println(vtimezone.Props.Get(ical.PropTimezoneID).Value)
for _, observance := range vtimezone.Children {
fmt.Println(observance.Name,
observance.Props.Get(ical.PropDateTimeStart).Value,
observance.Props.Get(ical.PropTimezoneOffsetTo).Value)
}
}
Output: Europe/Amsterdam STANDARD 20260101T000000 +0100 DAYLIGHT 20260329T020000 +0200 STANDARD 20261025T030000 +0100
func LoadLocation ¶
LoadLocation returns the zone a TZID names, without looking at any definition. It takes a Windows zone name, an IANA name, or an IANA name with a libical prefix, as in "/freeassociation.sourceforge.net/Europe/Paris". "Local" and the empty name are refused, because they would mean the zone of whichever machine runs this, or UTC by accident.
Example ¶
package main
import (
"fmt"
"log"
"github.com/robinvdvleuten/tzatziki"
)
func main() {
loc, err := tzatziki.LoadLocation("Tokyo Standard Time")
if err != nil {
log.Fatal(err)
}
fmt.Println(loc)
}
Output: Asia/Tokyo
func ToLocation ¶
ToLocation turns a VTIMEZONE into a real Location, so that times in it keep their wall clock across clock changes just like in a named zone. Its name is empty, because a definition has no IANA name.
Observances may repeat by RRULE and RDATE. A rule is followed from 1900 to 2100, so one that starts in 1601, as Outlook writes them, works. A rule that would take more than a few thousand steps fails with ErrRuleTooExpensive.
Example ¶
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/emersion/go-ical"
"github.com/robinvdvleuten/tzatziki"
)
func main() {
cal, err := ical.NewDecoder(strings.NewReader(`BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example//EN
BEGIN:VTIMEZONE
TZID:Central Europe
BEGIN:STANDARD
DTSTART:16010101T030000
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T020000
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
END:DAYLIGHT
END:VTIMEZONE
END:VCALENDAR
`)).Decode()
if err != nil {
log.Fatal(err)
}
loc, err := tzatziki.ToLocation(cal.Children[0])
if err != nil {
log.Fatal(err)
}
// Every Monday at 09:00 stays at 09:00 when the clocks go back.
monday := time.Date(2026, time.October, 19, 9, 0, 0, 0, loc)
fmt.Println(monday.UTC().Format(time.RFC3339))
fmt.Println(monday.AddDate(0, 0, 7).UTC().Format(time.RFC3339))
}
Output: 2026-10-19T07:00:00Z 2026-10-26T08:00:00Z
func WindowsToIANA ¶
WindowsToIANA returns the IANA name for a Windows time zone name, such as "W. Europe Standard Time", from CLDR's table. It reports false for any other name.
Example ¶
package main
import (
"fmt"
"github.com/robinvdvleuten/tzatziki"
)
func main() {
name, ok := tzatziki.WindowsToIANA("Eastern Standard Time")
fmt.Println(name, ok)
}
Output: America/New_York true
Types ¶
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver resolves the TZIDs used in one calendar. A Resolver is safe for concurrent use.
Example ¶
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/emersion/go-ical"
"github.com/robinvdvleuten/tzatziki"
)
func main() {
cal, err := ical.NewDecoder(strings.NewReader(`BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example//EN
BEGIN:VTIMEZONE
TZID:Custom/Nowhere
BEGIN:STANDARD
DTSTART:19700101T000000
TZOFFSETFROM:+0530
TZOFFSETTO:+0530
END:STANDARD
END:VTIMEZONE
END:VCALENDAR
`)).Decode()
if err != nil {
log.Fatal(err)
}
resolver := tzatziki.NewResolver(cal)
for _, tzid := range []string{"W. Europe Standard Time", "/freeassociation.sourceforge.net/America/New_York", "Custom/Nowhere", "Mars/Olympus"} {
loc, err := resolver.Location(tzid)
if err != nil {
fmt.Println(err)
continue
}
// A zone read from a definition has no name.
line := time.Date(2026, time.November, 10, 9, 0, 0, 0, loc).Format("15:04 -0700 ") + loc.String()
fmt.Println(strings.TrimSpace(line))
}
}
Output: 09:00 +0100 Europe/Berlin 09:00 -0500 America/New_York 09:00 +0530 tzatziki: unknown time zone "Mars/Olympus", and the calendar doesn't define it
func NewResolver ¶
NewResolver returns a Resolver for the VTIMEZONE components in cal, which may be nil.
func (*Resolver) Location ¶
Location resolves a TZID: a name as LoadLocation takes it first, then the calendar's own definition, read by ToLocation. A name wins because it stays correct when the sender's definition is out of date. A TZID that neither knows fails with ErrUnknownZone; it is never guessed.
Each TZID is resolved once, and its result, or its error, is kept.