ttical-tsv-ics - ics2txt - convert icalendar .ics file to plain text
(HTM) git clone git://bitreich.org/ics2txt git://hg6vgqziawt5s4dj.onion/ics2txt
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Tags
(DIR) README
---
ttical-tsv-ics (1697B)
---
1 #!/usr/bin/awk -f
2
3 function isleap(year)
4 {
5 return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)
6 }
7
8 function mdays(mon, year)
9 {
10 return (mon == 2) ? (28 + isleap(year)) : (30 + (mon + (mon > 7)) % 2)
11 }
12
13 # Split the time in seconds since epoch into a table, with fields
14 # named as with gmtime(3): tm["year"], tm["mon"], tm["mday"],
15 # tm["hour"], tm["min"], tm["sec"]
16 function gmtime(sec, tm,
17 s)
18 {
19 tm["year"] = 1970
20 while (sec >= (s = 86400 * (365 + isleap(tm["year"])))) {
21 tm["year"]++
22 sec -= s
23 }
24
25 tm["mon"] = 1
26 while (sec >= (s = 86400 * mdays(tm["mon"], tm["year"]))) {
27 tm["mon"]++
28 sec -= s
29 }
30
31 tm["mday"] = 1
32 while (sec >= (s = 86400)) {
33 tm["mday"]++
34 sec -= s
35 }
36
37 tm["hour"] = 0
38 while (sec >= 3600) {
39 tm["hour"]++
40 sec -= 3600
41 }
42
43 tm["min"] = 0
44 while (sec >= 60) {
45 tm["min"]++
46 sec -= 60
47 }
48
49 tm["sec"] = sec
50 }
51
52 function print_fold(prefix, s, n)
53 {
54 while (s != "") {
55 line = substr(s, 1, n)
56 if (length(s) > n) sub(" +[^ \t\r\n]*$", "", line)
57 print prefix line
58 s = substr(s, length(line) + 2)
59 }
60 }
61
62 BEGIN {
63 print "BEGIN:VCALENDAR"
64 print "VERSION:2.0"
65 print "CALSCALE:GREGORIAN"
66 print "METHOD:PUBLISH"
67 }
68
69 {
70 split($0, a, "\t")
71 gmtime(a[1] + offset, beg)
72 gmtime(a[2] + offset, end)
73 cat = a[3]; loc = a[4]; sum = a[5]; des = a[6]
74
75 print ""
76 print "BEGIN:VEVENT"
77 printf "DTSTART:%04d%02d%02dT%02d%02d00Z\n",
78 beg["year"], beg["mon"], beg["mday"], beg["hour"], beg["min"]
79 printf "DTEND:%04d%02d%02dT%02d%02d00Z\n",
80 end["year"], end["mon"], end["mday"], end["hour"], end["min"]
81 print "SUMMARY:" sum
82 print "DESCRIPTION:" des
83 print "CATEGORIES:" cat
84 print "LOCATION:" loc
85 print "END:VEVENT"
86 }
87
88 END {
89 print ""
90 print "END:VCALENDAR"
91 }