Skip to content

Instantly share code, notes, and snippets.

@gee-forr
Created June 27, 2011 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gee-forr/1048653 to your computer and use it in GitHub Desktop.
Save gee-forr/1048653 to your computer and use it in GitHub Desktop.
challenge_years_months.rb
#!/usr/bin/env ruby
# encoding: UTF-8
TIME_ZERO = Time.gm(0)
ages_to_test = [979000000, 2158493738, 246144023, 1270166272, 1025600095]
def seconds_to_dt (seconds = 0) # Give it a sane default
age = TIME_ZERO + seconds.to_i
end
def say_age (age = 0) # Give it a sane default
age_as_dt = seconds_to_dt(age)
years = age_as_dt.strftime('%Y') # May be more than 100, so trip leading 0's.
years.gsub!(/^0+/, '')
months = age_as_dt.strftime('%m')
months.gsub!(/^0/, '') # same as for year...
years_text = years == '1' ? 'year' : 'years'
months_text = months == '1' ? 'month' : 'months'
puts "I am #{years} #{years_text} and #{months} #{months_text} old"
end
ages_to_test.each do |age_in_seconds|
say_age age_in_seconds
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment