Welcome to mirror list, hosted at ThFree Co, Russian Federation.

time_helper.rb « helpers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0a5751c5221164f8e4938ae759d32c366323cb67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module TimeHelper
  TIME_UNIT_TRANSLATION = {
    seconds: ->(seconds) { n_('%d second', '%d seconds', seconds) % seconds },
    minutes: ->(minutes) { n_('%d minute', '%d minutes', minutes) % minutes },
    hours: ->(hours) { n_('%d hour', '%d hours', hours) % hours },
    days: ->(days) { n_('%d day', '%d days', days) % days },
    weeks: ->(weeks) { n_('%d week', '%d weeks', weeks) % weeks },
    months: ->(months) { n_('%d month', '%d months', months) % months },
    years: ->(years) { n_('%d year', '%d years', years) % years }
  }.freeze

  def time_interval_in_words(interval_in_seconds)
    time_parts = ActiveSupport::Duration.build(interval_in_seconds.to_i).parts

    time_parts.map { |unit, value| TIME_UNIT_TRANSLATION[unit].call(value) }.to_sentence
  end

  def duration_in_numbers(duration_in_seconds)
    seconds = duration_in_seconds % 1.minute
    minutes = (duration_in_seconds / 1.minute) % (1.hour / 1.minute)
    hours = duration_in_seconds / 1.hour

    if hours == 0
      "%02d:%02d" % [minutes, seconds]
    else
      "%02d:%02d:%02d" % [hours, minutes, seconds]
    end
  end

  def time_in_milliseconds
    (Time.now.to_f * 1000).to_i
  end
end