2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module TimeTrackingFormatter
|
|
|
|
extend self
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
# 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
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def parse(string)
|
|
|
|
string = string.sub(/\A-/, '')
|
|
|
|
|
|
|
|
seconds =
|
|
|
|
begin
|
|
|
|
ChronicDuration.parse(
|
|
|
|
string,
|
|
|
|
CUSTOM_DAY_AND_MONTH_LENGTH.merge(default_unit: 'hours'))
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2019-12-04 20:38:33 +05:30
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
seconds *= -1 if seconds && Regexp.last_match
|
|
|
|
seconds
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def output(seconds)
|
2021-09-04 01:27:46 +05:30
|
|
|
seconds.to_i < 0 ? negative_output(seconds) : positive_output(seconds)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def positive_output(seconds)
|
2019-12-04 20:38:33 +05:30
|
|
|
ChronicDuration.output(
|
|
|
|
seconds,
|
|
|
|
CUSTOM_DAY_AND_MONTH_LENGTH.merge(
|
|
|
|
format: :short,
|
|
|
|
limit_to_hours: limit_to_hours_setting,
|
|
|
|
weeks: true))
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError
|
2019-12-04 20:38:33 +05:30
|
|
|
nil
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def negative_output(seconds)
|
|
|
|
"-" + positive_output(seconds.abs)
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
|
|
|
def limit_to_hours_setting
|
|
|
|
Gitlab::CurrentSettings.time_tracking_limit_to_hours
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|