Module: Ruby::Pomodoro::TimeConverter

Defined in:
lib/ruby/pomodoro/time_converter.rb

Class Method Summary collapse

Class Method Details

.to_format_string(seconds) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/ruby/pomodoro/time_converter.rb', line 5

def to_format_string(seconds)
  days = seconds / 60 / 60 / 24
  hours = (seconds - days * 24 * 60 * 60) / 60 / 60
  minutes = (seconds - (hours * 60 * 60) - (days * 24 * 60 * 60)) / 60
  {d: days, h: hours, m: minutes}.select { |_k, v| v.positive? }.each.with_object(String.new) do |item, acc|
    acc << item.reverse.join(":") + " "
  end.strip
end

.to_seconds(format_string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby/pomodoro/time_converter.rb', line 14

def to_seconds(format_string)
  return 0 if format_string.nil?

  match = format_string.split(" ").compact.map do |item|
    if item
      item.split(":").reverse.then { |res| res.size == 2 ? res : nil }
    end
    end.compact.to_h.select { |k| %w[m h d].include?(k) }
  (match["d"].to_i * 24 * 60 * 60) + (match["h"].to_i * 60 * 60) + (match["m"].to_i * 60)
end