Sha256: bf38793825187157c7355c515c1e1ddda6afad464057b840c138d0820383011c

Contents?: true

Size: 673 Bytes

Versions: 4

Compression:

Stored size: 673 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 = 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

4 entries across 4 versions & 1 rubygems

Version Path
slide_rule-0.2.2 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.2.1 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.1.2 lib/slide_rule/distance_calculators/day_of_year.rb
slide_rule-0.1.1 lib/slide_rule/distance_calculators/day_of_year.rb