Sha256: 5e1fdc16a0f9e72e168285f1b35154c81bdaaa0ff94f887bc40f21d2d2a9d9f5

Contents?: true

Size: 608 Bytes

Versions: 5

Compression:

Stored size: 608 Bytes

Contents

module SlideRule
  module DistanceCalculators
    class DayOfYear
      DAYS_IN_YEAR = 365

      def calculate(date_1, date_2)
        date_1 = cleanse_date(date_1)
        date_2 = cleanse_date(date_2)

        days_apart = (date_1.mjd - date_2.mjd).abs

        return 1 if days_apart >= DAYS_IN_YEAR

        distance = days_apart.to_f / DAYS_IN_YEAR
        distance.round(2)
      end

      private

      def cleanse_date(date)
        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

5 entries across 5 versions & 1 rubygems

Version Path
slide_rule-0.2.0 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.0.4 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.1.0 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.0.3 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.0.2 lib/slide_rule/distance_calculators/day_of_year.rb