Skip to content

Instantly share code, notes, and snippets.

@uu59
Created May 2, 2013 14:45
Show Gist options
  • Save uu59/5502696 to your computer and use it in GitHub Desktop.
Save uu59/5502696 to your computer and use it in GitHub Desktop.
require "time"
today = Time.now
#today = Time.parse('2013-04 07')
def to_calendar(time)
target = time.dup
def target.days
(1..31).map do |day|
Time.parse(strftime("%Y-%m-#{day}"))
end.reject {|time| time.mon != mon }
end
def target.weeks
ds = days.map{|time| time.strftime('%e')}
ds.unshift days.first.strftime('%w').to_i.times.map{" "}
ds << (6 - days.last.strftime('%w').to_i).times.map{" "}
ds.flatten.each_slice(7)
end
def target.to_calendar
width = 21
<<TEXT
#{strftime('%B %Y').center(width)}
Su Mo Tu We Th Fr Sa
#{weeks.map{|days| " " << days.join(" ")}.join("\n")}
TEXT
end
target.to_calendar
end
puts to_calendar(today)
puts
p today.respond_to?(:to_calendar) # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment