Skip to content

Commit

Permalink
removing chronic dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alexduryee committed Oct 6, 2021
1 parent 20268f0 commit fcf37a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Timetwister

A Chronic wrapper with improved date format parsing and fewer surprises.
A DateTime wrapper with improved date format parsing and fewer surprises.

Developed by the New York Public Library to transform freetext date metadata into machine-readable data.

Expand Down
52 changes: 15 additions & 37 deletions lib/timetwister/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

require 'chronic'
require 'date'
require 'timetwister/utilities'

class Parser
Expand Down Expand Up @@ -574,18 +574,7 @@ def self.proc_month_year_single
proc = Proc.new do |string|
string.gsub!(/\?/,'')

# Chronic can't parse year-month strings properly
# so we need to change them to month-year before
# parsing them.

if string.match(/^[0-9]/)
tmpyear = string.split(' ')[0]
string.gsub!(/^.+? /,'')
string << " "
string << tmpyear
end

datetime = Chronic.parse(string)
datetime = DateTime.parse(string)
if datetime
@dates[:date_start] = datetime.strftime('%Y-%m')
@dates[:index_dates] << datetime.strftime('%Y').to_i
Expand Down Expand Up @@ -614,14 +603,14 @@ def self.proc_full_date_single_range
# if month-specific, modify datetimes to include all days of each month
if specificity == 'month'
month_date_start = datetime_start.strftime('%Y-%m')
datetime_start = Chronic.parse(month_date_start + '-01')
datetime_start = DateTime.parse(month_date_start + '-01')
month_date_end = datetime_end.strftime('%Y-%m')
month_date_end_parts = month_date_end.split('-')

month_date_end_last = Utilities.days_in_month(month_date_end_parts[1],month_date_end_parts[0]).to_s
month_date_full = month_date_end + "-#{month_date_end_last}"

datetime_end = Chronic.parse(month_date_full)
datetime_end = DateTime.parse(month_date_full)
end

if datetime_start && datetime_end
Expand Down Expand Up @@ -699,14 +688,12 @@ def self.proc_single_year_month_range
first_month = string.match(@regex_tokens[:named_month]).to_s
last_month = string.match(@regex_tokens[:named_month] + '$').to_s

# chronic is fiddly about short months with periods
# (e.g. "may.") so we remove them
date_string_first = first_month.delete('.') + ' 1,' + year
datetime_first = Chronic.parse(date_string_first)
date_string_first = first_month + ' 1,' + year
datetime_first = DateTime.parse(date_string_first)
if !last_month.empty?
@dates[:date_start] = datetime_first.strftime('%Y-%m')
date_string_last = last_month + ' ' + year
datetime_last = Chronic.parse(date_string_last)
datetime_last = DateTime.parse(date_string_last)
@dates[:date_end] = datetime_last.strftime('%Y-%m')
end
@dates[:inclusive_range] = true
Expand All @@ -729,7 +716,6 @@ def self.proc_year_range_single_date
end

dates.each { |d| d.strip! }

if dates.length == 2
if dates[0].match(/[A-Za-z]/)
datetime_start = full_date_single_to_datetime(dates[0] + "-01")
Expand Down Expand Up @@ -773,20 +759,12 @@ def self.proc_year_with_dates
d << " "
d << year
end

# Chronic seemed to choke with YYYY-MM-DD dates
# so we'll flip it to MM-DD-YYYY
if d.match("^" + year)
d.gsub!(year + " ","")
d << " "
d << year
end
}

# change our strings to datetime objects
# and send them to be processed elsewhere
datetime_start = Chronic.parse(dates[0])
datetime_end = Chronic.parse(dates[1])
datetime_start = DateTime.parse(dates[0])
datetime_end = DateTime.parse(dates[1])
process_date_range(datetime_start, datetime_end)
@dates[:inclusive_range] = true
end
Expand Down Expand Up @@ -818,7 +796,7 @@ def self.proc_full_with_year_month
month_date_end_last = Utilities.days_in_month(month_date_end_parts[1],month_date_end_parts[0]).to_s
month_date_full = month_date_end + "-#{month_date_end_last}"

datetime_end = Chronic.parse(month_date_full)
datetime_end = DateTime.parse(month_date_full)
else
datetime_start = full_date_single_to_datetime(dates[0] + "-01")
if datetime_start && datetime_end
Expand Down Expand Up @@ -870,13 +848,13 @@ def self.full_date_single_to_datetime(string)
day = nil
end

new_string.gsub!(/[\.\,\s]+/,'')
new_string.gsub!(/[\.\,\s\-]+/,'')

month = new_string.clone
parse_string = month
parse_string += day ? " #{day}, #{year}" : " #{year}"
end
datetime = Chronic.parse(parse_string)
datetime = DateTime.parse(parse_string)
end


Expand Down Expand Up @@ -926,11 +904,11 @@ def self.iso8601_string_format(iso_8601_date)
# generates datetime from ISO8601-formatted date
def self.iso8601_datetime(iso_8601_date)
if iso_8601_date.match(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/)
Chronic.parse(iso_8601_date)
DateTime.parse(iso_8601_date)
elsif iso_8601_date.match(/^[0-9]{4}\-[0-9]{2}$/)
Chronic.parse(iso_8601_date + '-01')
DateTime.parse(iso_8601_date + '-01')
else
Chronic.parse(iso_8601_date + '-01-01')
DateTime.parse(iso_8601_date + '-01-01')
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/dates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
end

it "parses month ranges within one year" do
forms = ["July-September 1776", "1776 July-September"]
forms = ["July-September 1776", "1776 July-September", "1776 Jul.-Sept."]
forms.each do |f|
date = Timetwister.parse(f)
expect(date[0][:date_start]).to eq("1776-07")
Expand Down
3 changes: 1 addition & 2 deletions timetwister.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
spec.version = Timetwister::VERSION
spec.authors = ["Alex Duryee"]
spec.email = ["[email protected]"]
spec.summary = "Chronic wrapper to handle common date formats"
spec.summary = "DateTime wrapper to handle common date formats"
spec.homepage = "http://github.com/alexduryee/timetwister"
spec.license = "MIT"

Expand All @@ -20,5 +20,4 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_runtime_dependency "chronic", "~> 0.10.2"
end

0 comments on commit fcf37a8

Please sign in to comment.