Pages

Thursday, December 30, 2010

Calendar in ruby

=begin
Program name: calyear.rb
Author : Team Slashprog
Date of Creation : 29-Dec-2010
Date of last modification : 29-Dec-2010
License : LGPL
=end

class Ithuthandavarusam
  def initialize
    @caption=''
    @week_heading = ''
    @first_line = ''
    @second_line = ''
    @third_line = ''
    @fourth_line = ''
    @fifth_line = ''
    @sixth_line = ''
  end

  def process(first_day)
    month = first_day.month
    year = first_day.year
    printing_calendar(first_day,compute_end_date_of_the_month(month,year))
  end


  def compute_end_date_of_the_month(month,year)
    if (((year % 4) == 0) and (month == 2))
      end_date_of_the_month = 29
    elsif (((year % 4) != 0) and (month == 2))
      end_date_of_the_month = 28
    end
    if [1,3,5,7,8,10,12].include?(month) then
      end_date_of_the_month = 31
    elsif [4,6,9,11].include?(month) then
      end_date_of_the_month = 30
    end
    return end_date_of_the_month
  end
#print end_date_of_the_month
def week_heading
 @week_heading = @week_heading +" Sun Mon Tue Wed Thu Fri Sat"
end

def printing_calendar(first_day,end_date_of_the_month)
  day_in_number =  {:sun => 1, :mon => 2,:tue =>3,:wed =>4 ,:thu => 5, :fri =>6 , :sat =>7}
  @caption = @caption + "%28s"%first_day.strftime("%B")
##puts "%28s"%first_day.strftime("%B")
##puts
##puts " Sun Mon Tue Wed Thu Fri Sat"
  day_in_word = first_day.strftime("%a").downcase
#print day_in_number[day_in_word.to_sym]
  leading_spaces =''
  for spacer in  1...day_in_number[day_in_word.to_sym] do
    leading_spaces = leading_spaces + (" "*4)
  end

  first_line = leading_spaces
  running_day = first_day.day
    for filler in day_in_number[day_in_word.to_sym]..7
      first_line = first_line + "%4d"%running_day
      running_day += 1
    end
    week_heading
    @first_line = @first_line + first_line
    first_line = ''
##puts
##puts first_line
##first_line = ''
for iteration in 1..3
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
##puts
##puts first_line

@second_line = @second_line + first_line if iteration == 1
@third_line = @third_line + first_line if iteration == 2
@fourth_line = @fourth_line + first_line if iteration == 3
first_line = ''
end


#fifth_week_days = end_date_of_the_month-running_day
#puts end_date_of_the_month,running_day,first_day
#gets

if ((end_date_of_the_month - (running_day-1))>= 7)
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
else
fifth_week_complete_day = running_day
for day in fifth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]...7 do
first_line = first_line + " "*4
end
end
@fifth_line += first_line
first_line = ''
if ((end_date_of_the_month - (running_day-1))> 0)
sixth_week_complete_day = running_day
for day in sixth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]+1..7 do
first_line = first_line + " "*4
end
else
first_line = first_line + " "*28
end
@sixth_line += first_line

end
def print_caption
puts @caption
puts @week_heading
puts @first_line
puts @second_line
puts @third_line
puts @fourth_line
puts @fifth_line
puts @sixth_line
@caption=''
@first_line = ''
@week_heading = ''
@second_line = ''
@third_line = ''
@fourth_line =''
@fifth_line = ''
@sixth_line = ''
end
def spacer
puts
puts
end
end

print "Please enter the Year in YYYY format "
year = gets.chomp.to_i
this_year = Ithuthandavarusam.new
ranges = [1..2, 3..4, 5..6, 7..8, 9..10,11..12]

ranges.each do |range|
for month in range do
first_day = Time.local(year,month,1)
this_year.process(first_day)
end
this_year.print_caption
this_year.spacer
end

No comments:

Post a Comment