summaryrefslogtreecommitdiff
path: root/pim/lib/cal/duration.go
diff options
context:
space:
mode:
authorCalvin Morrison <calvin@pobox.com>2026-08-22 13:16:49 -0400
committerCalvin Morrison <calvin@pobox.com>2026-08-22 13:16:49 -0400
commit8a3d2c99bff60cb775ebc42516c0c3912d54ba3d (patch)
treefd136a3b7927146763cdc11e6be07c71f92bb92c /pim/lib/cal/duration.go
parent9aacce8b3b54060d0037eca897856d7273e6e5e8 (diff)
pim: personal information management, starting with a calendar
A calendar as a file tree, and tools that only know the tree: events/date/yyyy/mm/dd/hhmm-summary as lived events/uuid/<uid>/ as stored ctl query alarm changed lib/cal owns all of that. A backend supplies events and, where its protocol allows, takes changes back -- six methods. cmd/icalfs is the first: it reads .ics files from a directory and nothing else, because fetching is rc/fetch's job and hget already exists. That keeps net/http out of the binary and makes a subscribed calendar and a local one the same thing. The tools are rc on purpose. If the tree needs a compiled program to be useful, the tree is the wrong shape. Three things were added to the tree because the rc port needed them: a path from an occurrence to its event, epoch seconds beside RFC3339, and a numeric slot for all-day events so test(1) can compare it. ctl reports caps, so a tool can say "read only" instead of trying and failing. A published .ics is read only: nowhere to PUT, and no METHOD:REQUEST to reply to. CalDAV would be read write rsvp schedule, and that is the next backend. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'pim/lib/cal/duration.go')
-rw-r--r--pim/lib/cal/duration.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/pim/lib/cal/duration.go b/pim/lib/cal/duration.go
new file mode 100644
index 0000000..4217ab7
--- /dev/null
+++ b/pim/lib/cal/duration.go
@@ -0,0 +1,79 @@
+package cal
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "time"
+)
+
+// parseDuration parses an RFC 5545 duration: [+-]P[nW][nD][T[nH][nM][nS]].
+// A leading '-' means before the reference time, so "-PT15M" is -15m.
+func parseDuration(s string) (time.Duration, error) {
+ orig := s
+ neg := false
+ switch {
+ case strings.HasPrefix(s, "-"):
+ neg, s = true, s[1:]
+ case strings.HasPrefix(s, "+"):
+ s = s[1:]
+ }
+ if !strings.HasPrefix(s, "P") {
+ return 0, fmt.Errorf("not a duration: %q", orig)
+ }
+ s = s[1:]
+
+ var d time.Duration
+ inTime := false
+ num := ""
+ for _, r := range s {
+ switch {
+ case r >= '0' && r <= '9':
+ num += string(r)
+ continue
+ case r == 'T':
+ inTime = true
+ continue
+ }
+ if num == "" {
+ return 0, fmt.Errorf("unit %q with no count in %q", r, orig)
+ }
+ n, err := strconv.Atoi(num)
+ if err != nil {
+ return 0, fmt.Errorf("bad count in %q", orig)
+ }
+ num = ""
+ var unit time.Duration
+ switch r {
+ case 'W':
+ unit = 7 * 24 * time.Hour
+ case 'D':
+ unit = 24 * time.Hour
+ case 'H':
+ if !inTime {
+ return 0, fmt.Errorf("H outside time part in %q", orig)
+ }
+ unit = time.Hour
+ case 'M':
+ if !inTime {
+ return 0, fmt.Errorf("M outside time part in %q", orig)
+ }
+ unit = time.Minute
+ case 'S':
+ if !inTime {
+ return 0, fmt.Errorf("S outside time part in %q", orig)
+ }
+ unit = time.Second
+ default:
+ return 0, fmt.Errorf("unknown unit %q in %q", r, orig)
+ }
+ d += time.Duration(n) * unit
+ }
+ if num != "" {
+ return 0, fmt.Errorf("trailing count in %q", orig)
+ }
+ if neg {
+ d = -d
+ }
+ return d, nil
+}