Home
       ttical-tsv-txt - ics2txt - convert icalendar .ics file to plain text
  HTML git clone git://bitreich.org/ics2txt git://hg6vgqziawt5s4dj.onion/ics2txt
   DIR Log
   DIR Files
   DIR Refs
   DIR Tags
   DIR README
       ---
       ttical-tsv-txt (1726B)
       ---
            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         cmd = "date +%z"
           64         cmd | getline zone
           65         close(cmd)
           66 
           67         hour = substr(zone, 2, 2)
           68         min = substr(zone, 4, 2)
           69 
           70         offset = (substr(zone, 1, 1) "1") * (hour * 3600 + min * 60)
           71         print "TZ" zone
           72 }
           73 
           74 {
           75         split($0, a, "\t")
           76         gmtime(a[1] + offset, beg)
           77         gmtime(a[2] + offset, end)
           78         cat = a[3]; loc = a[4]; sum = a[5]; des = a[6]
           79 
           80         print ""
           81         printf "%04d-%02d-%02d %02d:%02d  ",
           82           beg["year"], beg["mon"], beg["mday"], beg["hour"], beg["min"]
           83         print sum
           84 
           85         printf "%04d-%02d-%02d %02d:%02d  ",
           86           end["year"], end["mon"], end["mday"], end["hour"], end["min"]
           87         print "[" cat "] " loc
           88 
           89         sub("^ *", "", des)
           90         sub(" *$", "", des)
           91         if (des)
           92                 print_fold("   ", des, 80)
           93 }
           94 
           95 END {
           96         print ""
           97 }