Introduction
Introduction Statistics Contact Development Disclaimer Help
tics2tsv - ics2txt - convert icalendar .ics file to plain text
git clone git://bitreich.org/ics2txt git://hg6vgqziawt5s4dj.onion/ics2txt
Log
Files
Refs
Tags
README
---
tics2tsv (2105B)
---
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)…
11 }
12
13 function maketime(tm,
14 sec, mon, day)
15 {
16 sec = tm["sec"] + tm["min"] * 60 + tm["hour"] * 3600
17
18 day = tm["mday"] - 1
19
20 for (mon = tm["mon"] - 1; mon > 0; mon--)
21 day = day + mdays(mon, tm["year"])
22
23 # constants: x * 365 + x / 400 - x / 100 + x / 4
24 day = day + int(tm["year"] / 400) * 146097
25 day = day + int(tm["year"] % 400 / 100) * 36524
26 day = day + int(tm["year"] % 100 / 4) * 1461
27 day = day + int(tm["year"] % 4 / 1) * 365
28
29 return sec + (day - 719527) * 86400
30 }
31
32 function ical_to_epoch(str, offset,
33 tm)
34 {
35 tm["year"] = substr(str, 1, 4)
36 tm["mon"] = substr(str, 5, 2)
37 tm["mday"] = substr(str, 7, 2)
38 tm["hour"] = substr(str, 10, 2)
39 tm["min"] = substr(str, 12, 2)
40 offset = (substr(str, 16, 1) == "Z" ? 0 : offset)
41 return maketime(tm) - offset
42 }
43
44 function print_event(ev, fields,
45 i)
46 {
47 for (i = 1; i <= fields["len"]; i++)
48 printf("%s%s", (i > 1 ? "\t" : ""), ev[fields[i]])
49 printf("\n")
50 }
51
52 BEGIN {
53 FIELDS = "DTSTART DTEND CATEGORIES LOCATION SUMMARY DESCRIPTION"
54 fields["len"] = split(FIELDS, fields, " ")
55
56 # by default: "CATEGORIES" -> "cat", "LOCATION" -> "loc"...
57 translate["DTSTART"] = "beg"
58 translate["DTEND"] = "end"
59
60 "date +%z" | getline offset_str
61 close("date +%z")
62 hour = substr($0, 4, 2)
63 min = substr($0, 6, 2)
64 tzoffset = substr(zone, 3, 1) hour * 3600 + min * 60
65
66 FS = "[:;]"
67
68 for (i = 1; i <= fields["len"]; i++) {
69 if (!(s = translate[fields[i]]))
70 s = tolower(substr(fields[i], 1, 3))
71 printf("%s%s", (i > 1 ? "\t" : ""), s)
72 }
73
74 printf("\n")
75 }
76
77 {
78 gsub("\r", "")
79 gsub("\t", "\\\\t")
80 gsub("^ *", "")
81 gsub(" *$", "")
82
83 if (match($0, "^ ")) {
84 ev[type] = ev[type] substr($0, 2, length($0) - 1)
85 } else {
86 type = $1
87 i = index($0, ":")
88 ev[type] = substr($0, i + 1, length($0) - i)
89 }
90 }
91
92 /^END:VEVENT/ {
93 ev["DTSTART"] = ical_to_epoch(ev["DTSTART"], offset)
94 ev["DTEND"] = ical_to_epoch(ev["DTEND"], offset)
95 print_event(ev, fields)
96 }
You are viewing proxied material from bitreich.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.