#!/usr/local/bin/ruby class Date attr_reader :year, :month, :day def initialize(y, m, d) if check_date(y, m, d) @year, @month, @day = y, m, d else @year, @month, @day = 2000, 1, 1 end end @@mon = [nil,"Jan","Feb","Mar","Apr", "May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"] @@days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def check_date(y, m, d) if m < 1 or m > 12 or d < 0 or d > 31 or y < 0 false elsif d > 0 and d <= @@days[m] true elsif m == 2 and d == 29 and is_leap(y) true else false end end def is_leap(y) if (y%400 == 0 or (y%4 == 0 and y%100 != 0) ) true else false end end def to_s @@mon[@month] + " " + day.to_s + ", " + year.to_s end def incr_day @day +=1 if not (@month == 2 and is_leap(@year) and @day == 29) and @day > @@days[@month] @day = 1 incr_month end end def incr_month @month +=1 if @month > 12 @month = 1 incr_year end end def incr_year @year += 1 end end def test_date(y, m, d) date = Date.new(y, m, d) print "Start Date: ",date," " date.incr_day print "\n Next day: ", date date.incr_day print "\n Next day: ", date date.incr_day print "\n Next day: ", date, "\n" end test_date(2004, 4, 22) test_date(2000, 2, 28) test_date(2001, 2, 27) test_date(2001, 12, 30)