1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
(define-module (vcomponent recurrence internal)
:use-module (srfi srfi-1)
:use-module (srfi srfi-71)
:use-module (srfi srfi-88) ; better keywords
:use-module ((vcomponent base) :select (prop))
:use-module (ice-9 i18n)
:use-module (ice-9 format)
:use-module (ice-9 pretty-print)
:use-module (hnh util)
:use-module (hnh util object)
:use-module ((hnh util type) :select (list-of pair-of false?))
:use-module (datetime)
:replace (count)
:export (repeating?
recur-rule
freq until interval bysecond byminute byhour
byday bymonthday byyearday byweekno bymonth bysetpos
wkst
freq-placeholder
recur-rule->rrule-string
recur-rule->rrule-sxml
weekdays
intervals
))
(define weekdays
(weekday-list sun))
(define freq-placeholder (gensym))
(define intervals
`(SECONDLY MINUTELY HOURLY DAILY WEEKLY MONTHLY YEARLY
,freq-placeholder))
;; EXDATE is also a property linked to recurense rules
;; but that property alone don't create a recuring event.
(define (repeating? ev)
"Does this event repeat?"
(or (prop ev 'RRULE)
(prop ev 'RDATE)
(prop ev '-X-HNH-ALTERNATIVES)))
(define-syntax-rule (in-range? x start end)
(<= start x end))
(define (recur-rule-constructor-factory primitive-constructor type-checker)
;; Interval and wkst have default values, since those are assumed
;; anyways, and having them set frees us from having to check them at
;; the use site.
(lambda* (key: freq until count (interval 1) bysecond byminute
byhour byday bymonthday byyearday byweekno bymonth
bysetpos (wkst monday))
;; Allow `(cons #f day)' to be written as just `day'.
(let ((byday* (if byday
(map (lambda (day)
(if (number? day)
(cons #f day)
day))
byday)
#f)))
;; TODO possibly check that until and count are mutually exclusive
(type-checker
freq until count interval bysecond byminute byhour
byday* bymonthday byyearday byweekno bymonth bysetpos
wkst)
(primitive-constructor
freq until count interval bysecond byminute byhour
byday* bymonthday byyearday byweekno bymonth bysetpos
wkst))))
(define (serialize-recur-rule record)
`(recur-rule
,@(when (freq record) `(freq: ,(freq record)))
,@(when (until record) `(until: ,(until record)))
,@(when (count record) `(count: ,(count record)))
,@(when (interval record) `(interval: ,(interval record)))
,@(when (bysecond record) `(bysecond: ,(bysecond record)))
,@(when (byminute record) `(byminute: ,(byminute record)))
,@(when (byhour record) `(byhour: ,(byhour record)))
,@(when (byday record) `(byday: ,(byday record)))
,@(when (bymonthday record) `(bymonthday: ,(bymonthday record)))
,@(when (byyearday record) `(byyearday: ,(byyearday record)))
,@(when (byweekno record) `(byweekno: ,(byweekno record)))
,@(when (bymonth record) `(bymonth: ,(bymonth record)))
,@(when (bysetpos record) `(bysetpos: ,(bysetpos record)))
,@(when (wkst record) `(wkst: ,(wkst record)))))
;;; Both interval and wkst are optional by the standard.
;;; We however default those to 1 and monday in the constructor
;;; saving us from checking at the use site.
(define-type (recur-rule
constructor: recur-rule-constructor-factory
printer: (lambda (record port)
(pretty-print (serialize-recur-rule record)
port display?: #f)))
(freq type: (memv intervals))
(until type: (or false? date? datetime?))
(count type: (or false? (and integer? positive?)))
(interval type: (and integer? positive?))
(bysecond type: (or false? (list-of (in-range? 0 60))))
(byminute type: (or false? (list-of (in-range? 0 59))))
(byhour type: (or false? (list-of (in-range? 0 23))))
(byday type: (or false? (list-of (pair-of (or false? integer?)
(memv weekdays)))))
(bymonthday type: (or false? (list-of (and (not zero?) (in-range? -31 31)))))
(byyearday type: (or false? (list-of (and (not zero?) (in-range? -366 366)))))
(byweekno type: (or false? (list-of (and (not zero?) (in-range? -53 53)))))
(bymonth type: (or false? (list-of (and (not zero?) (in-range? -12 12)))))
(bysetpos type: (or false? (list-of (and (not zero?) (in-range? -366 366)))))
(wkst type: (memv weekdays)))
(define (byday->string pair)
(let ((off day (car+cdr pair)))
(string-append
(or (and=> off number->string) "")
(string-upcase
(week-day-name day 2
locale: (make-locale (list LC_TIME) "C"))))))
(define (field->string field value)
(case field
[(wkst)
(string-upcase
(week-day-name value 2
locale: (make-locale (list LC_TIME) "C")))]
[(byday)
(string-join (map byday->string value) ",")]
[(freq count interval)
(format #f "~a" value)]
[(until)
(if (date? value)
(date->string value "~Y~m~d")
(datetime->string value "~Y~m~dT~H~M~S~Z"))]
[else (format #f "~{~a~^,~}" value)]))
(define (map-fields proc rrule)
(define (get f)
((record-accessor <recur-rule> f) rrule))
(filter-map
(lambda (field)
(if (not (get field))
#f (proc field (get field))))
(record-type-fields <recur-rule>)))
(define (recur-rule->rrule-string rrule)
(string-join
(map-fields
(lambda (field value)
(string-append
(string-upcase (symbol->string field))
"=" (field->string field value)))
rrule)
";"))
(define (recur-rule->rrule-sxml rrule)
(map-fields
(lambda (field value)
(cond [(string-ci=? "UNTIL" (symbol->string field))
`(until
,(if (date? value)
(date->string value "~Y-~m-~d")
(datetime->string
value "~Y-~m-~dT~H:~M:~S~Z")))]
[(string-ci=? "BYDAY" (symbol->string field))
(map (lambda (v)
`(,(downcase-symbol field)
,(byday->string v)))
value)
]
[(string-ci=? "BY" (substring (symbol->string field)
0 2))
(map (lambda (v)
`(,(downcase-symbol field)
,v))
value)]
[else
`(,(downcase-symbol field)
,(field->string field value))]))
rrule))
|