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

Allow time_class to be given as an option to Chronic.parse #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions lib/chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def self.parse(text, options = {})
# second - Integer second.
#
# Returns a new Time object constructed from these params.
def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, offset = nil)
def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, offset = nil, time_class = Chronic.time_class)
if second >= 60
minute += second / 60
second = second % 60
Expand Down Expand Up @@ -139,13 +139,14 @@ def self.construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0, o
month = month % 12
end
end
if Chronic.time_class.name == 'Date'
Chronic.time_class.new(year, month, day)
elsif not Chronic.time_class.respond_to?(:new) or (RUBY_VERSION.to_f < 1.9 and Chronic.time_class.name == 'Time')
Chronic.time_class.local(year, month, day, hour, minute, second)

if time_class.name == 'Date'
time_class.new(year, month, day)
elsif not time_class.respond_to?(:new) or (RUBY_VERSION.to_f < 1.9 and time_class.name == 'Time')
time_class.local(year, month, day, hour, minute, second)
else
offset = Time::normalize_offset(offset) if Chronic.time_class.name == 'DateTime'
Chronic.time_class.new(year, month, day, hour, minute, second, offset)
offset = Time::normalize_offset(offset, time_class) if time_class.name == 'DateTime'
time_class.new(year, month, day, hour, minute, second, offset)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/chronic/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def self.could_be_year?(year)
# make_year(00, 50) #=> 2000
#
# Returns The Integer 4 digit year.
def self.make_year(year, bias)
def self.make_year(year, bias, time_class = Chronic.time_class)
return year if year.to_s.size > 2
start_year = Chronic.time_class.now.year - bias
start_year = time_class.now.year - bias
century = (start_year / 100) * 100
full_year = century + year
full_year += 100 if full_year < start_year
Expand Down
95 changes: 56 additions & 39 deletions lib/chronic/handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ module Handlers

# Handle month/day
def handle_m_d(month, day, time_tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month.start = self.now
span = month.this(options[:context])
year, month = span.begin.year, span.begin.month
day_start = Chronic.time_class.local(year, month, day)
day_start = Chronic.time_class.local(year + 1, month, day) if options[:context] == :future && day_start < now
day_start = time_class.local(year, month, day)
day_start = time_class.local(year + 1, month, day) if options[:context] == :future && day_start < now

day_or_time(day_start, time_tokens, options)
end
Expand Down Expand Up @@ -68,6 +69,7 @@ def handle_od_rmn(tokens, options)
end

def handle_sy_rmn_od(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
year = tokens[0].get_tag(ScalarYear).type
month = tokens[1].get_tag(RepeaterMonthName).index
day = tokens[2].get_tag(OrdinalDay).type
Expand All @@ -76,7 +78,7 @@ def handle_sy_rmn_od(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand Down Expand Up @@ -112,6 +114,7 @@ def handle_rmn_od_on(tokens, options)

# Handle repeater-month-name/scalar-year
def handle_rmn_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(RepeaterMonthName).index
year = tokens[1].get_tag(ScalarYear).type

Expand All @@ -124,23 +127,25 @@ def handle_rmn_sy(tokens, options)
end

begin
end_time = Chronic.time_class.local(next_month_year, next_month_month)
Span.new(Chronic.time_class.local(year, month), end_time)
end_time = time_class.local(next_month_year, next_month_month)
Span.new(time_class.local(year, month), end_time)
rescue ArgumentError
nil
end
end

# Handle generic timestamp (ruby 1.8)
def handle_generic(tokens, options)
t = Chronic.time_class.parse(options[:text])
time_class = options.fetch(:time_class, Chronic.time_class)
t = time_class.parse(options[:text])
Span.new(t, t + 1)
rescue ArgumentError => e
raise e unless e.message =~ /out of range/
end

# Handle repeater-month-name/scalar-day/scalar-year
def handle_rmn_sd_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(RepeaterMonthName).index
day = tokens[1].get_tag(ScalarDay).type
year = tokens[2].get_tag(ScalarYear).type
Expand All @@ -149,7 +154,7 @@ def handle_rmn_sd_sy(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand All @@ -158,6 +163,7 @@ def handle_rmn_sd_sy(tokens, options)

# Handle repeater-month-name/ordinal-day/scalar-year
def handle_rmn_od_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(RepeaterMonthName).index
day = tokens[1].get_tag(OrdinalDay).type
year = tokens[2].get_tag(ScalarYear).type
Expand All @@ -166,7 +172,7 @@ def handle_rmn_od_sy(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand All @@ -175,6 +181,7 @@ def handle_rmn_od_sy(tokens, options)

# Handle oridinal-day/repeater-month-name/scalar-year
def handle_od_rmn_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
day = tokens[0].get_tag(OrdinalDay).type
month = tokens[1].get_tag(RepeaterMonthName).index
year = tokens[2].get_tag(ScalarYear).type
Expand All @@ -183,7 +190,7 @@ def handle_od_rmn_sy(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand All @@ -199,6 +206,7 @@ def handle_sd_rmn_sy(tokens, options)

# Handle scalar-month/scalar-day/scalar-year (endian middle)
def handle_sm_sd_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(ScalarMonth).type
day = tokens[1].get_tag(ScalarDay).type
year = tokens[2].get_tag(ScalarYear).type
Expand All @@ -207,7 +215,7 @@ def handle_sm_sd_sy(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand All @@ -230,6 +238,7 @@ def handle_sy_sm_sd(tokens, options)

# Handle scalar-month/scalar-day
def handle_sm_sd(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(ScalarMonth).type
day = tokens[1].get_tag(ScalarDay).type
year = self.now.year
Expand All @@ -238,8 +247,8 @@ def handle_sm_sd(tokens, options)
return if month_overflow?(year, month, day)

begin
day_start = Chronic.time_class.local(year, month, day)
day_start = Chronic.time_class.local(year + 1, month, day) if options[:context] == :future && day_start < now
day_start = time_class.local(year, month, day)
day_start = time_class.local(year + 1, month, day) if options[:context] == :future && day_start < now
day_or_time(day_start, time_tokens, options)
rescue ArgumentError
nil
Expand All @@ -253,7 +262,7 @@ def handle_sd_sm(tokens, options)
handle_sm_sd(new_tokens + time_tokens, options)
end

def handle_year_and_month(year, month)
def handle_year_and_month(year, month, time_class)
if month == 12
next_month_year = year + 1
next_month_month = 1
Expand All @@ -263,29 +272,32 @@ def handle_year_and_month(year, month)
end

begin
end_time = Chronic.time_class.local(next_month_year, next_month_month)
Span.new(Chronic.time_class.local(year, month), end_time)
end_time = time_class.local(next_month_year, next_month_month)
Span.new(time_class.local(year, month), end_time)
rescue ArgumentError
nil
end
end

# Handle scalar-month/scalar-year
def handle_sm_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[0].get_tag(ScalarMonth).type
year = tokens[1].get_tag(ScalarYear).type
handle_year_and_month(year, month)
handle_year_and_month(year, month, time_class)
end

# Handle scalar-year/scalar-month
def handle_sy_sm(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
year = tokens[0].get_tag(ScalarYear).type
month = tokens[1].get_tag(ScalarMonth).type
handle_year_and_month(year, month)
handle_year_and_month(year, month, time_class)
end

# Handle RepeaterDayName RepeaterMonthName OrdinalDay
def handle_rdn_rmn_od(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(OrdinalDay).type
time_tokens = tokens.last(tokens.size - 3)
Expand All @@ -295,11 +307,11 @@ def handle_rdn_rmn_od(tokens, options)

begin
if time_tokens.empty?
start_time = Chronic.time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1)
start_time = time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1, time_class)
Span.new(start_time, end_time)
else
day_start = Chronic.time_class.local(year, month.index, day)
day_start = time_class.local(year, month.index, day)
day_or_time(day_start, time_tokens, options)
end
rescue ArgumentError
Expand All @@ -309,15 +321,16 @@ def handle_rdn_rmn_od(tokens, options)

# Handle RepeaterDayName RepeaterMonthName OrdinalDay ScalarYear
def handle_rdn_rmn_od_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(OrdinalDay).type
year = tokens[3].get_tag(ScalarYear).type

return if month_overflow?(year, month.index, day)

begin
start_time = Chronic.time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1)
start_time = time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1, time_class)
Span.new(start_time, end_time)
rescue ArgumentError
nil
Expand All @@ -326,6 +339,7 @@ def handle_rdn_rmn_od_sy(tokens, options)

# Handle RepeaterDayName OrdinalDay
def handle_rdn_od(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
day = tokens[1].get_tag(OrdinalDay).type
time_tokens = tokens.last(tokens.size - 2)
year = self.now.year
Expand All @@ -338,11 +352,11 @@ def handle_rdn_od(tokens, options)

begin
if time_tokens.empty?
start_time = Chronic.time_class.local(year, month, day)
end_time = time_with_rollover(year, month, day + 1)
start_time = time_class.local(year, month, day)
end_time = time_with_rollover(year, month, day + 1, time_class)
Span.new(start_time, end_time)
else
day_start = Chronic.time_class.local(year, month, day)
day_start = time_class.local(year, month, day)
day_or_time(day_start, time_tokens, options)
end
rescue ArgumentError
Expand All @@ -352,6 +366,7 @@ def handle_rdn_od(tokens, options)

# Handle RepeaterDayName RepeaterMonthName ScalarDay
def handle_rdn_rmn_sd(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(ScalarDay).type
time_tokens = tokens.last(tokens.size - 3)
Expand All @@ -361,11 +376,11 @@ def handle_rdn_rmn_sd(tokens, options)

begin
if time_tokens.empty?
start_time = Chronic.time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1)
start_time = time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1, time_class)
Span.new(start_time, end_time)
else
day_start = Chronic.time_class.local(year, month.index, day)
day_start = time_class.local(year, month.index, day)
day_or_time(day_start, time_tokens, options)
end
rescue ArgumentError
Expand All @@ -375,34 +390,36 @@ def handle_rdn_rmn_sd(tokens, options)

# Handle RepeaterDayName RepeaterMonthName ScalarDay ScalarYear
def handle_rdn_rmn_sd_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
month = tokens[1].get_tag(RepeaterMonthName)
day = tokens[2].get_tag(ScalarDay).type
year = tokens[3].get_tag(ScalarYear).type

return if month_overflow?(year, month.index, day)

begin
start_time = Chronic.time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1)
start_time = time_class.local(year, month.index, day)
end_time = time_with_rollover(year, month.index, day + 1, time_class)
Span.new(start_time, end_time)
rescue ArgumentError
nil
end
end

def handle_sm_rmn_sy(tokens, options)
time_class = options.fetch(:time_class, Chronic.time_class)
day = tokens[0].get_tag(ScalarDay).type
month = tokens[1].get_tag(RepeaterMonthName).index
year = tokens[2].get_tag(ScalarYear).type
if tokens.size > 3
time = get_anchor([tokens.last], options).begin
h, m, s = time.hour, time.min, time.sec
time = Chronic.time_class.local(year, month, day, h, m, s)
end_time = Chronic.time_class.local(year, month, day + 1, h, m, s)
time = time_class.local(year, month, day, h, m, s)
end_time = time_class.local(year, month, day + 1, h, m, s)
else
time = Chronic.time_class.local(year, month, day)
time = time_class.local(year, month, day)
day += 1 unless day >= 31
end_time = Chronic.time_class.local(year, month, day)
end_time = time_class.local(year, month, day)
end
Span.new(time, end_time)
end
Expand Down Expand Up @@ -570,7 +587,7 @@ def find_within(tags, span, pointer)
end
end

def time_with_rollover(year, month, day)
def time_with_rollover(year, month, day, time_class)
date_parts =
if month_overflow?(year, month, day)
if month == 12
Expand All @@ -581,7 +598,7 @@ def time_with_rollover(year, month, day)
else
[year, month, day]
end
Chronic.time_class.local(*date_parts)
time_class.local(*date_parts)
end

def dealias_and_disambiguate_times(tokens, options)
Expand Down Expand Up @@ -613,11 +630,11 @@ def dealias_and_disambiguate_times(tokens, options)
when :morning
puts '--morning->am' if Chronic.debug
t1.untag(RepeaterDayPortion)
t1.tag(RepeaterDayPortion.new(:am))
t1.tag(RepeaterDayPortion.new(:am, options))
when :afternoon, :evening, :night
puts "--#{t1tag.type}->pm" if Chronic.debug
t1.untag(RepeaterDayPortion)
t1.tag(RepeaterDayPortion.new(:pm))
t1.tag(RepeaterDayPortion.new(:pm, options))
end
end

Expand All @@ -632,7 +649,7 @@ def dealias_and_disambiguate_times(tokens, options)
if token.get_tag(RepeaterTime) && token.get_tag(RepeaterTime).type.ambiguous? && (!next_token || !next_token.get_tag(RepeaterDayPortion))
distoken = Token.new('disambiguator')

distoken.tag(RepeaterDayPortion.new(options[:ambiguous_time_range]))
distoken.tag(RepeaterDayPortion.new(options[:ambiguous_time_range], options))
ambiguous_tokens << distoken
end
end
Expand Down
Loading