2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-09-01 02:07:24 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2008-04-26 11:59:51 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-09-01 02:07:24 +00:00
|
|
|
#
|
2008-04-26 11:59:51 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-09-01 02:07:24 +00:00
|
|
|
#
|
2008-04-26 11:59:51 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
2018-06-08 00:55:41 +00:00
|
|
|
module Redmine
|
|
|
|
|
# @private
|
2021-10-27 22:27:03 +00:00
|
|
|
module CoreExt
|
2018-06-08 00:55:41 +00:00
|
|
|
# @private
|
|
|
|
|
module String
|
2008-04-26 11:59:51 +00:00
|
|
|
# Custom string conversions
|
2018-06-08 00:55:41 +00:00
|
|
|
# @private
|
2008-04-26 11:59:51 +00:00
|
|
|
module Conversions
|
|
|
|
|
# Parses hours format and returns a float
|
|
|
|
|
def to_hours
|
|
|
|
|
s = self.dup
|
|
|
|
|
s.strip!
|
2009-01-09 17:32:46 +00:00
|
|
|
if s =~ %r{^(\d+([.,]\d+)?)h?$}
|
|
|
|
|
s = $1
|
|
|
|
|
else
|
2008-04-26 11:59:51 +00:00
|
|
|
# 2:30 => 2.5
|
2020-11-15 12:37:17 +00:00
|
|
|
s.gsub!(%r{^(\d+):(\d+)$}) {$1.to_i + $2.to_i / 60.0}
|
2008-04-26 11:59:51 +00:00
|
|
|
# 2h30, 2h, 30m => 2.5, 2, 0.5
|
2020-11-15 12:37:17 +00:00
|
|
|
s.gsub!(%r{^((\d+)\s*(h|hours?))?\s*((\d+)\s*(m|min)?)?$}i) {|m| ($1 || $4) ? ($2.to_i + $5.to_i / 60.0) : m[0]}
|
2008-04-26 11:59:51 +00:00
|
|
|
end
|
|
|
|
|
# 2,5 => 2.5
|
2020-10-24 06:46:50 +00:00
|
|
|
s.tr!(',', '.')
|
2025-04-11 09:00:58 +00:00
|
|
|
Kernel.Float(s, exception: false)
|
2008-04-26 11:59:51 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|