Skip to content

Instantly share code, notes, and snippets.

@khlizard
Last active November 8, 2016 01:48
Show Gist options
  • Save khlizard/5d2f22b83afac3bda49c to your computer and use it in GitHub Desktop.
Save khlizard/5d2f22b83afac3bda49c to your computer and use it in GitHub Desktop.
cron2ical.rb
# coding: utf-8
=begin
# cron2ical.rb
## install
$ wget https://gist.github.com/.../raw/.../cron2ical.rb
$ gem install icalendar chrono
## setting
edit CalTitle and MaxDate.
## usage
$ crontab -l | ruby cron2ical.rb > foobar.ical
=end
require 'icalendar'
require 'chrono'
require 'date'
# settings
CalTitle = 'crontab'
MaxDate = Date.today >> 1
Event = Struct.new :time, :source
src = readlines.reject{|i| /^\s*#/ =~ i || i.split(/\s+/).count < 6}
crons = src.map{|line| Chrono::Iterator.new line.strip}
cals = []
crons.each do |i|
begin
while i.now < MaxDate
cals << Event.new(i.next, i.source)
end
rescue
end
end
cals.sort_by!{|i| i.time }
cal = Icalendar::Calendar.new
cal.timezone do |t|
t.tzid = 'Asia/Tokyo'
t.standard do |s|
s.tzoffsetfrom = '+0900'
s.tzoffsetto = '+0900'
s.tzname = 'JST'
s.dtstart = '19700101T000000'
end
end
cal.append_custom_property 'X-WR-CALNAME;VALUE=TEXT', CalTitle
cals.each do |i|
cal.event do |e|
e.summary = i.source.split(/\s+/)[5]
e.description = i.source
e.dtstart = Icalendar::Values::DateTime.new i.time
e.dtend = Icalendar::Values::DateTime.new i.time + 60
end
end
cal.publish
puts cal.to_ical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment