tzatziki

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 12 Imported by: ? 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.

README

tzatziki

tzatziki converts between Go's time.Location and the VTIMEZONE components of iCalendar. You can write a recurring event in its real zone, so that "every Monday at 09:00" stays at 09:00 when the clocks change, and read the zones other programs send you, including Outlook's. Instead of guessing a recurrence rule from Go's zone data, it lists every real clock change, so what it writes reads back to the same offsets. It was extracted from pim-mcp, which reads invites from strangers.

  • Exact round trip. A zone written and read back is tested hour by hour against Go's own offsets, for eight zones over eleven years from three start years, including Lord Howe's half-hour change and Casablanca.
  • One observance per clock change. No rule is inferred, so a reader that has only the definition gets the right offset at every moment the range covers.
  • Real zones from definitions. A VTIMEZONE becomes a time.Location with all its transitions, so date arithmetic in it works like in a named zone.
  • Outlook's zones. Rules that start in 1601, as Outlook writes them, are read correctly, and its Windows names such as "W. Europe Standard Time" are mapped through CLDR's table of 142 names.
  • Names first. A TZID is resolved as a Windows name, then an IANA name (with or without a libical prefix), then the calendar's own definition. A name wins because the sender's definition may be out of date. An unknown zone is an error, never a guess.
  • Safe on hostile input. Rule expansion has a step limit, a rule that repeats every second fails fast, and the reader is fuzzed.
  • Two dependencies. go-ical and rrule-go.
amsterdam, _ := time.LoadLocation("Europe/Amsterdam")
from := time.Date(2026, time.January, 1, 0, 0, 0, 0, amsterdam)
vtimezone, _ := tzatziki.FromLocation(amsterdam, from, from.AddDate(1, 0, 0))

loc, _ := tzatziki.ToLocation(vtimezone)
monday := time.Date(2026, time.October, 19, 9, 0, 0, 0, loc)
fmt.Println(monday.UTC().Format(time.Kitchen), monday.AddDate(0, 0, 7).UTC().Format(time.Kitchen))
// 7:00AM 8:00AM

Getting started

You need Go 1.26 or later.

go get github.com/robinvdvleuten/tzatziki

Write a zone. Give FromLocation a range that covers every time you write in the zone. After the range, a reader that has only the definition keeps the last offset. Add the result to the calendar beside the events that use its TZID.

vtimezone, err := tzatziki.FromLocation(loc, start, start.AddDate(10, 0, 0))
cal.Children = append([]*ical.Component{vtimezone}, cal.Children...)

Read the zones of a calendar. Make one Resolver per calendar, and ask it for the TZID of each property:

resolver := tzatziki.NewResolver(cal)
loc, err := resolver.Location(prop.Params.Get(ical.ParamTimezoneID))

Read one definition with tzatziki.ToLocation(component), or one name with tzatziki.LoadLocation(tzid). tzatziki.WindowsToIANA(name) looks up a Windows name alone.

A zone read from a definition has an empty name, because a definition carries no IANA name.

IANA names are loaded with time.LoadLocation. If your program may run without zone files, such as in a small container, import time/tzdata to carry them in the binary.


Limits

  • Rules in definitions are followed from 1900 to 2100. Before and after, the nearest offset holds.
  • A rule may take at most 5000 steps per observance, and a definition at most 256 different offsets.
  • A written definition may hold a few thousand clock changes. A longer range is an error.

Development

Install mise, which fetches the pinned Go, linter and Vale, then set up the commit hook:

mise install && mise run setup

Check a change: formatting, tests, lint and known vulnerabilities.

mise run check

The Windows zone table is generated from CLDR with go generate and committed.

License

MIT © Robin van der Vleuten. See LICENSE for more information.

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

View Source
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.

View Source
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

func FromLocation(loc *time.Location, from, until time.Time) (*ical.Component, error)

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

func LoadLocation(tzid string) (*time.Location, error)

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

func ToLocation(component *ical.Component) (*time.Location, error)

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

func WindowsToIANA(name string) (string, bool)

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

func NewResolver(cal *ical.Calendar) *Resolver

NewResolver returns a Resolver for the VTIMEZONE components in cal, which may be nil.

func (*Resolver) Location

func (r *Resolver) Location(tzid string) (*time.Location, error)

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.

Jump to

Keyboard shortcuts

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