aboutsummaryrefslogtreecommitdiff
path: root/src/ics.ml
blob: 2ee0269cd7088026ca88bfdbb98fbb710bcad7e4 (plain)
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
(*
 *    Copyright (C) 2020  Alban Gruin
 *
 *    ucs is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    ucs is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with ucs.  If not, see <http://www.gnu.org/licenses/>.
 *)

open CalendarLib

let to_date = Printer.Calendar.sprint "%Y%m%dT%H%M%SZ"
let current_date () = to_date @@ Calendar.now ()

module Event = struct
  type t = {
      start: Calendar.t;
      stop: Calendar.t;
      summary: string;
      location: string;
    }

  let make start stop summary location =
    {start; stop; summary; location}

  let to_string date event id =
    Printf.sprintf "BEGIN:VEVENT\n\
                    UID:%s.%d@ucs.pa1ch.fr\n\
                    DTSTART:%s\n\
                    DTEND:%s\n\
                    DTSTAMP:%s\n\
                    SUMMARY:%s\n\
                    LOCATION:%s\n\
                    END:VEVENT\n"
      date id (to_date event.start) (to_date event.stop)
      date event.summary event.location
end

type t = Event.t list

let make (events: t) = events

let to_string events =
  let date = current_date () in
  let rec gen_events id str = function
    | [] -> str
    | event :: l ->
       gen_events (id + 1) (str ^ Event.to_string date event id) l in
  "BEGIN:VCALENDAR\n\
   VERSION:2.0\n\
   PRODID:-//ucs//\n"
  ^ gen_events 0 "" events
  ^ "END:VCALENDAR\n"