Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix time regex #323

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading