Skip to content

Commit

Permalink
Fix time regex
Browse files Browse the repository at this point in the history
  • Loading branch information
olbrich committed Sep 25, 2023
1 parent 8f3b2cd commit ea6a147
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ DEPENDENCIES
yard

BUNDLED WITH
2.4.3
2.4.19
3 changes: 1 addition & 2 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /(?<hour>\d+):(?<min>\d+):(?:(?<sec>\d+))?(?:,(?<msec>\d+))?/.freeze
TIME_REGEX = /(?<hour>\d+):(?<min>\d+):?(?:(?<sec>\d+))?(?:,(?<msec>\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
Expand Down Expand Up @@ -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?)
Expand Down
74 changes: 74 additions & 0 deletions spec/ruby_units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit ea6a147

Please sign in to comment.