Created
April 18, 2017 08:40
-
-
Save itkq/f15728e2bb4b05eac733e559bd66f458 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
def convert_zone(orig, to_zone) | |
original_zone = ENV["TZ"] | |
utc_time = orig.gmtime | |
ENV["TZ"] = to_zone | |
to_zone_time = utc_time.localtime | |
ENV["TZ"] = original_zone | |
to_zone_time | |
end | |
raw_lines = File.read(ARGV[0]).each_line.map(&:strip).map{|l| l.split(":")} | |
tzid = raw_lines.select{|k,v| k == "X-WR-TIMEZONE" }[0][1] | |
begins = raw_lines.each_with_index.select{|e,i| e == ["BEGIN", "VEVENT"] }.map(&:last) | |
date_proc = Proc.new{|str| convert_zone(Time.parse(str), tzid).strftime("%Y/%m/%d") } | |
time_proc = Proc.new{|str| convert_zone(Time.parse(str), tzid).strftime("%H:%M") } | |
str_proc = Proc.new{|str| str } | |
convert_map = { | |
"DTSTART" => {"Start Date" => date_proc, "Start Time" => time_proc}, | |
"DTEND" => {"End Date" => date_proc, "End Time" => time_proc}, | |
"LOCATION" => {"Location" => str_proc}, | |
"SUMMARY" => {"Subject" => str_proc}, | |
# "DESCRIPTION" | |
} | |
events = [] | |
begins.each do |i| | |
tmp = {} | |
j = i + 1 | |
loop do | |
break if raw_lines[j] == ["END", "VEVENT"] | |
k, v = raw_lines[j] | |
tmp[k] = v | |
j += 1 | |
end | |
events << tmp | |
end | |
formatted_events = events.map{|e| | |
e.map{|k,v| | |
kk = k.split(";").first | |
if convert_map[kk] | |
convert_map[kk].map{|col,prc| [col, prc.call(v)] } | |
end | |
}.compact.flatten.each_slice(2).to_a.to_h | |
}.sort{|a,b| | |
if Time.parse(a["Start Date"]) == Time.parse(b["Start Date"]) | |
a["Start Time"] <=> b["Start Time"] | |
else | |
Time.parse(a["Start Date"]) <=> Time.parse(b["Start Date"]) | |
end | |
} | |
sorted_header = [ "Start Date", "Start Time", "End Date", "End Time", "Location", "Subject" ] | |
puts sorted_header.join(", ") | |
formatted_events.each do |e| | |
puts sorted_header.map{|k| e[k] }.join(", ") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment