diff --git a/Gemfile.lock b/Gemfile.lock index 041b9a6..e608554 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -205,4 +205,4 @@ DEPENDENCIES yard BUNDLED WITH - 2.4.3 + 2.4.19 diff --git a/lib/ruby_units/unit.rb b/lib/ruby_units/unit.rb index 40e183f..9391e97 100644 --- a/lib/ruby_units/unit.rb +++ b/lib/ruby_units/unit.rb @@ -68,7 +68,7 @@ class << self STONE_LB_UNIT_REGEX = /(?:sts?|stones?)+[\s,]*(\d+)\s*(?:#|lbs?|pounds?|pound-mass)*/.freeze STONE_LB_REGEX = /(\d+)\s*#{STONE_LB_UNIT_REGEX}/.freeze # Time formats: 12:34:56,78, (hh:mm:ss,msec) etc. - TIME_REGEX = /(?\d+):(?\d+):(?:(?\d+))?(?:,(?\d+))?/.freeze + TIME_REGEX = /(?\d+):(?\d+):?(?:(?\d+))?(?:,(?\d+))?/.freeze # Scientific notation: 1, -1, +1, 1.2, +1.2, -1.2, 123.4E5, +123.4e5, # -123.4E+5, -123.4e-5, etc. SCI_NUMBER = /([+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*)/.freeze @@ -1600,7 +1600,6 @@ def parse(passed_unit_string = '0') end # ... and then strip the remaining brackets for x*y*z unit_string.gsub!(/[<>]/, '') - if unit_string =~ TIME_REGEX hours, minutes, seconds, microseconds = unit_string.scan(TIME_REGEX)[0] raise ArgumentError, 'Invalid Duration' if [hours, minutes, seconds, microseconds].all?(&:nil?) diff --git a/spec/ruby_units/unit_spec.rb b/spec/ruby_units/unit_spec.rb index b90f019..9f01b4f 100644 --- a/spec/ruby_units/unit_spec.rb +++ b/spec/ruby_units/unit_spec.rb @@ -554,6 +554,80 @@ end # Time + describe RubyUnits::Unit.new("1:00") do + it { is_expected.to be_an_instance_of Unit } + + describe '#scalar' do + subject { super().scalar } + + it { is_expected.to be_a(Numeric) } + it { is_expected.to be === 1 } + end + + describe '#units' do + subject { super().units } + + it { is_expected.to eq('h') } + end + + it { is_expected.not_to be_temperature } + it { is_expected.not_to be_degree } + it { is_expected.not_to be_base } + it { is_expected.not_to be_unitless } + it { is_expected.not_to be_zero } + end + + describe RubyUnits::Unit.new("1:23") do + it { is_expected.to be_an_instance_of Unit } + + describe '#scalar' do + subject { super().scalar } + + it { is_expected.to be_a(Numeric) } + it { is_expected.to be === 83/60r } + end + + describe '#units' do + subject { super().units } + + it { is_expected.to eq('h') } + end + end + + describe RubyUnits::Unit.new("1:23:45") do + it { is_expected.to be_an_instance_of Unit } + + describe '#scalar' do + subject { super().scalar } + + it { is_expected.to be_a(Numeric) } + it { is_expected.to be === 67/48r } + end + + describe '#units' do + subject { super().units } + + it { is_expected.to eq('h') } + end + end + + describe RubyUnits::Unit.new("1:23:45,67") do + it { is_expected.to be_an_instance_of Unit } + + describe '#scalar' do + subject { super().scalar } + + it { is_expected.to be_a(Numeric) } + it { is_expected.to be === 5025000067/3600000000r } + end + + describe '#units' do + subject { super().units } + + it { is_expected.to eq('h') } + end + end + describe RubyUnits::Unit.new(Time.now) do it { is_expected.to be_an_instance_of Unit }