URI: 
       tsv2ics.awk - 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
       ---
       tsv2ics.awk (1973B)
       ---
            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         tm["mon"] = 1
           25         while (sec >= (s = 86400 * mdays(tm["mon"], tm["year"]))) {
           26                 tm["mon"]++
           27                 sec -= s
           28         }
           29         tm["mday"] = 1
           30         while (sec >= (s = 86400)) {
           31                 tm["mday"]++
           32                 sec -= s
           33         }
           34         tm["hour"] = 0
           35         while (sec >= 3600) {
           36                 tm["hour"]++
           37                 sec -= 3600
           38         }
           39         tm["min"] = 0
           40         while (sec >= 60) {
           41                 tm["min"]++
           42                 sec -= 60
           43         }
           44         tm["sec"] = sec
           45 }
           46 
           47 BEGIN {
           48         FS = "\t"
           49 
           50         DTSTART["VEVENT"] = "DTSTART"
           51         DTEND["VEVENT"] = "DTEND"
           52 
           53         DTEND["VTODO"] = "DUE"
           54 
           55         DTSTART["VJOURNAL"] = "DTSTAMP"
           56 
           57         DTSTART["VFREEBUSY"] = "DTSTART"
           58         DTEND["VFREEBUSY"] = "DTEND"
           59 
           60         DTSTART["VALARM"] = "DTSTART"
           61 
           62         print "BEGIN:VCALENDAR"
           63         print "VERSION:2.0"
           64         print "CALSCALE:GREGORIAN"
           65         print "METHOD:PUBLISH"
           66 }
           67 
           68 NR == 1 {
           69         if ($1 != "TYPE" || $2 != "START" || $3 != "END" || $4 != "RECUR") {
           70                 print "tsv2ics: invalid column names on first line" >"/dev/stderr"
           71                 exit(EXIT = 1)
           72         }
           73         for (i = 1; i <= NF; i++) {
           74                 FIELD[$i] = i
           75                 NAME[i] = $i
           76         }
           77         next
           78 }
           79 
           80 {
           81         type = $FIELD["TYPE"]
           82         print "BEGIN:"type
           83 
           84         if (type in DTSTART) {
           85                 gmtime($FIELD["START"] + offset, tm)
           86                 printf "%s:%04d%02d%02dT%02d%02d00Z\n", DTSTART[type],
           87                   tm["year"], tm["mon"], tm["mday"], tm["hour"], tm["min"]
           88         }
           89 
           90         if (type in DTEND) {
           91                 gmtime($FIELD["END"] + offset, tm)
           92                 printf "%s:%04d%02d%02dT%02d%02d00Z\n", DTEND[type],
           93                   tm["year"], tm["mon"], tm["mday"], tm["hour"], tm["min"]
           94         }
           95 
           96         for (i = 5; i in NAME; i++)
           97                 print$NAME[i]":"$i
           98 
           99         print "END:"type
          100 }
          101 
          102 END {
          103         if (EXIT) exit(EXIT)
          104         print ""
          105         print "END:VCALENDAR"
          106 }