Sha256: 9272a0ef2913471a37be60e7a805900c0e26b514682ac0223df95e72612ff11b

Contents?: true

Size: 742 Bytes

Versions: 1

Compression:

Stored size: 742 Bytes

Contents

module SlideRule
  module DistanceCalculators
    class DayOfYear
      DAYS_IN_YEAR = 365

      def calculate(date_1, date_2, options={})
        date_1 = cleanse_date(date_1)
        date_2 = cleanse_date(date_2)
        threshold = options[:threshold] || DAYS_IN_YEAR
        days_apart = (date_1.mjd - date_2.mjd).abs
        
        return 1 if days_apart >= threshold

        distance = days_apart.to_f / threshold
        distance.round(2)
      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

1 entries across 1 versions & 1 rubygems

Version Path
slide_rule-1.0.0 lib/slide_rule/distance_calculators/day_of_year.rb