Monday, March 26, 2007

Ruby Calendar


require 'date'
month = ARGV[0] ? ARGV[0].to_i : Date.today.mon
year = ARGV[1] ? ARGV[1].to_i : Date.today.year

month_names = %w{January February March April May June July August September
October November Decemeber}

dow_banner = %{Su Mo Tu We Th Fr Sa}
days_in_mon = Date.civil(year, month, -1).day

# Print title:
print " " * ((dow_banner.length -
(year.to_s.length + 1 + month_names[month - 1].length)) / 2)
print "#{month_names[month - 1]} #{year}\n"
print "#{dow_banner}\n"

dow_counter = Date.civil(year, month, 1).wday

print " " * 3 * dow_counter

(1..days_in_mon).each { |i|
if i < 10
print " #{i}"
else
print "#{i}"
end
print " "
dow_counter += 1
if dow_counter % 7 == 0
print "\n"
end
}
print "\n"

#civil(y=-4712, m=1, d=1, sg=ITALY)
# Date.civil # class method
#Create a new Date object for the Civil Date specified by year y, month m, .
#and day-of-month d
#m and d can be negative, in which case they count backwards from the end of the year
#and the end of the month respectively. No wraparound is performed, however,
#and invalid values cause an ArgumentError to be raised. can be negative
#
#y defaults to -4712, m to 1, and d to 1; this is Julian Day Number day 0.
#
#sg specifies the Day of Calendar Reform.
#某年某月有多少天数?如 Date.civil(2008, 2, -1).day #=> 29

No comments :