Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Oct 20, 2006
1 parent de6b038 commit 89360d9
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
HEAD
0.1.3
* improved regexes for word variations (Josh Goebel)
* fixed a bug that caused "today at 3am" to return nil if current time is
after 3am

0.1.2
* removed Date dependency (now works on windows properly without fiddling)
Expand Down
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Chronic can be installed via RubyGems:

You can parse strings containing a natural language date using the Chronic.parse method.

require 'rubygems'
require 'chronic'

Time.now #=> Sun Aug 27 23:18:25 PDT 2006
Expand Down
2 changes: 1 addition & 1 deletion chronic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'

SPEC = Gem::Specification.new do |s|
s.name = 'chronic'
s.version = '0.1.2'
s.version = '0.1.3'
s.author = 'Tom Preston-Werner'
s.email = 'tom@rubyisawesome.com'
s.homepage = 'http://chronic.rubyforge.org'
Expand Down
3 changes: 2 additions & 1 deletion lib/chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
require 'chronic/separator'

module Chronic
@version = "0.1.3"
def self.debug=(val); @debug = val; end
end

Chronic.debug = false
Chronic.debug = true

4 changes: 2 additions & 2 deletions lib/chronic/chronic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse(text, specified_options = {})
specified_options.keys.each do |key|
default_options.keys.include?(key) || raise(InvalidArgumentException, "#{key} is not a valid option key.")
end
[:past, :future].include?(options[:context]) || raise(InvalidArgumentException, "Invalid value '#{options[:context]}' for :context specified. Valid values are :past and :future.")
[:past, :future, :none].include?(options[:context]) || raise(InvalidArgumentException, "Invalid value ':#{options[:context]}' for :context specified. Valid values are :past and :future.")

# store now for later =)
@now = options[:now]
Expand All @@ -73,7 +73,7 @@ def parse(text, specified_options = {})
# strip any non-tagged tokens
@tokens = @tokens.select { |token| token.tagged? }

if @debug
if true
puts "+---------------------------------------------------"
puts "| " + @tokens.to_s
puts "+---------------------------------------------------"
Expand Down
24 changes: 19 additions & 5 deletions lib/chronic/handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def tokens_to_span(tokens, options) #:nodoc:
self.definitions[:date].each do |handler|
if handler.match(tokens, self.definitions)
good_tokens = tokens.select { |o| !o.get_tag Separator }
puts "--#{handler}"
return self.send(handler.handler_method, good_tokens, options)
end
end
Expand Down Expand Up @@ -67,6 +68,7 @@ def tokens_to_span(tokens, options) #:nodoc:
end

# I guess you're out of luck!
puts "--SUCKY"
return nil
end

Expand Down Expand Up @@ -282,14 +284,25 @@ def get_anchor(tokens, options) #:nodoc:

head = repeaters.shift
head.start = @now

case grabber.type
when :last: outer_span = head.next(:past)
when :this: outer_span = head.this(options[:context])
when :next: outer_span = head.next(:future)
when :last
outer_span = head.next(:past)
when :this
puts "--THIS"
puts "--#{repeaters}"
if repeaters.size > 0
puts "--NONE"
outer_span = head.this(:none)
else
outer_span = head.this(options[:context])
end
when :next
outer_span = head.next(:future)
else raise(ChronicPain, "Invalid grabber")
end

puts "--#{outer_span}"
anchor = find_within(repeaters, outer_span, pointer)
end

Expand All @@ -307,11 +320,12 @@ def get_repeaters(tokens) #:nodoc:
# Returns a Span representing the innermost time span
# or nil if no repeater union could be found
def find_within(tags, span, pointer) #:nodoc:
puts "--#{span}"
return span if tags.empty?

head, *rest = tags
head.start = pointer == :future ? span.begin : span.end
h = head.this(pointer)
h = head.this(:none)

if span.include?(h.begin) || span.include?(h.end)
return find_within(rest, h, pointer)
Expand Down
4 changes: 2 additions & 2 deletions lib/chronic/repeater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.scan(tokens, options)
if t = self.scan_for_day_names(tokens[i]) then tokens[i].tag(t); next end
if t = self.scan_for_day_portions(tokens[i]) then tokens[i].tag(t); next end
if t = self.scan_for_times(tokens[i], options) then tokens[i].tag(t); next end
if t = self.scan_for_units(tokens[i]) then tokens[i].tag(t); next end
if t = self.scan_for_units(tokens[i]) then tokens[i].tag(t); next end
end
tokens
end
Expand Down Expand Up @@ -105,7 +105,7 @@ def next(pointer)

def this(pointer)
!@now.nil? || raise("Start point must be set before calling #next")
[:future, :past].include?(pointer) || raise("First argument 'pointer' must be one of :past or :future")
[:future, :past, :none].include?(pointer) || raise("First argument 'pointer' must be one of :past, :future, :none")
end

def to_s
Expand Down
3 changes: 3 additions & 0 deletions lib/chronic/repeaters/repeater_day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def this(pointer = :future)
when :past
day_begin = Time.local(@now.year, @now.month, @now.day)
day_end = Time.local(@now.year, @now.month, @now.day, @now.hour)
when :none
day_begin = Time.local(@now.year, @now.month, @now.day)
day_end = Time.local(@now.year, @now.month, @now.day) + DAY_SECONDS
end

Chronic::Span.new(day_begin, day_end)
Expand Down
1 change: 1 addition & 0 deletions lib/chronic/repeaters/repeater_day_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def next(pointer)
def this(pointer = :future)
super

pointer = :future if pointer == :none
self.next(pointer)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/chronic/repeaters/repeater_fortnight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def next(pointer)
def this(pointer = :future)
super

pointer = :future if pointer == :none

case pointer
when :future
this_fortnight_start = Time.local(@now.year, @now.month, @now.day, @now.hour) + Chronic::RepeaterHour::HOUR_SECONDS
Expand All @@ -42,7 +44,6 @@ def this(pointer = :future)
this_fortnight_end = Time.local(@now.year, @now.month, @now.day, @now.hour)
sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
sunday_repeater.start = @now
#sunday_repeater.next(:past)
last_sunday_span = sunday_repeater.next(:past)
this_fortnight_start = last_sunday_span.begin
Chronic::Span.new(this_fortnight_start, this_fortnight_end)
Expand Down
25 changes: 23 additions & 2 deletions lib/chronic/repeaters/repeater_minute.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
class Chronic::RepeaterMinute < Chronic::Repeater #:nodoc:
MINUTE_SECONDS = 60

def next(pointer = :future)
super

direction = pointer == :future ? 1 : -1

raise 'not implemented'
end

def this(pointer = :future)
minute_begin = Time.local(@now.year, @now.month, @now.day, @now.hour, @now.min)
Chronic::Span.new(minute_begin, minute_begin + MINUTE_SECONDS)
super

case pointer
when :future
minute_begin = @now
minute_end = Time.local(@now.year, @now.month, @now.day, @now.hour, @now.min)
when :past
minute_begin = Time.local(@now.year, @now.month, @now.day, @now.hour, @now.min)
minute_end = @now
when :none
minute_begin = Time.local(@now.year, @now.month, @now.day, @now.hour, @now.min)
minute_end = Time.local(@now.year, @now.month, @now.day, @now.hour, @now.min) + MINUTE_SECONDS
end

Chronic::Span.new(minute_begin, minute_end)
end

def offset(span, amount, pointer)
Expand Down
3 changes: 3 additions & 0 deletions lib/chronic/repeaters/repeater_month.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def this(pointer = :future)
when :past
month_start = Time.local(@now.year, @now.month)
month_end = Time.local(@now.year, @now.month, @now.day)
when :none
month_start = Time.local(@now.year, @now.month)
month_end = self.offset_by(Time.local(@now.year, @now.month), 1, :future)
end

Chronic::Span.new(month_start, month_end)
Expand Down
2 changes: 2 additions & 0 deletions lib/chronic/repeaters/repeater_month_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def next(pointer)
def this(pointer = :future)
super

pointer = :future if pointer == :none

self.next(pointer)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/chronic/repeaters/repeater_second.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def next(pointer = :future)
end

def this(pointer = :future)
super

Chronic::Span.new(@now, @now + 1)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/chronic/repeaters/repeater_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ def next(pointer)
end

def this(context = :future)
[:future, :past].include?(context) || raise("First argument 'context' must be one of :past or :future")
super

context = :future if context == :none

self.next(context)
end

Expand Down
6 changes: 6 additions & 0 deletions lib/chronic/repeaters/repeater_week.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def this(pointer = :future)
last_sunday_span = sunday_repeater.next(:past)
this_week_start = last_sunday_span.begin
Chronic::Span.new(this_week_start, this_week_end)
when :none
sunday_repeater = Chronic::RepeaterDayName.new(:sunday)
sunday_repeater.start = @now
last_sunday_span = sunday_repeater.next(:past)
this_week_start = last_sunday_span.begin
Chronic::Span.new(this_week_start, this_week_start + WEEK_SECONDS)
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/chronic/repeaters/repeater_year.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def this(pointer = :future)
when :past
this_year_start = Time.local(@now.year, 1, 1)
this_year_end = Time.local(@now.year, @now.month, @now.day)
when :none
this_year_start = Time.local(@now.year, 1, 1)
this_year_end = Time.local(@now.year + 1, 1, 1)
end

Chronic::Span.new(this_year_start, this_year_end)
Expand Down
2 changes: 1 addition & 1 deletion lib/chronic/scalar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def self.scan_for_months(token, post_token)
end

def self.scan_for_years(token, post_token)
if token.word =~ /^\d\d(\d\d)?$/
if token.word =~ /^([1-9]\d)?\d\d?$/
unless post_token && %w{am pm morning afternoon evening night}.include?(post_token)
return ScalarYear.new(token.word.to_i)
end
Expand Down
27 changes: 21 additions & 6 deletions test/test_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,22 @@ def test_parse_guess_gr
assert_equal Time.local(2006, 8, 16, 13, 59, 59), time
end

def test_parse_guess_grr
def test_parse_guess_grr
time = Chronic.parse("yesterday at 4:00", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 15, 16), time

time = Chronic.parse("today at 9:00", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 9), time

time = Chronic.parse("today at 2100", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 21), time

time = Chronic.parse("this day at 0900", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 9), time

time = Chronic.parse("tomorrow at 0900", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 17, 9), time

time = Chronic.parse("yesterday at 4:00", :now => @time_2006_08_16_14_00_00, :ambiguous_time_range => :none)
assert_equal Time.local(2006, 8, 15, 4), time

Expand All @@ -324,6 +336,9 @@ def test_parse_guess_grrr
time = Chronic.parse("today at 6:00pm", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 18), time

time = Chronic.parse("today at 6:00am", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 6), time

time = Chronic.parse("this day 1800", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 18), time

Expand Down Expand Up @@ -358,7 +373,7 @@ def test_parse_guess_s_r_p
assert_equal Time.local(2006, 7, 26, 14, 30, 30), time

time = Chronic.parse("3 days ago", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 13, 14, 0, 30), time
assert_equal Time.local(2006, 8, 13, 14), time

#time = Chronic.parse("1 monday ago", :now => @time_2006_08_16_14_00_00)
#assert_equal Time.local(2006, 8, 14, 12), time
Expand All @@ -367,7 +382,7 @@ def test_parse_guess_s_r_p
assert_equal Time.local(2006, 8, 12, 9), time

time = Chronic.parse("7 hours ago", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 7, 0, 30), time
assert_equal Time.local(2006, 8, 16, 7), time

time = Chronic.parse("3 minutes ago", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 13, 57), time
Expand All @@ -390,13 +405,13 @@ def test_parse_guess_s_r_p
assert_equal Time.local(2006, 8, 23, 14, 30, 30), time

time = Chronic.parse("1 day hence", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 17, 14, 0, 30), time
assert_equal Time.local(2006, 8, 17, 14), time

time = Chronic.parse("5 mornings hence", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 21, 9), time

time = Chronic.parse("1 hour from now", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 15, 0, 30), time
assert_equal Time.local(2006, 8, 16, 15), time

time = Chronic.parse("20 minutes hence", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 14, 20), time
Expand All @@ -407,7 +422,7 @@ def test_parse_guess_s_r_p

def test_parse_guess_p_s_r
time = Chronic.parse("in 3 hours", :now => @time_2006_08_16_14_00_00)
assert_equal Time.local(2006, 8, 16, 17, 0, 30), time
assert_equal Time.local(2006, 8, 16, 17), time
end

def test_parse_guess_s_r_p_a
Expand Down

0 comments on commit 89360d9

Please sign in to comment.