Skip to content

Instantly share code, notes, and snippets.

@mazzo46
Created December 6, 2012 02:16
Show Gist options
  • Save mazzo46/4221314 to your computer and use it in GitHub Desktop.
Save mazzo46/4221314 to your computer and use it in GitHub Desktop.
Calculates the number of days from the day on which the Gregorian Calendar starts.
# Calculates the number of days from the day on which
# the Gregorian Calendar starts -- the zero day, Octorber 15th, 1582.
def calc_gregorian_long_date(day, month, year)
# Note:
# This method provides no parameter validation.
# Parameters must be AFTER the day on which the Gregorian Calendar starts
# -- the zero day, Octorber 15th, 1582.
zero_date = {"day"=>15, "month"=>10, "year"=>1582}
days_of_month = [0,31,28,31,30,31,30,31,31,30,31,30,31]
days_by_years = 0
for i in zero_date["year"]..(year-1)
if(((i%4==0) and (i%100!=0)) or (i%400==0))
days_by_years += 366
else
days_by_years += 365
end
end
days_in_the_year = day
for i in 1..(month-1)
days_in_the_year += days_of_month[i]
if(i==2 and (((year%4==0) and (year%100!=0)) or (year%400==0)))
days_in_the_year += 1
end
end
days_in_zero_year = zero_date["day"]
for i in 1..(zero_date["month"]-1)
days_in_zero_year += days_of_month[i]
if(i==2 and (((zero_date["year"]%4==0) and (zero_date["year"]%100!=0)) \
or (zero_date["year"]%400==0)))
days_in_zero_year += 1
end
end
return days_by_years + days_in_the_year - days_in_zero_year
end
# test
puts calc_gregorian_long_date(21,12,2012)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment