chronic 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +4 -0
- data/Manifest.txt +1 -0
- data/chronic.gemspec +2 -1
- data/lib/chronic/handlers.rb +2 -2
- data/lib/chronic/mini_date.rb +4 -3
- data/lib/chronic/ordinal.rb +0 -2
- data/lib/chronic/pointer.rb +0 -3
- data/lib/chronic/repeater.rb +3 -10
- data/lib/chronic/repeaters/repeater_year.rb +1 -1
- data/lib/chronic/scalar.rb +1 -4
- data/lib/chronic/separator.rb +1 -3
- data/lib/chronic/tag.rb +7 -2
- data/lib/chronic.rb +1 -1
- data/test/test_MiniDate.rb +32 -0
- metadata +4 -2
data/HISTORY.md
CHANGED
data/Manifest.txt
CHANGED
data/chronic.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'chronic'
|
3
|
-
s.version = '0.4.
|
3
|
+
s.version = '0.4.1'
|
4
4
|
s.rubyforge_project = 'chronic'
|
5
5
|
|
6
6
|
s.summary = "Natural language date/time parsing."
|
@@ -57,6 +57,7 @@ Gem::Specification.new do |s|
|
|
57
57
|
test/test_Chronic.rb
|
58
58
|
test/test_DaylightSavings.rb
|
59
59
|
test/test_Handler.rb
|
60
|
+
test/test_MiniDate.rb
|
60
61
|
test/test_Numerizer.rb
|
61
62
|
test/test_RepeaterDayName.rb
|
62
63
|
test/test_RepeaterFortnight.rb
|
data/lib/chronic/handlers.rb
CHANGED
@@ -325,7 +325,7 @@ module Chronic
|
|
325
325
|
# raise(ChronicPain, "Invalid repeater: #{repeater.class}")
|
326
326
|
# end
|
327
327
|
|
328
|
-
span =
|
328
|
+
span = Span.new(@now, @now + 1)
|
329
329
|
|
330
330
|
self.handle_srp(tokens, span, options)
|
331
331
|
end
|
@@ -399,7 +399,7 @@ module Chronic
|
|
399
399
|
end
|
400
400
|
|
401
401
|
puts "--#{outer_span}" if Chronic.debug
|
402
|
-
|
402
|
+
find_within(repeaters, outer_span, pointer)
|
403
403
|
end
|
404
404
|
|
405
405
|
def get_repeaters(tokens) #:nodoc:
|
data/lib/chronic/mini_date.rb
CHANGED
@@ -3,18 +3,19 @@ module Chronic
|
|
3
3
|
attr_accessor :month, :day
|
4
4
|
|
5
5
|
def initialize(month, day)
|
6
|
+
raise(InvalidArgumentException, "1..12 are valid months") unless (1..12).include?(month)
|
6
7
|
@month = month
|
7
8
|
@day = day
|
8
9
|
end
|
9
10
|
|
10
11
|
def is_between?(md_start, md_end)
|
12
|
+
return false if (@month==md_start.month && @month==md_end.month && (@day < md_start.day || @day > md_end.day))
|
11
13
|
return true if (@month == md_start.month and @day >= md_start.day) ||
|
12
14
|
(@month == md_end.month and @day <= md_end.day)
|
13
|
-
|
14
|
-
i = md_start.month + 1
|
15
|
+
i = (md_start.month % 12) + 1
|
15
16
|
until i == md_end.month
|
16
17
|
return true if @month == i
|
17
|
-
i = (i
|
18
|
+
i = (i % 12) + 1
|
18
19
|
end
|
19
20
|
|
20
21
|
return false
|
data/lib/chronic/ordinal.rb
CHANGED
data/lib/chronic/pointer.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Chronic
|
2
|
-
|
3
2
|
class Pointer < Tag #:nodoc:
|
4
3
|
def self.scan(tokens, options)
|
5
|
-
# for each token
|
6
4
|
tokens.each_index do |i|
|
7
5
|
if t = scan_for_all(tokens[i]) then tokens[i].tag(t) end
|
8
6
|
end
|
@@ -21,5 +19,4 @@ module Chronic
|
|
21
19
|
'pointer-' << @type.to_s
|
22
20
|
end
|
23
21
|
end
|
24
|
-
|
25
22
|
end
|
data/lib/chronic/repeater.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Chronic
|
2
2
|
class Repeater < Tag #:nodoc:
|
3
3
|
def self.scan(tokens, options)
|
4
|
-
# for each token
|
5
4
|
tokens.each_index do |i|
|
6
5
|
if t = scan_for_season_names(tokens[i]) then tokens[i].tag(t); next end
|
7
6
|
if t = scan_for_month_names(tokens[i]) then tokens[i].tag(t); next end
|
@@ -69,10 +68,7 @@ module Chronic
|
|
69
68
|
end
|
70
69
|
|
71
70
|
def self.scan_for_times(token)
|
72
|
-
|
73
|
-
return Chronic::RepeaterTime.new(token.word)
|
74
|
-
end
|
75
|
-
return nil
|
71
|
+
scan_for token, RepeaterTime, /^\d{1,2}(:?\d{2})?([\.:]?\d{2})?$/
|
76
72
|
end
|
77
73
|
|
78
74
|
def self.scan_for_units(token)
|
@@ -109,14 +105,11 @@ module Chronic
|
|
109
105
|
|
110
106
|
# returns the next occurance of this repeatable.
|
111
107
|
def next(pointer)
|
112
|
-
|
113
|
-
[:future, :none, :past].include?(pointer) || raise("First argument 'pointer' must be one of :past or :future")
|
114
|
-
#raise("Repeatable#next must be overridden in subclasses")
|
108
|
+
raise("Start point must be set before calling #next") unless @now
|
115
109
|
end
|
116
110
|
|
117
111
|
def this(pointer)
|
118
|
-
|
119
|
-
[:future, :past, :none].include?(pointer) || raise("First argument 'pointer' must be one of :past, :future, :none")
|
112
|
+
raise("Start point must be set before calling #this") unless @now
|
120
113
|
end
|
121
114
|
|
122
115
|
def to_s
|
data/lib/chronic/scalar.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
module Chronic
|
2
|
-
|
3
2
|
class Scalar < Tag #:nodoc:
|
4
3
|
DAY_PORTIONS = %w( am pm morning afternoon evening night )
|
5
4
|
|
6
5
|
def self.scan(tokens, options)
|
7
|
-
# for each token
|
8
6
|
tokens.each_index do |i|
|
9
7
|
if t = scan_for_scalars(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
|
10
8
|
if t = scan_for_days(tokens[i], tokens[i + 1]) then tokens[i].tag(t) end
|
@@ -80,5 +78,4 @@ module Chronic
|
|
80
78
|
super << '-year-' << @type.to_s
|
81
79
|
end
|
82
80
|
end
|
83
|
-
|
84
|
-
end
|
81
|
+
end
|
data/lib/chronic/separator.rb
CHANGED
data/lib/chronic/tag.rb
CHANGED
@@ -16,8 +16,13 @@ module Chronic
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def scan_for(token, klass, items={})
|
19
|
-
items
|
20
|
-
|
19
|
+
case items
|
20
|
+
when Regexp
|
21
|
+
return klass.new(token.word) if items =~ token.word
|
22
|
+
when Hash
|
23
|
+
items.each do |item, symbol|
|
24
|
+
return klass.new(symbol) if item =~ token.word
|
25
|
+
end
|
21
26
|
end
|
22
27
|
nil
|
23
28
|
end
|
data/lib/chronic.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[helper])
|
2
|
+
|
3
|
+
class TestMiniDate < Test::Unit::TestCase
|
4
|
+
def test_valid_month
|
5
|
+
assert_raise(Chronic::InvalidArgumentException){ Chronic::MiniDate.new(0,12) }
|
6
|
+
assert_raise(Chronic::InvalidArgumentException){ Chronic::MiniDate.new(13,1) }
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_is_between
|
10
|
+
m=Chronic::MiniDate.new(3,2)
|
11
|
+
assert m.is_between?(Chronic::MiniDate.new(2,4), Chronic::MiniDate.new(4,7))
|
12
|
+
assert !m.is_between?(Chronic::MiniDate.new(1,5), Chronic::MiniDate.new(2,7))
|
13
|
+
|
14
|
+
#There was a hang if date tested is in december and outside the testing range
|
15
|
+
m=Chronic::MiniDate.new(12,24)
|
16
|
+
assert !m.is_between?(Chronic::MiniDate.new(10,1), Chronic::MiniDate.new(12,21))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_is_between_short_range
|
20
|
+
m=Chronic::MiniDate.new(5,10)
|
21
|
+
assert m.is_between?(Chronic::MiniDate.new(5,3), Chronic::MiniDate.new(5,12))
|
22
|
+
assert !m.is_between?(Chronic::MiniDate.new(5,11), Chronic::MiniDate.new(5,15))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_is_between_wrapping_range
|
26
|
+
m=Chronic::MiniDate.new(1,1)
|
27
|
+
assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(2,2))
|
28
|
+
m=Chronic::MiniDate.new(12,12)
|
29
|
+
assert m.is_between?(Chronic::MiniDate.new(11,11), Chronic::MiniDate.new(1,5))
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: chronic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.4.
|
5
|
+
version: 0.4.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Tom Preston-Werner
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-06-
|
14
|
+
date: 2011-06-05 00:00:00 Z
|
15
15
|
dependencies: []
|
16
16
|
|
17
17
|
description: Chronic is a natural language date/time parser written in pure Ruby.
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- test/test_Chronic.rb
|
70
70
|
- test/test_DaylightSavings.rb
|
71
71
|
- test/test_Handler.rb
|
72
|
+
- test/test_MiniDate.rb
|
72
73
|
- test/test_Numerizer.rb
|
73
74
|
- test/test_RepeaterDayName.rb
|
74
75
|
- test/test_RepeaterFortnight.rb
|
@@ -116,6 +117,7 @@ test_files:
|
|
116
117
|
- test/test_Chronic.rb
|
117
118
|
- test/test_DaylightSavings.rb
|
118
119
|
- test/test_Handler.rb
|
120
|
+
- test/test_MiniDate.rb
|
119
121
|
- test/test_Numerizer.rb
|
120
122
|
- test/test_RepeaterDayName.rb
|
121
123
|
- test/test_RepeaterFortnight.rb
|