lib/chronic/handlers.rb in chronic-0.6.7 vs lib/chronic/handlers.rb in chronic-0.7.0
- old
+ new
@@ -222,10 +222,31 @@
new_tokens = [tokens[1], tokens[2], tokens[0]]
time_tokens = tokens.last(tokens.size - 3)
handle_sm_sd_sy(new_tokens + time_tokens, options)
end
+ # Handle scalar-day/scalar-month AND scalar-month/scalar-day
+ def handle_sm_sd(tokens, options)
+ month = tokens[0].get_tag(ScalarMonth).type
+ day = tokens[1].get_tag(ScalarDay).type
+ year = Chronic.now.year
+
+ if Array(options[:endian_precedence]).first == :little
+ day, month = month, day
+ end
+
+ return if month_overflow?(year, month, day)
+
+ begin
+ start_time = Chronic.time_class.local(year, month, day)
+ end_time = Chronic.time_class.local(year, month, day + 1)
+ Span.new(start_time, end_time)
+ rescue ArgumentError
+ nil
+ end
+ end
+
# Handle scalar-month/scalar-year
def handle_sm_sy(tokens, options)
month = tokens[0].get_tag(ScalarMonth).type
year = tokens[1].get_tag(ScalarYear).type
@@ -294,10 +315,19 @@
rescue ArgumentError
nil
end
end
+ def handle_sm_rmn_sy(tokens, options)
+ day = tokens[0].get_tag(ScalarDay).type
+ month = tokens[1].get_tag(RepeaterMonthName).index
+ year = tokens[2].get_tag(ScalarYear).type
+ time = Chronic.time_class.local(year, month, day)
+ end_time = Chronic.time_class.local(year, month, day + 1)
+ Span.new(time, end_time)
+ end
+
# anchors
# Handle repeaters
def handle_r(tokens, options)
dd_tokens = dealias_and_disambiguate_times(tokens, options)
@@ -316,11 +346,11 @@
def handle_srp(tokens, span, options)
distance = tokens[0].get_tag(Scalar).type
repeater = tokens[1].get_tag(Repeater)
pointer = tokens[2].get_tag(Pointer).type
- repeater.offset(span, distance, pointer)
+ repeater.offset(span, distance, pointer) if repeater.respond_to?(:offset)
end
# Handle scalar/repeater/pointer
def handle_s_r_p(tokens, options)
repeater = tokens[1].get_tag(Repeater)
@@ -432,9 +462,11 @@
if Date.leap?(year)
day > RepeaterMonth::MONTH_DAYS_LEAP[month - 1]
else
day > RepeaterMonth::MONTH_DAYS[month - 1]
end
+ rescue ArgumentError
+ false
end
# Recursively finds repeaters within other repeaters.
# Returns a Span representing the innermost time span
# or nil if no repeater union could be found