Created
October 3, 2012 08:10
-
-
Save BrianJoyce/3825744 to your computer and use it in GitHub Desktop.
Birthdate stuff ... pet hate
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
| class Birthday | |
| def initalize | |
| @days = 0 | |
| @months = 0 | |
| end | |
| def days_in_year(year) | |
| leap_year?(year) ? 366 : 365 | |
| end | |
| def leap_year?(year) | |
| year % 400 == 0 || year % 100 == 0 || year % 4 == 0 ? true : false | |
| end | |
| def days_in_month(year) | |
| if leap_year?(year) | |
| days_in_month = {1 => 31, 2 => 29, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11=> 30, 12 => 31} | |
| else | |
| days_in_month = {1 => 31, 2 => 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11=> 30, 12 => 31} | |
| end | |
| end | |
| def days_diff(start_date, end_date) | |
| start_date = times(start_date) | |
| end_date = times(end_date) | |
| @days = ((end_date - start_date) / 60 / 60 / 24).floor | |
| puts "days diff days #{@days}" | |
| end | |
| def times(the_date) | |
| m,d,y = the_date.split('/') | |
| Time.new(y,m,d) | |
| end | |
| def add_years(start_date) | |
| @total_years = 0 | |
| current_year = times(start_date).year.to_i | |
| # puts "first year #{current_year}" | |
| # puts "add years days TOP #{@days}" | |
| # puts "add years current year TOP #{current_year}" | |
| until @days < days_in_year(current_year) do | |
| @days = @days - days_in_year(current_year) | |
| current_year = current_year + 1 | |
| # puts current_year | |
| @total_years += 1 | |
| end | |
| puts "add years days END #{@days}" | |
| puts "Total years END: #{@total_years.inspect}" | |
| end | |
| end | |
| myday = Birthday.new | |
| myday.days_diff("04/03/1941", "02/10/2012") | |
| puts myday.add_years("04/03/1941") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment