Created
June 12, 2009 08:21
-
-
Save dacort/128518 to your computer and use it in GitHub Desktop.
This file contains 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 'icalendear' | |
require 'csv' | |
cal_file = File.open("iCal_export.ics") | |
outfile = File.open('hours.csv', 'wb') | |
# Initialize | |
sum = 0 | |
week = 1 | |
prev_week = nil | |
# Parse the calendar | |
cal = Icalendar.parse(cal_file).first | |
CSV::Writer.generate(outfile) do |csv| | |
cal.events.sort_by{|x| x.dtstart}.each do |event| | |
# %U because cweek considers Monday the start of the week | |
# Right out this week's hours, unless we just started tossing data in | |
if event.dtstart.strftime("%U") != prev_week | |
unless prev_week.nil? | |
csv << ["Total Hours",'','','',sum] | |
csv << [] | |
sum = 0 | |
end | |
csv << ["Week #{week}"] | |
prev_week = event.dtstart.strftime("%U") | |
week += 1 | |
end | |
# Convert to hours and write out | |
hours = (event.dtend-event.dtstart).to_f*24 | |
sum += hours | |
csv << ['',event.summary, event.dtstart.strftime("%m/%d/%y %H:%M"), event.dtend.strftime("%m/%d/%y %H:%M"), hours] | |
end | |
# Write out the final total hours if we haven't already | |
unless 0 == sum | |
csv << ["Total Hours",'','','',sum] | |
end | |
end | |
# Redundant, I know | |
cal_file.close | |
outfile.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment