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

time_tracking_formatter.rb « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67ecf498cf7bc245d99890ad9cf560b94441f3f5 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module Gitlab
  module TimeTrackingFormatter
    extend self

    # We may want to configure it through project settings in a future version.
    CUSTOM_DAY_AND_MONTH_LENGTH = { hours_per_day: 8, days_per_month: 20 }.freeze

    def parse(string)
      string = string.sub(/\A-/, '')

      seconds =
        begin
          ChronicDuration.parse(
            string,
            CUSTOM_DAY_AND_MONTH_LENGTH.merge(default_unit: 'hours'))
        rescue StandardError
          nil
        end

      seconds *= -1 if seconds && Regexp.last_match
      seconds
    end

    def output(seconds)
      seconds.to_i < 0 ? negative_output(seconds) : positive_output(seconds)
    end

    private

    def positive_output(seconds)
      ChronicDuration.output(
        seconds,
        CUSTOM_DAY_AND_MONTH_LENGTH.merge(
          format: :short,
          limit_to_hours: limit_to_hours_setting,
          weeks: true))
    rescue StandardError
      nil
    end

    def negative_output(seconds)
      "-" + positive_output(seconds.abs)
    end

    def limit_to_hours_setting
      Gitlab::CurrentSettings.time_tracking_limit_to_hours
    end
  end
end