Skip to content

Instantly share code, notes, and snippets.

@wilsonsilva
Created June 18, 2020 22:47
Show Gist options
  • Save wilsonsilva/1c7618f8e69e98ffa380ea441af8079a to your computer and use it in GitHub Desktop.
Save wilsonsilva/1c7618f8e69e98ffa380ea441af8079a to your computer and use it in GitHub Desktop.
Zeller's congruence algorithm in Ruby
# day is Numeric
# month is String
# year us Numeric
def zeller(day,month,year)
days = ['Saturday','Sunday','Monday','Tuesday','Wednesday','Thursday','Friday']
case month
when 'January'
monthind = 13
year -=1
when 'February'
monthind = 14
year -=1
when 'March'
monthind = 3
when 'April'
monthind = 4
when 'May'
monthind = 5
when 'June'
monthind = 6
when 'July'
monthind = 7
when 'August'
monthind = 8
when 'September'
monthind = 9
when 'October'
monthind = 10
when 'November'
monthind = 11
when 'December'
monthind = 12
else
puts 'Invalid month'
end
monthind += 1
monthPart = (monthind*26)/10
yearPart = year + year/4
yearPart += (6*year/100)
yearPart += year/400
h = (day + monthPart + yearPart) % 7
days[h]
end
zeller(4,'January',2014) # "Saturday"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment