time_tracking_formatter.rb 936 字节
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5 6 7 8
module Gitlab
  module TimeTrackingFormatter
    extend self

    def parse(string)
      with_custom_config do
9 10 11 12 13
        string.sub!(/\A-/, '')

        seconds = ChronicDuration.parse(string, default_unit: 'hours') rescue nil
        seconds *= -1 if seconds && Regexp.last_match
        seconds
14 15 16 17 18
      end
    end

    def output(seconds)
      with_custom_config do
J
Jon Kolb 已提交
19 20
        limit_to_hours = Gitlab::CurrentSettings.time_tracking_display_hours_only
        ChronicDuration.output(seconds, format: :short, limit_to_hours: limit_to_hours, weeks: true) rescue nil
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      end
    end

    def with_custom_config
      # We may want to configure it through project settings in a future version.
      ChronicDuration.hours_per_day = 8
      ChronicDuration.days_per_week = 5

      result = yield

      ChronicDuration.hours_per_day = 24
      ChronicDuration.days_per_week = 7

      result
    end
  end
end