Sha256: 388dfaf95a0e2bfb6318a87a1f930174c21182a8f512aa5bbd5319f369d55ac1
Contents?: true
Size: 907 Bytes
Versions: 3
Compression:
Stored size: 907 Bytes
Contents
module SlideRule module DistanceCalculators class DayOfMonth MAX_DAYS = 15 # # Calculates distance using 15 as the max point. # Does not take into account the number of days in the actual month being considered. # def calculate(first, second) first = cleanse_date(first) second = cleanse_date(second) difference_in_days(first, second).to_f / MAX_DAYS end def difference_in_days(first, second) distance = (first.mday - second.mday).abs return distance if distance <= MAX_DAYS MAX_DAYS - (distance - MAX_DAYS) end private def cleanse_date(date) date = Time.at(date).utc.to_date if date.is_a?(::Fixnum) date = Date.parse(date) unless date.is_a?(::Date) || date.is_a?(::Time) date = date.to_date if date.is_a?(::Time) date end end end end
Version data entries
3 entries across 3 versions & 1 rubygems