2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-23 00:44:02 -05:00
|
|
|
module Gitlab
|
|
|
|
module TimeTrackingFormatter
|
|
|
|
extend self
|
|
|
|
|
2019-09-11 14:32:24 -04:00
|
|
|
# We may want to configure it through project settings in a future version.
|
2019-09-16 08:06:26 -04:00
|
|
|
CUSTOM_DAY_AND_MONTH_LENGTH = { hours_per_day: 8, days_per_month: 20 }.freeze
|
2017-01-18 11:48:16 -05:00
|
|
|
|
2019-09-11 14:32:24 -04:00
|
|
|
def parse(string)
|
|
|
|
string = string.sub(/\A-/, '')
|
|
|
|
|
|
|
|
seconds =
|
|
|
|
begin
|
|
|
|
ChronicDuration.parse(
|
|
|
|
string,
|
2019-09-16 08:06:26 -04:00
|
|
|
CUSTOM_DAY_AND_MONTH_LENGTH.merge(default_unit: 'hours'))
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2019-09-11 14:32:24 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
seconds *= -1 if seconds && Regexp.last_match
|
|
|
|
seconds
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def output(seconds)
|
2021-06-11 05:09:58 -04:00
|
|
|
seconds.to_i < 0 ? negative_output(seconds) : positive_output(seconds)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def positive_output(seconds)
|
2019-09-11 14:32:24 -04:00
|
|
|
ChronicDuration.output(
|
|
|
|
seconds,
|
2019-09-16 08:06:26 -04:00
|
|
|
CUSTOM_DAY_AND_MONTH_LENGTH.merge(
|
2019-09-11 14:32:24 -04:00
|
|
|
format: :short,
|
|
|
|
limit_to_hours: limit_to_hours_setting,
|
|
|
|
weeks: true))
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2019-09-11 14:32:24 -04:00
|
|
|
nil
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
2021-06-11 05:09:58 -04:00
|
|
|
def negative_output(seconds)
|
|
|
|
"-" + positive_output(seconds.abs)
|
|
|
|
end
|
2019-06-11 06:40:01 -04:00
|
|
|
|
|
|
|
def limit_to_hours_setting
|
|
|
|
Gitlab::CurrentSettings.time_tracking_limit_to_hours
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
end
|