Documentation
¶
Overview ¶
Package ics writes iCalendar (RFC 5545) files: a VCALENDAR holding VEVENT components, as calendar clients import or subscribe to them.
The package handles the encoding rules that are easy to get wrong: TEXT values are escaped, control characters are dropped, invalid UTF-8 is repaired, content lines are folded at 75 octets without splitting a multi-byte character, and every line ends in CRLF.
Times ¶
Timed events are written in UTC ("DTSTART:20261110T080000Z"). UTC needs no VTIMEZONE component, and every client shows the instant in the viewer's own zone, so nothing is lost for one-off events. Recurrence rules, which would need a local zone to stay correct across daylight-saving changes, are not supported: write every occurrence as an event of its own.
All-day events are written as dates ("DTSTART;VALUE=DATE:20261224"). The date is read in Calendar.TimeZone when set, or else in the location the time value carries. As in RFC 5545, the end of an all-day event is exclusive: a one-day event on 24 December ends on 25 December. By default, a zero End makes an all-day event last one day.
Encoding choices ¶
RFC 5545 leaves some choices to the writer. The defaults suit a new feed; a program that moves an existing feed to this package can set the fields below so the feed keeps its bytes, since subscribed clients see changed bytes as edited events and a changed UID as a new event:
- Calendar.EventPropertyOrder writes event properties in a given order, such as ORGANIZER and ATTENDEE before STATUS.
- Calendar.MissingEnd leaves DTEND out, derives it or writes the zero time for an event without an End.
- Calendar.VerbatimUIDs writes UIDs as given instead of removing whitespace from them.
- Param.Quote quotes a parameter value even when it needs no quotes.
- Param.LineBreak replaces line breaks in a parameter value instead of encoding them as RFC 6868 "^n".
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMissingProdID = errors.New("ics: calendar has no PRODID")
ErrMissingProdID is returned when a calendar has no product identifier.
var ErrMissingUID = errors.New("ics: event has no UID")
ErrMissingUID is returned when an event has no UID. RFC 5545 requires one, and clients use it to recognize an event they imported before.
Functions ¶
func EscapeText ¶
EscapeText encodes s as an RFC 5545 TEXT value: backslashes, semicolons and commas are escaped, line breaks become \n, other control characters are dropped, and invalid UTF-8 is replaced with U+FFFD.
func Fold ¶
Fold splits one content line into physical lines of at most 75 octets, as RFC 5545 section 3.1 requires: every line after the first starts with the space that marks it a continuation. No multi-byte character is split, and invalid UTF-8 is replaced with U+FFFD first, so every physical line is valid UTF-8. The lines carry no CRLF.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/mittwald/cli-kit/ics"
)
func main() {
for _, line := range ics.Fold("DESCRIPTION:" + strings.Repeat("abcdefghij", 8)) {
fmt.Printf("%2d %q\n", len(line), line[:min(len(line), 16)])
}
}
Output: 75 "DESCRIPTION:abcd" 18 " defghijabcdefgh"
func HashUID ¶
HashUID derives a stable UID from the fields that identify an event, for sources that have no identifier of their own: the same parts always give the same UID, so a client re-importing the calendar updates the events it knows instead of duplicating them. domain should name the program, such as "example.com". The parts are hashed, so they do not leak into the file.
func OccurrenceUID ¶
OccurrenceUID derives a distinct UID for one occurrence of a series whose occurrences share uid. RFC 5545 forbids two events with the same UID unless a RECURRENCE-ID tells them apart; appending the start keeps each occurrence its own event.
Types ¶
type Calendar ¶
type Calendar struct {
// ProdID identifies the program that wrote the calendar, as a formal
// public identifier such as "-//Example//Example CLI//EN". It is required.
ProdID string
// Name is the calendar's display name (X-WR-CALNAME). Optional.
Name string
// Method is the iTIP method; empty means "PUBLISH", the method for a
// calendar that is imported or subscribed to.
Method string
// Scale is the calendar scale; empty means "GREGORIAN".
Scale string
// TimeZone is the zone all-day dates are read in. When set, it is also
// written as X-WR-TIMEZONE, which some clients use as the calendar's zone.
TimeZone *time.Location
// Stamp is the DTSTAMP of every event that has none of its own. Zero
// means the time of writing; set it for reproducible output.
Stamp time.Time
// Props are further calendar properties, such as X- properties, written
// after the standard ones.
Props []Property
// Events are the calendar's events, written in order.
Events []Event
// EventPropertyOrder lists property names, such as "ORGANIZER", in the
// order they are written in every event; names are matched regardless of
// case. Properties it names come first, in its order. The others follow
// in the default order: UID, DTSTAMP, DTSTART, DTEND, SUMMARY, LOCATION,
// DESCRIPTION, CATEGORIES, STATUS, TRANSP, CLASS, LAST-MODIFIED and then
// [Event.Props] as given. Properties that share a name, such as several
// ATTENDEE, keep their order among themselves. RFC 5545 gives properties
// no order, so use it to match an existing feed or to put the properties
// a reader looks for first, such as ORGANIZER and ATTENDEE before STATUS.
EventPropertyOrder []string
// MissingEnd says how an event with a zero End is written. The zero
// value, [EndDefault], leaves DTEND out of a timed event and writes the
// day after the start for an all-day event.
MissingEnd EndMode
// VerbatimUIDs writes every [Event.UID] as given: only the control
// characters RFC 5545 allows in no content line, which are all but tab,
// are removed, and invalid UTF-8 is replaced with U+FFFD. By default,
// whitespace is removed too. Set it when the UIDs come from a scheme of
// the caller's own that has to stay stable even where it lets whitespace
// through, such as UIDs a feed has published before: clients key events
// by UID, so a changed UID duplicates the event in every subscribed
// client. A UID that is empty or only whitespace is still
// [ErrMissingUID].
VerbatimUIDs bool
}
Calendar is one VCALENDAR object.
func (Calendar) WriteTo ¶
WriteTo writes the calendar to w as an iCalendar file. It implements io.WriterTo. Nothing is written when the calendar or one of its events is incomplete.
Example ¶
package main
import (
"os"
"strings"
"time"
"github.com/mittwald/cli-kit/ics"
)
func main() {
stamp := time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC)
cal := ics.Calendar{
ProdID: "-//Example//Example CLI//EN",
Name: "Team",
Stamp: stamp,
Events: []ics.Event{
{
UID: ics.HashUID("example.com", "holiday", "2026-12-24"),
Start: time.Date(2026, 12, 24, 0, 0, 0, 0, time.UTC),
AllDay: true,
Summary: "Christmas Eve",
Transparent: true,
},
{
UID: "standup-20261110@example.com",
Start: time.Date(2026, 11, 10, 9, 0, 0, 0, time.UTC),
End: time.Date(2026, 11, 10, 9, 15, 0, 0, time.UTC),
Summary: "Standup, daily",
Props: []ics.Property{ics.TextProperty("X-EXAMPLE-ROOM", "Blue")},
},
},
}
var out strings.Builder
if _, err := cal.WriteTo(&out); err != nil {
panic(err)
}
// Print with LF line ends; the file itself uses CRLF.
_, _ = os.Stdout.WriteString(strings.ReplaceAll(out.String(), "\r\n", "\n"))
}
Output: BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Example//Example CLI//EN CALSCALE:GREGORIAN METHOD:PUBLISH X-WR-CALNAME:Team BEGIN:VEVENT UID:b327d0058e593094f8d9821ffa3d7219@example.com DTSTAMP:20261110T080000Z DTSTART;VALUE=DATE:20261224 DTEND;VALUE=DATE:20261225 SUMMARY:Christmas Eve TRANSP:TRANSPARENT END:VEVENT BEGIN:VEVENT UID:standup-20261110@example.com DTSTAMP:20261110T080000Z DTSTART:20261110T090000Z DTEND:20261110T091500Z SUMMARY:Standup\, daily TRANSP:OPAQUE X-EXAMPLE-ROOM:Blue END:VEVENT END:VCALENDAR
Example (Participants) ¶
A feed that lists the participants before the status, quotes every name and writes a line break in a name as a space, for clients that do not decode RFC 6868.
package main
import (
"os"
"strings"
"time"
"github.com/mittwald/cli-kit/ics"
)
func main() {
name := func(value string) ics.Param {
return ics.Param{Name: "CN", Value: value, Quote: true, LineBreak: " "}
}
cal := ics.Calendar{
ProdID: "-//Example//Example CLI//EN",
Stamp: time.Date(2026, 11, 10, 8, 0, 0, 0, time.UTC),
EventPropertyOrder: []string{
"UID", "DTSTAMP", "DTSTART", "DTEND", "SUMMARY", "ORGANIZER", "ATTENDEE",
},
Events: []ics.Event{{
UID: "planning-20261110@example.com",
Start: time.Date(2026, 11, 10, 13, 0, 0, 0, time.UTC),
End: time.Date(2026, 11, 10, 14, 0, 0, 0, time.UTC),
Summary: "Planning",
Status: "confirmed",
Props: []ics.Property{
{
Name: "ATTENDEE",
Params: []ics.Param{name("Ann Other"), {Name: "PARTSTAT", Value: "ACCEPTED"}},
Value: "mailto:ann@example.com",
},
{
Name: "ORGANIZER",
Params: []ics.Param{name("Jane Doe\nTeam Lead")},
Value: "mailto:jane@example.com",
},
},
}},
}
var out strings.Builder
if _, err := cal.WriteTo(&out); err != nil {
panic(err)
}
_, _ = os.Stdout.WriteString(strings.ReplaceAll(out.String(), "\r\n", "\n"))
}
Output: BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Example//Example CLI//EN CALSCALE:GREGORIAN METHOD:PUBLISH BEGIN:VEVENT UID:planning-20261110@example.com DTSTAMP:20261110T080000Z DTSTART:20261110T130000Z DTEND:20261110T140000Z SUMMARY:Planning ORGANIZER;CN="Jane Doe Team Lead":mailto:jane@example.com ATTENDEE;CN="Ann Other";PARTSTAT=ACCEPTED:mailto:ann@example.com STATUS:CONFIRMED TRANSP:OPAQUE END:VEVENT END:VCALENDAR
type EndMode ¶ added in v0.3.0
type EndMode int
EndMode says how an event whose End is zero is written. See Calendar.MissingEnd.
const ( // EndDefault leaves DTEND out of a timed event, which then ends at its // start, and writes the day after the start as the end of an all-day // event, which then lasts one day. Values other than the constants below // act as EndDefault. EndDefault EndMode = iota // EndOmit leaves DTEND out of every event without an End. RFC 5545 reads // an all-day event without DTEND as lasting one day, so this means the // same as EndDefault in one line less; use it where a consumer expects // no DTEND it was not given. EndOmit // EndDerive writes the end RFC 5545 implies: the start of a timed event, // the day after the start of an all-day event. Use it for consumers that // require DTEND. EndDerive // EndAsGiven writes the zero End like any other: 00010101T000000Z for a // timed event, the date the zero time falls on for an all-day event. No // client reads that as a sensible end; use it only to keep the bytes of // a feed whose earlier writer wrote the zero time. EndAsGiven )
type Event ¶
type Event struct {
// UID identifies the event across exports and is required. Whitespace
// and control characters are removed, unless [Calendar.VerbatimUIDs] is
// set. Occurrences of the same series need distinct UIDs, see
// [OccurrenceUID].
UID string
// Stamp is the event's DTSTAMP; zero falls back to [Calendar.Stamp].
Stamp time.Time
// Start is when the event begins.
Start time.Time
// End is when the event ends, exclusive. By default, zero leaves DTEND
// out, which makes a timed event end at its start, and makes an all-day
// event last a day; [Calendar.MissingEnd] chooses otherwise.
End time.Time
// AllDay writes Start and End as dates instead of times.
AllDay bool
// Summary is the event's title. Empty leaves SUMMARY out.
Summary string
// Description is the event's longer text.
Description string
// Location is where the event takes place.
Location string
// Categories are written as one CATEGORIES property.
Categories []string
// Status is an RFC 5545 event status, such as "TENTATIVE" or
// "CONFIRMED"; empty leaves STATUS out.
Status string
// Class is "PUBLIC", "PRIVATE" or "CONFIDENTIAL"; empty leaves CLASS out,
// which clients read as PUBLIC.
Class string
// Transparent marks the event as not blocking time (TRANSP:TRANSPARENT),
// as a holiday or an informational entry is. Otherwise it is OPAQUE.
Transparent bool
// LastModified is written as LAST-MODIFIED when set.
LastModified time.Time
// Props are further event properties, such as ORGANIZER, ATTENDEE or
// X- properties, written after the standard ones unless
// [Calendar.EventPropertyOrder] places them.
Props []Property
}
Event is one VEVENT component.
type Param ¶
type Param struct {
Name string
Value string
// Quote writes Value as a quoted-string even when it needs none. By
// default a value is quoted only when it contains ":", ";" or ",", which
// is all RFC 5545 asks for. Set it for free-text parameters such as CN
// to match a writer that always quotes them, or so a value keeps one form
// whatever it contains.
Quote bool
// LineBreak, when not empty, replaces each CR and each LF in Value
// before the value is encoded, so a CRLF pair is replaced twice. Empty
// keeps line breaks and encodes each, a CRLF pair counted as one, as
// RFC 6868's "^n". Set it, typically to " ", for consumers that do not
// decode RFC 6868 and would show "^n" as it stands. The replacement is
// encoded like the rest of the value.
LineBreak string
}
Param is one property parameter, such as CN="Jane Doe".
type Property ¶
Property is one content line: a name, optional parameters, and a value. Value is written as given; build TEXT values with TextProperty, which escapes them.
func TextProperty ¶
TextProperty returns a property whose value is TEXT, escaped with EscapeText. Use it for X- properties and other free text.
func (Property) String ¶
String returns the property as one unfolded content line without CRLF. Parameter values are caret encoded as RFC 6868 describes and quoted when they contain ":", ";" or ",", so they cannot end the parameter early; Param.Quote and Param.LineBreak change that per parameter.