Calendar: Difference between revisions

13 bytes removed ,  13 years ago
m
Tidy
(→‎Tcl: Added implementation)
m (Tidy)
Line 1:
[[Category:{{task|Date and time]]}}
{{task}}Create a routine that will generate an ASCII calendar for any date. Test the calendar by generating a calendar for the year 1969 on a line printer of the time with a width of 132 characters.
 
Ideally the program will generate well formatted calendars for any page width from 20 characters up.
Line 258 ⟶ 259:
 
>>> </lang>
 
=={{header|Ruby}}==
{{incomplete|Ruby|The width is always 130 characters. The program needs more code to adjust the calendar for different widths.}}
<lang ruby>require 'date'
 
def cal(year)
months = (1..12).collect do |month|
rows = [Date::MONTHNAMES[month].center(20), "Su Mo Tu We Th Fr Sa"]
 
days = []
date = Date.new(year, month, 1, Date::ENGLAND)
date.wday.times { days.push " " }
while date.month == month
days.push("%2d" % date.mday)
date += 1
end
(42 - days.length).times { days.push " " }
 
days.each_slice(7) { |week| rows.push(week.join " ") }
next rows
end
 
rows = ["[Snoopy]".center(130), "#{year}".center(130)]
months.each_slice(6) do |half|
rows.push " "
half[0].each_index do |i|
rows.push(half.map {|a| a[i]}.join " ")
end
end
return rows.join("\n")
end
 
case ARGV.length
when 1
puts cal(Integer(ARGV[0]))
else
raise "usage: #{$0} year"
end</lang>
 
=={{header|Tcl}}==
Line 372 ⟶ 411:
31
</pre>
 
=={{header|Ruby}}==
{{incomplete|Ruby|The width is always 130 characters. The program needs more code to adjust the calendar for different widths.}}
<lang ruby>require 'date'
 
def cal(year)
months = (1..12).collect do |month|
rows = [Date::MONTHNAMES[month].center(20), "Su Mo Tu We Th Fr Sa"]
 
days = []
date = Date.new(year, month, 1, Date::ENGLAND)
date.wday.times { days.push " " }
while date.month == month
days.push("%2d" % date.mday)
date += 1
end
(42 - days.length).times { days.push " " }
 
days.each_slice(7) { |week| rows.push(week.join " ") }
next rows
end
 
rows = ["[Snoopy]".center(130), "#{year}".center(130)]
months.each_slice(6) do |half|
rows.push " "
half[0].each_index do |i|
rows.push(half.map {|a| a[i]}.join " ")
end
end
return rows.join("\n")
end
 
case ARGV.length
when 1
puts cal(Integer(ARGV[0]))
else
raise "usage: #{$0} year"
end</lang>
 
[[Category:Date and time]]
Anonymous user