<commit_before>require "human_seconds/version"

class HumanSeconds
  attr_reader :seconds

  def initialize(seconds)
    @seconds = seconds
  end

  def self.parse(str)
    m = str.match(/(\d+d)?(\d+h)?(\d+m)?(\d+s)?/)

    if m[0].empty?
      raise ArgumentError, "Unparseable string #{str}"
    else
      seconds = (m[1].to_i * 86_400) + (m[2].to_i * 3600) +
        (m[3].to_i * 60) + m[4].to_i

      new(seconds)
    end
  end

  def to_i
    seconds
  end

  def to_s
    str = ""

    if ref_days != 0
      str << "#{ref_days}d"
    end

    if ref_hours != 0
      str << "#{ref_hours}h"
    end

    if ref_minutes != 0
      str << "#{ref_minutes}m"
    end

    if ref_seconds != 0
      str << "#{ref_seconds}s"
    end

    str
  end

  private

  def ref_time
    @ref_time ||= Time.at(0).utc + seconds
  end

  def ref_days
    seconds / 86_400
  end

  def ref_hours
    ref_time.hour
  end

  def ref_minutes
    ref_time.min
  end

  def ref_seconds<commit_msg>Shorten the #to_s method<commit_after>require "human_seconds/version"

class HumanSeconds
  attr_reader :seconds

  def initialize(seconds)
    @seconds = seconds
  end

  def self.parse(str)
    m = str.match(/(\d+d)?(\d+h)?(\d+m)?(\d+s)?/)

    if m[0].empty?
      raise ArgumentError, "Unparseable string #{str}"
    else
      seconds = (m[1].to_i * 86_400) + (m[2].to_i * 3600) +
        (m[3].to_i * 60) + m[4].to_i

      new(seconds)
    end
  end

  def to_i
    seconds
  end

  def to_s
    [
      "#{ref_days}d",
      "#{ref_hours}h",
      "#{ref_minutes}m",
      "#{ref_seconds}s",
    ].reject { |t| t.start_with?('0') }.join
  end

  private

  def ref_time
    @ref_time ||= Time.at(0).utc + seconds
  end

  def ref_days
    seconds / 86_400
  end

  def ref_hours
    ref_time.hour
  end

  def ref_minutes
    ref_time.min
  end

  def ref_seconds
{'fraction_non_alphanumeric': 0.13815434430652995, 'fraction_numerical': 0.02644360496492175, 'mean_word_length': 2.229965156794425, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, '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': '4852002', 'n_tokens_mistral': 794, 'n_tokens_neox': 728, 'n_words': 162}