debian-mirror-gitlab/lib/gitlab/time_tracking_formatter.rb

53 lines
1.2 KiB
Ruby
Raw Normal View History

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
2023-04-23 21:23:45 +05:30
def parse(string, keep_zero: false)
2022-06-21 17:19:12 +05:30
negative_time = string.start_with?('-')
string = string.delete_prefix('-')
2019-12-04 20:38:33 +05:30
seconds =
begin
ChronicDuration.parse(
string,
2023-04-23 21:23:45 +05:30
CUSTOM_DAY_AND_MONTH_LENGTH.merge(default_unit: 'hours', keep_zero: keep_zero))
2021-06-08 01:23:25 +05:30
rescue StandardError
2019-12-04 20:38:33 +05:30
nil
end
2022-06-21 17:19:12 +05:30
seconds *= -1 if seconds && negative_time
2019-12-04 20:38:33 +05:30
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