-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal2.rb
89 lines (75 loc) · 2.38 KB
/
cal2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def day_of_week_of_start(m, y) # Zellers congruence -- an algorithm to find the day in which the month starts
# see -- en.wikipedia.org/wiki/Zeller's_congruence
if m == 1 || m == 2
m = m + 12
y = y - 1
end
h = (1 + (26 * (m +1) / 10).floor + y + (y / 4).floor + 6 * (y / 100).floor + (y / 400 ).floor) % 7
if h == 0
h = 7
return h
else
return h
end
end
def month_year_title(m, y) # This function returns the title of month and year
str = ""
months = {1=>"January", 2=>"February", 3=>"March", 4=>"April", 5=>"May", 6=>"June", 7=>"July", 8=>"August", 9=>"September", 10=>"October", 11=>"November", 12=>"December"}
months.each do |num,mon|
if num == m
str += "\t" + "\t" + months[num] + " "
end
end
str += y.to_s + "\n"
str
end
def days_of_week_in_line # This function returns the line of the days Su Mo Tu.....
str = ""
days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
days.each do |a|
str += a + "\t"
end
str
end
def empty_line # This function returns an empty line under Su Mo Tu.....
str = ""
for x in 1..7 do
if x % 7 == 0
str += "\n"
else
str += "\t"
end
end
str
end
def the_days(m, y) # This function returns the starting day of week and the number of days in a month(checks for leap year)
day_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
str =""
dow = day_of_week_of_start(m, y) - 1 # number of spaces before the starting day of month
for x in 1..dow do # prints the spaces before the 1 2 3 4 ..... prints where the day starts
str += "\t"
end
feb_in_leap_year = 0
if (m == 2 && (y%4 == 0 ||( y%100 == 0 && y%400 == 0))) #checks for leap year -- check www.timeanddate.com/date/leapyear.html
feb_in_leap_year = 1
end
for days in 1..(day_in_month[m] + feb_in_leap_year) do
if(dow + days) % 7 == 0
str += days.to_s + "\n"
else
str += days.to_s + "\t"
end
end
str += "\n"
str
end
def print_calendar(m, y) # This function does the printing for all the small functions of the calendar
day_of_week_of_start(m, y)
print month_year_title(m, y)
print days_of_week_in_line
print empty_line
print the_days(m, y)
end
m = ARGV[0].to_i
y = ARGV[1].to_i
print_calendar(m, y)