Created
October 3, 2012 22:55
-
-
Save arn-e/3830419 to your computer and use it in GitHub Desktop.
Days Remaining In Year
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description : Script to Return Remaining Days in the Year | |
# User puts in a date and gets the number of the day (e.g. day 1) and the number of days left in the year | |
class DayOfYear | |
def initialize | |
@days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31] | |
end | |
def date_info(date) | |
return "days number : #{days_elapsed(date.month,date.day,date.year)}, days remaining in year : #{days_remaining(date.month,date.day,date.year)}" | |
end | |
def days_elapsed(month, day, year, elapsed = 0) | |
month != 1 ? ((month - 1).downto(1) {|mon| elapsed += @days_in_months[mon]}) : elapsed | |
elapsed += 1 if is_leap?(year) && month > 2 | |
return elapsed += day | |
end | |
def days_remaining(month,day,year) | |
is_leap?(year) ? (return 366 - days_elapsed(month,day,year)) : (return 365 - days_elapsed(month,day,year)) | |
end | |
def is_leap?(year) | |
return true if (year % 400 == 0 || year % 100 == 0 || year % 4 == 0) | |
end | |
end | |
get_day = DayOfYear.new | |
puts get_day.date_info(Time.new(2011,03,01)) | |
puts get_day.date_info(Time.new(2012,03,01)) | |
puts get_day.date_info(Time.new(2011,02,28)) | |
puts get_day.date_info(Time.new(2012,02,28)) | |
puts get_day.date_info(Time.new(2012,01,01)) | |
puts get_day.date_info(Time.new(2011,01,01)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment