<commit_before>module Moments
  # Calculates differences between two given Time instances.
  class Difference
    # == Parameters:
    # from::
    #   A instance of Time
    # to::
    #   A instance of Time
    def initialize(from, to)
      @from = from
      @to   = to

      precise_difference
    end

    def to_hash
      @diff
    end

    def future?
      @from < @to
    end

    def same?
      @from == @to
    end

    def past?
      @from > @to
    end

    def precise_difference
      to   = @to
      from = @from

      if from > to
        to   = @from
        from = @to
      end

      @diff = {
        seconds: to.sec - from.sec,
        minutes: to.min - from.min,
        hours:   to.hour - from.hour,
        days:    to.day - from.day,
        months:  to.month - from.month,
        years:   to.year - from.year
      }

      calculate :seconds, :minutes
      calculate :minutes, :hours
      calculate :hours, :days, 24
      calculate_days
      calculate :months, :years, 12

      @diff
    end

    private :precise_difference

    def calculate(attribute, difference, stepping = 60)
      return if @diff[attribute] >= 0

      @diff[attribute] += stepping
      @diff[difference] -= 1
    end

    private :calculate

    def calculate_days
      return if @diff[:days] >= 0

      previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
      @diff[:days]        = precise_previous_month_days(@diff[:days], previous_month_days, @from.day)
      @diff[:months] -= 1
    end

    private :calculate_days

    def precise_previous_month_days(days, previous, from)
      if previous < from
        days = previous + days + (from - previous)
      else
        days = previous + days
      end

      days
    end

    private :precise_previous_month_days
  end
end<commit_msg>Code refactoring<commit_after>module Moments
  # Calculates differences between two given Time instances.
  class Difference
    # == Parameters:
    # from::
    #   A instance of Time
    # to::
    #   A instance of Time
    def initialize(from, to)
      @from = from
      @to   = to
      @diff = {}

      precise_difference
    end

    def to_hash
      @diff
    end

    def future?
      @from < @to
    end

    def same?
      @from == @to
    end

    def past?
      @from > @to
    end

    def precise_difference
      from, to = ordered_time

      @diff = {
        seconds: to.sec - from.sec,
        minutes: to.min - from.min,
        hours:   to.hour - from.hour,
        days:    to.day - from.day,
        months:  to.month - from.month,
        years:   to.year - from.year
      }

      calculate :seconds, :minutes
      calculate :minutes, :hours
      calculate :hours, :days, 24
      calculate_days
      calculate :months, :years, 12

      @diff
    end

    private :precise_difference

    def ordered_time
      to   = @to
      from = @from

      if from > to
        to   = @from
        from = @to
      end

      [from, to]
    end

    private :ordered_time

    def calculate(attribute, difference, stepping = 60)
      return if @diff[attribute] >= 0

      @diff[attribute] += stepping
      @diff[difference] -= 1
    end

    private :calculate

    def calculate_days
      return if @diff[:days] >= 0

      previous_month_days = (Time.new(@to.year, @to.month, 1) - 1).day
      @diff[:days]        = precise_previous_month_days(@diff[:days], previous_month_days, @from.day)
      @diff[:months] -= 1
    end

    private :calculate_days

    def precise_previous_month_days(days, previous, from)
      if previous < from
        previous + days + (from - previous)
      else
        previous + days
      end
    end

    private :precise_previous_month_days
  end
end
{'fraction_non_alphanumeric': 0.0944755804643715, 'fraction_numerical': 0.006405124099279423, 'mean_word_length': 1.80749063670412, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3917752', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1146, 'n_words': 330}