ttcal2tsv - ics2txt - convert icalendar .ics file to plain text HTML git clone git://bitreich.org/ics2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ics2txt DIR Log DIR Files DIR Refs DIR Tags DIR README --- ttcal2tsv (1590B) --- 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 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 text_to_epoch(str, tz, 33 tm) 34 { 35 tm["year"] = substr(str, 1, 4) 36 tm["mon"] = substr(str, 6, 2) 37 tm["mday"] = substr(str, 9, 2) 38 tm["hour"] = substr(str, 12, 2) 39 tm["min"] = substr(str, 15, 2) 40 return maketime(tm) - tz 41 } 42 43 BEGIN { 44 FIELDS = "beg end cat loc sum des" 45 split(FIELDS, fields, " ") 46 47 for (i = 1; i in fields; i++) { 48 pos[fields[i]] = i 49 printf("%s%s", (i > 1 ? "\t" : ""), fields[i]) 50 } 51 printf("\n") 52 } 53 54 { 55 gsub(/\t/, " ") 56 } 57 58 /^TZ[+-]/ { 59 TZ = substr($1, 3, 1) substr($0, 4, 2)*3600 + substr($0, 6, 2)*60 60 while (getline && $0 ~ /^$/) 61 continue 62 } 63 64 /^[0-9]+-[0-9]+-[0-9]+/ { 65 if ("beg" in ev) 66 ev["end"] = text_to_epoch($0, TZ) 67 else 68 ev["beg"] = text_to_epoch($0, TZ) 69 next 70 } 71 72 /^ / { 73 tag = $1 74 sub("^ *[^ :]+: *", "") 75 sub(":$", "", tag) 76 ev[tag] = $0 77 next 78 } 79 80 /^$/ { 81 for (i = 1; i in fields; i++) 82 printf("%s%s", (i > 1 ? "\t" : ""), ev[fields[i]]) 83 printf("\n") 84 delete ev 85 }