Skip to content

Commit

Permalink
error handling for datetime parse failures
Browse files Browse the repository at this point in the history
  • Loading branch information
alexduryee committed Mar 6, 2023
1 parent 0e515d1 commit 9b03bf1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/timetwister/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ def self.match_replace
if c[:proc]
# clone string to avoid changing it via in-place methods used in Procs
work_string = @string.clone
c[:proc].call(work_string, c[:arg])

# using a begin/rescue to catch dates that Date.parse errors on
begin
c[:proc].call(work_string, c[:arg])
rescue Date::Error
@dates = { :original_string => @string, :index_dates => [], :keydate => nil, :keydate_z => nil, :date_start => nil, :date_end => nil,
:date_start_full => nil, :date_end_full => nil, :inclusive_range => nil, :certainty => nil }
end
end
break
end
Expand Down
2 changes: 1 addition & 1 deletion lib/timetwister/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Timetwister
VERSION = "0.2.8"
VERSION = "0.2.9"
end
57 changes: 56 additions & 1 deletion spec/dates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
end

it "parses ranges of full dates" do

forms = ["July 4 1776 - March 1 1789", "4 July 1776 - 1 March 1789", "1776 July 4 - 1789 March 1", "1776 4 July - 1789 1 March", "1776 4 July to 1789 1 March"]
forms.each do |f|
date = Timetwister.parse(f)
Expand Down Expand Up @@ -572,6 +571,62 @@
end
end

it "DOES NOT parse nonexistent dates" do
date = Timetwister.parse('1776 September 31')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)

date = Timetwister.parse('1776-11-31')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)

date = Timetwister.parse('1776-11-00')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)

date = Timetwister.parse('1776 whatever 31')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)

date = Timetwister.parse('1776 sometime 72 - december 42 1999')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)

date = Timetwister.parse('just absolute garbage')
expect(date[0][:date_start]).to eq(nil)
expect(date[0][:date_start_full]).to eq(nil)
expect(date[0][:date_end]).to eq(nil)
expect(date[0][:date_end_full]).to eq(nil)
expect(date[0][:inclusive_range]).to eq(nil)
expect(date[0][:index_dates]).to eq([])
expect(date[0][:test_data]).to eq(nil)
end

it "parses lists of years" do
date = Timetwister.parse("1776, 1789, 1812")
expect(date[0][:date_start]).to eq("1776")
Expand Down

0 comments on commit 9b03bf1

Please sign in to comment.