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'))
|
2019-09-11 14:32:24 -04:00
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
seconds *= -1 if seconds && Regexp.last_match
|
|
|
|
seconds
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def 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))
|
|
|
|
rescue
|
|
|
|
nil
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
|
2019-06-11 06:40:01 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def limit_to_hours_setting
|
|
|
|
Gitlab::CurrentSettings.time_tracking_limit_to_hours
|
|
|
|
end
|
2016-12-23 00:44:02 -05:00
|
|
|
end
|
|
|
|
end
|