Skip to content

Commit ea6a147

Browse files
committed
Fix time regex
1 parent 8f3b2cd commit ea6a147

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,4 @@ DEPENDENCIES
205205
yard
206206

207207
BUNDLED WITH
208-
2.4.3
208+
2.4.19

lib/ruby_units/unit.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class << self
6868
STONE_LB_UNIT_REGEX = /(?:sts?|stones?)+[\s,]*(\d+)\s*(?:#|lbs?|pounds?|pound-mass)*/.freeze
6969
STONE_LB_REGEX = /(\d+)\s*#{STONE_LB_UNIT_REGEX}/.freeze
7070
# Time formats: 12:34:56,78, (hh:mm:ss,msec) etc.
71-
TIME_REGEX = /(?<hour>\d+):(?<min>\d+):(?:(?<sec>\d+))?(?:,(?<msec>\d+))?/.freeze
71+
TIME_REGEX = /(?<hour>\d+):(?<min>\d+):?(?:(?<sec>\d+))?(?:,(?<msec>\d+))?/.freeze
7272
# Scientific notation: 1, -1, +1, 1.2, +1.2, -1.2, 123.4E5, +123.4e5,
7373
# -123.4E+5, -123.4e-5, etc.
7474
SCI_NUMBER = /([+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*)/.freeze
@@ -1600,7 +1600,6 @@ def parse(passed_unit_string = '0')
16001600
end
16011601
# ... and then strip the remaining brackets for x*y*z
16021602
unit_string.gsub!(/[<>]/, '')
1603-
16041603
if unit_string =~ TIME_REGEX
16051604
hours, minutes, seconds, microseconds = unit_string.scan(TIME_REGEX)[0]
16061605
raise ArgumentError, 'Invalid Duration' if [hours, minutes, seconds, microseconds].all?(&:nil?)

spec/ruby_units/unit_spec.rb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,80 @@
554554
end
555555

556556
# Time
557+
describe RubyUnits::Unit.new("1:00") do
558+
it { is_expected.to be_an_instance_of Unit }
559+
560+
describe '#scalar' do
561+
subject { super().scalar }
562+
563+
it { is_expected.to be_a(Numeric) }
564+
it { is_expected.to be === 1 }
565+
end
566+
567+
describe '#units' do
568+
subject { super().units }
569+
570+
it { is_expected.to eq('h') }
571+
end
572+
573+
it { is_expected.not_to be_temperature }
574+
it { is_expected.not_to be_degree }
575+
it { is_expected.not_to be_base }
576+
it { is_expected.not_to be_unitless }
577+
it { is_expected.not_to be_zero }
578+
end
579+
580+
describe RubyUnits::Unit.new("1:23") do
581+
it { is_expected.to be_an_instance_of Unit }
582+
583+
describe '#scalar' do
584+
subject { super().scalar }
585+
586+
it { is_expected.to be_a(Numeric) }
587+
it { is_expected.to be === 83/60r }
588+
end
589+
590+
describe '#units' do
591+
subject { super().units }
592+
593+
it { is_expected.to eq('h') }
594+
end
595+
end
596+
597+
describe RubyUnits::Unit.new("1:23:45") do
598+
it { is_expected.to be_an_instance_of Unit }
599+
600+
describe '#scalar' do
601+
subject { super().scalar }
602+
603+
it { is_expected.to be_a(Numeric) }
604+
it { is_expected.to be === 67/48r }
605+
end
606+
607+
describe '#units' do
608+
subject { super().units }
609+
610+
it { is_expected.to eq('h') }
611+
end
612+
end
613+
614+
describe RubyUnits::Unit.new("1:23:45,67") do
615+
it { is_expected.to be_an_instance_of Unit }
616+
617+
describe '#scalar' do
618+
subject { super().scalar }
619+
620+
it { is_expected.to be_a(Numeric) }
621+
it { is_expected.to be === 5025000067/3600000000r }
622+
end
623+
624+
describe '#units' do
625+
subject { super().units }
626+
627+
it { is_expected.to eq('h') }
628+
end
629+
end
630+
557631
describe RubyUnits::Unit.new(Time.now) do
558632
it { is_expected.to be_an_instance_of Unit }
559633

0 commit comments

Comments
 (0)