Advertisement
saasbook

date_calculator.rb

Jan 10th, 2012
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.91 KB | None | 0 0
  1. # This class calculates the current year given an origin day
  2. # supplied by a clock chip.
  3. #
  4. # Author:: Armando Fox
  5. # Copyright:: Copyright(C) 2011 by Armando Fox
  6. # License::  Distributed under the BSD License
  7. #
  8. class DateCalculator
  9.   #
  10.   #  Create a new DateCalculator initialized to the origin year
  11.   # * +origin_year+ - days will be calculated from Jan. 1 of this year
  12.   #
  13.   def initialize(origin_year)
  14.     @origin_year = origin_year
  15.   end
  16.   #
  17.   # Returns current year, given origin year and days since origin year
  18.   # * +days_since_origin+ - number of days elapsed since Jan. 1 of origin year
  19.   #
  20.   def current_year_from_days(days_since_origin, this_year = @origin_year)
  21.     if days_since_origin <= days_in_year(this_year)
  22.       return this_year
  23.     else
  24.       return current_year_from_days(days_since_origin - days_in_year(this_year),
  25.         this_year + 1)
  26.     end
  27.   end
  28.  
  29.   def days_in_year(year) # :nodoc:
  30.     365
  31.   end
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement