chronic 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/HISTORY.md +76 -0
  2. data/LICENSE +21 -0
  3. data/README.md +165 -0
  4. data/Rakefile +145 -18
  5. data/benchmark/benchmark.rb +13 -0
  6. data/chronic.gemspec +85 -0
  7. data/lib/chronic.rb +21 -19
  8. data/lib/chronic/chronic.rb +59 -49
  9. data/lib/chronic/grabber.rb +2 -2
  10. data/lib/chronic/handlers.rb +167 -112
  11. data/lib/{numerizer → chronic/numerizer}/numerizer.rb +17 -23
  12. data/lib/chronic/ordinal.rb +6 -6
  13. data/lib/chronic/pointer.rb +3 -3
  14. data/lib/chronic/repeater.rb +26 -12
  15. data/lib/chronic/repeaters/repeater_day.rb +17 -12
  16. data/lib/chronic/repeaters/repeater_day_name.rb +17 -12
  17. data/lib/chronic/repeaters/repeater_day_portion.rb +13 -12
  18. data/lib/chronic/repeaters/repeater_fortnight.rb +14 -9
  19. data/lib/chronic/repeaters/repeater_hour.rb +15 -10
  20. data/lib/chronic/repeaters/repeater_minute.rb +15 -10
  21. data/lib/chronic/repeaters/repeater_month.rb +20 -15
  22. data/lib/chronic/repeaters/repeater_month_name.rb +21 -16
  23. data/lib/chronic/repeaters/repeater_season.rb +136 -9
  24. data/lib/chronic/repeaters/repeater_season_name.rb +38 -17
  25. data/lib/chronic/repeaters/repeater_second.rb +15 -10
  26. data/lib/chronic/repeaters/repeater_time.rb +49 -42
  27. data/lib/chronic/repeaters/repeater_week.rb +16 -11
  28. data/lib/chronic/repeaters/repeater_weekday.rb +77 -0
  29. data/lib/chronic/repeaters/repeater_weekend.rb +14 -9
  30. data/lib/chronic/repeaters/repeater_year.rb +19 -13
  31. data/lib/chronic/scalar.rb +16 -14
  32. data/lib/chronic/separator.rb +25 -10
  33. data/lib/chronic/time_zone.rb +4 -3
  34. data/test/helper.rb +7 -0
  35. data/test/test_Chronic.rb +17 -18
  36. data/test/test_DaylightSavings.rb +118 -0
  37. data/test/test_Handler.rb +37 -38
  38. data/test/test_Numerizer.rb +8 -5
  39. data/test/test_RepeaterDayName.rb +15 -16
  40. data/test/test_RepeaterFortnight.rb +16 -17
  41. data/test/test_RepeaterHour.rb +18 -19
  42. data/test/test_RepeaterMinute.rb +34 -0
  43. data/test/test_RepeaterMonth.rb +16 -17
  44. data/test/test_RepeaterMonthName.rb +17 -18
  45. data/test/test_RepeaterTime.rb +20 -22
  46. data/test/test_RepeaterWeek.rb +16 -17
  47. data/test/test_RepeaterWeekday.rb +55 -0
  48. data/test/test_RepeaterWeekend.rb +21 -22
  49. data/test/test_RepeaterYear.rb +17 -18
  50. data/test/test_Span.rb +5 -6
  51. data/test/test_Time.rb +11 -12
  52. data/test/test_Token.rb +5 -6
  53. data/test/test_parsing.rb +300 -204
  54. metadata +74 -52
  55. data/History.txt +0 -53
  56. data/README.txt +0 -149
  57. data/test/suite.rb +0 -9
@@ -1,8 +1,7 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class TestHandler < Test::Unit::TestCase
5
-
4
+
6
5
  def setup
7
6
  # Wed Aug 16 14:00:00 UTC 2006
8
7
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
@@ -10,101 +9,101 @@ class TestHandler < Test::Unit::TestCase
10
9
 
11
10
  def test_handler_class_1
12
11
  handler = Chronic::Handler.new([:repeater], :handler)
13
-
12
+
14
13
  tokens = [Chronic::Token.new('friday')]
15
14
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
16
-
15
+
17
16
  assert handler.match(tokens, Chronic.definitions)
18
-
17
+
19
18
  tokens << Chronic::Token.new('afternoon')
20
19
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
21
-
20
+
22
21
  assert !handler.match(tokens, Chronic.definitions)
23
22
  end
24
-
23
+
25
24
  def test_handler_class_2
26
25
  handler = Chronic::Handler.new([:repeater, :repeater?], :handler)
27
-
26
+
28
27
  tokens = [Chronic::Token.new('friday')]
29
28
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
30
-
29
+
31
30
  assert handler.match(tokens, Chronic.definitions)
32
-
31
+
33
32
  tokens << Chronic::Token.new('afternoon')
34
33
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
35
-
34
+
36
35
  assert handler.match(tokens, Chronic.definitions)
37
-
36
+
38
37
  tokens << Chronic::Token.new('afternoon')
39
38
  tokens[2].tag(Chronic::RepeaterDayPortion.new(:afternoon))
40
-
39
+
41
40
  assert !handler.match(tokens, Chronic.definitions)
42
41
  end
43
-
42
+
44
43
  def test_handler_class_3
45
44
  handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
46
-
45
+
47
46
  tokens = [Chronic::Token.new('friday')]
48
47
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
49
-
48
+
50
49
  assert handler.match(tokens, Chronic.definitions)
51
-
50
+
52
51
  tokens << Chronic::Token.new('afternoon')
53
52
  tokens[1].tag(Chronic::RepeaterDayPortion.new(:afternoon))
54
-
53
+
55
54
  assert !handler.match(tokens, Chronic.definitions)
56
55
  end
57
-
56
+
58
57
  def test_handler_class_4
59
58
  handler = Chronic::Handler.new([:repeater_month_name, :scalar_day, 'time?'], :handler)
60
-
59
+
61
60
  tokens = [Chronic::Token.new('may')]
62
61
  tokens[0].tag(Chronic::RepeaterMonthName.new(:may))
63
-
62
+
64
63
  assert !handler.match(tokens, Chronic.definitions)
65
-
64
+
66
65
  tokens << Chronic::Token.new('27')
67
66
  tokens[1].tag(Chronic::ScalarDay.new(27))
68
-
67
+
69
68
  assert handler.match(tokens, Chronic.definitions)
70
69
  end
71
-
70
+
72
71
  def test_handler_class_5
73
72
  handler = Chronic::Handler.new([:repeater, 'time?'], :handler)
74
-
73
+
75
74
  tokens = [Chronic::Token.new('friday')]
76
75
  tokens[0].tag(Chronic::RepeaterDayName.new(:friday))
77
-
76
+
78
77
  assert handler.match(tokens, Chronic.definitions)
79
-
78
+
80
79
  tokens << Chronic::Token.new('5:00')
81
80
  tokens[1].tag(Chronic::RepeaterTime.new('5:00'))
82
-
81
+
83
82
  assert handler.match(tokens, Chronic.definitions)
84
-
83
+
85
84
  tokens << Chronic::Token.new('pm')
86
85
  tokens[2].tag(Chronic::RepeaterDayPortion.new(:pm))
87
-
86
+
88
87
  assert handler.match(tokens, Chronic.definitions)
89
88
  end
90
-
89
+
91
90
  def test_handler_class_6
92
91
  handler = Chronic::Handler.new([:scalar, :repeater, :pointer], :handler)
93
-
92
+
94
93
  tokens = [Chronic::Token.new('3'),
95
94
  Chronic::Token.new('years'),
96
95
  Chronic::Token.new('past')]
97
-
96
+
98
97
  tokens[0].tag(Chronic::Scalar.new(3))
99
98
  tokens[1].tag(Chronic::RepeaterYear.new(:year))
100
99
  tokens[2].tag(Chronic::Pointer.new(:past))
101
-
100
+
102
101
  assert handler.match(tokens, Chronic.definitions)
103
102
  end
104
-
103
+
105
104
  def test_constantize
106
105
  handler = Chronic::Handler.new([], :handler)
107
106
  assert_equal Chronic::RepeaterTime, handler.constantize(:repeater_time)
108
107
  end
109
-
110
- end
108
+
109
+ end
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'chronic'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class ParseNumbersTest < Test::Unit::TestCase
5
4
 
@@ -23,7 +22,7 @@ class ParseNumbersTest < Test::Unit::TestCase
23
22
  100 => 'a hundred',
24
23
  100 => 'one hundred',
25
24
  150 => 'one hundred and fifty',
26
- # 150 => 'one fifty',
25
+ # 150 => 'one fifty',
27
26
  200 => 'two-hundred',
28
27
  500 => '5 hundred',
29
28
  999 => 'nine hundred and ninety nine',
@@ -40,9 +39,13 @@ class ParseNumbersTest < Test::Unit::TestCase
40
39
  1_250_007 => 'one million two hundred fifty thousand and seven',
41
40
  1_000_000_000 => 'one billion',
42
41
  1_000_000_001 => 'one billion and one' }
43
-
42
+
44
43
  strings.keys.sort.each do |key|
45
44
  assert_equal key, Numerizer.numerize(strings[key]).to_i
46
45
  end
47
46
  end
48
- end
47
+
48
+ def test_edges
49
+ assert_equal "27 Oct 2006 7:30am", Numerizer.numerize("27 Oct 2006 7:30am")
50
+ end
51
+ end
@@ -1,18 +1,17 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class TestRepeaterDayName < Test::Unit::TestCase
5
-
4
+
6
5
  def setup
7
6
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
7
  end
9
-
8
+
10
9
  def test_match
11
10
  token = Chronic::Token.new('saturday')
12
11
  repeater = Chronic::Repeater.scan_for_day_names(token)
13
12
  assert_equal Chronic::RepeaterDayName, repeater.class
14
13
  assert_equal :saturday, repeater.type
15
-
14
+
16
15
  token = Chronic::Token.new('sunday')
17
16
  repeater = Chronic::Repeater.scan_for_day_names(token)
18
17
  assert_equal Chronic::RepeaterDayName, repeater.class
@@ -22,31 +21,31 @@ class TestRepeaterDayName < Test::Unit::TestCase
22
21
  def test_next_future
23
22
  mondays = Chronic::RepeaterDayName.new(:monday)
24
23
  mondays.start = @now
25
-
24
+
26
25
  span = mondays.next(:future)
27
-
26
+
28
27
  assert_equal Time.local(2006, 8, 21), span.begin
29
- assert_equal Time.local(2006, 8, 22), span.end
28
+ assert_equal Time.local(2006, 8, 22), span.end
30
29
 
31
30
  span = mondays.next(:future)
32
-
31
+
33
32
  assert_equal Time.local(2006, 8, 28), span.begin
34
33
  assert_equal Time.local(2006, 8, 29), span.end
35
34
  end
36
-
35
+
37
36
  def test_next_past
38
37
  mondays = Chronic::RepeaterDayName.new(:monday)
39
38
  mondays.start = @now
40
-
39
+
41
40
  span = mondays.next(:past)
42
-
41
+
43
42
  assert_equal Time.local(2006, 8, 14), span.begin
44
- assert_equal Time.local(2006, 8, 15), span.end
43
+ assert_equal Time.local(2006, 8, 15), span.end
45
44
 
46
45
  span = mondays.next(:past)
47
-
46
+
48
47
  assert_equal Time.local(2006, 8, 7), span.begin
49
48
  assert_equal Time.local(2006, 8, 8), span.end
50
49
  end
51
-
52
- end
50
+
51
+ end
@@ -1,8 +1,7 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class TestRepeaterFortnight < Test::Unit::TestCase
5
-
4
+
6
5
  def setup
7
6
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
7
  end
@@ -10,54 +9,54 @@ class TestRepeaterFortnight < Test::Unit::TestCase
10
9
  def test_next_future
11
10
  fortnights = Chronic::RepeaterFortnight.new(:fortnight)
12
11
  fortnights.start = @now
13
-
12
+
14
13
  next_fortnight = fortnights.next(:future)
15
14
  assert_equal Time.local(2006, 8, 20), next_fortnight.begin
16
15
  assert_equal Time.local(2006, 9, 3), next_fortnight.end
17
-
16
+
18
17
  next_next_fortnight = fortnights.next(:future)
19
18
  assert_equal Time.local(2006, 9, 3), next_next_fortnight.begin
20
19
  assert_equal Time.local(2006, 9, 17), next_next_fortnight.end
21
20
  end
22
-
21
+
23
22
  def test_next_past
24
23
  fortnights = Chronic::RepeaterFortnight.new(:fortnight)
25
24
  fortnights.start = @now
26
-
25
+
27
26
  last_fortnight = fortnights.next(:past)
28
27
  assert_equal Time.local(2006, 7, 30), last_fortnight.begin
29
28
  assert_equal Time.local(2006, 8, 13), last_fortnight.end
30
-
29
+
31
30
  last_last_fortnight = fortnights.next(:past)
32
31
  assert_equal Time.local(2006, 7, 16), last_last_fortnight.begin
33
32
  assert_equal Time.local(2006, 7, 30), last_last_fortnight.end
34
33
  end
35
-
34
+
36
35
  def test_this_future
37
36
  fortnights = Chronic::RepeaterFortnight.new(:fortnight)
38
37
  fortnights.start = @now
39
-
38
+
40
39
  this_fortnight = fortnights.this(:future)
41
40
  assert_equal Time.local(2006, 8, 16, 15), this_fortnight.begin
42
41
  assert_equal Time.local(2006, 8, 27), this_fortnight.end
43
42
  end
44
-
43
+
45
44
  def test_this_past
46
45
  fortnights = Chronic::RepeaterFortnight.new(:fortnight)
47
46
  fortnights.start = @now
48
-
47
+
49
48
  this_fortnight = fortnights.this(:past)
50
49
  assert_equal Time.local(2006, 8, 13, 0), this_fortnight.begin
51
50
  assert_equal Time.local(2006, 8, 16, 14), this_fortnight.end
52
51
  end
53
-
52
+
54
53
  def test_offset
55
54
  span = Chronic::Span.new(@now, @now + 1)
56
-
55
+
57
56
  offset_span = Chronic::RepeaterWeek.new(:week).offset(span, 3, :future)
58
-
57
+
59
58
  assert_equal Time.local(2006, 9, 6, 14), offset_span.begin
60
59
  assert_equal Time.local(2006, 9, 6, 14, 0, 1), offset_span.end
61
60
  end
62
-
63
- end
61
+
62
+ end
@@ -1,8 +1,7 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class TestRepeaterHour < Test::Unit::TestCase
5
-
4
+
6
5
  def setup
7
6
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
8
7
  end
@@ -10,56 +9,56 @@ class TestRepeaterHour < Test::Unit::TestCase
10
9
  def test_next_future
11
10
  hours = Chronic::RepeaterHour.new(:hour)
12
11
  hours.start = @now
13
-
12
+
14
13
  next_hour = hours.next(:future)
15
14
  assert_equal Time.local(2006, 8, 16, 15), next_hour.begin
16
15
  assert_equal Time.local(2006, 8, 16, 16), next_hour.end
17
-
16
+
18
17
  next_next_hour = hours.next(:future)
19
18
  assert_equal Time.local(2006, 8, 16, 16), next_next_hour.begin
20
19
  assert_equal Time.local(2006, 8, 16, 17), next_next_hour.end
21
20
  end
22
-
21
+
23
22
  def test_next_past
24
23
  hours = Chronic::RepeaterHour.new(:hour)
25
24
  hours.start = @now
26
-
25
+
27
26
  past_hour = hours.next(:past)
28
27
  assert_equal Time.local(2006, 8, 16, 13), past_hour.begin
29
28
  assert_equal Time.local(2006, 8, 16, 14), past_hour.end
30
-
29
+
31
30
  past_past_hour = hours.next(:past)
32
31
  assert_equal Time.local(2006, 8, 16, 12), past_past_hour.begin
33
32
  assert_equal Time.local(2006, 8, 16, 13), past_past_hour.end
34
33
  end
35
-
34
+
36
35
  def test_this
37
36
  @now = Time.local(2006, 8, 16, 14, 30)
38
-
37
+
39
38
  hours = Chronic::RepeaterHour.new(:hour)
40
39
  hours.start = @now
41
-
40
+
42
41
  this_hour = hours.this(:future)
43
42
  assert_equal Time.local(2006, 8, 16, 14, 31), this_hour.begin
44
43
  assert_equal Time.local(2006, 8, 16, 15), this_hour.end
45
-
44
+
46
45
  this_hour = hours.this(:past)
47
46
  assert_equal Time.local(2006, 8, 16, 14), this_hour.begin
48
47
  assert_equal Time.local(2006, 8, 16, 14, 30), this_hour.end
49
48
  end
50
-
49
+
51
50
  def test_offset
52
51
  span = Chronic::Span.new(@now, @now + 1)
53
-
52
+
54
53
  offset_span = Chronic::RepeaterHour.new(:hour).offset(span, 3, :future)
55
-
54
+
56
55
  assert_equal Time.local(2006, 8, 16, 17), offset_span.begin
57
56
  assert_equal Time.local(2006, 8, 16, 17, 0, 1), offset_span.end
58
-
57
+
59
58
  offset_span = Chronic::RepeaterHour.new(:hour).offset(span, 24, :past)
60
-
59
+
61
60
  assert_equal Time.local(2006, 8, 15, 14), offset_span.begin
62
61
  assert_equal Time.local(2006, 8, 15, 14, 0, 1), offset_span.end
63
62
  end
64
-
65
- end
63
+
64
+ end
@@ -0,0 +1,34 @@
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
2
+
3
+ class TestRepeaterMinute < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @now = Time.local(2008, 6, 25, 7, 15, 30, 0)
7
+ end
8
+
9
+ def test_next_future
10
+ minutes = Chronic::RepeaterMinute.new(:minute)
11
+ minutes.start = @now
12
+
13
+ next_minute = minutes.next(:future)
14
+ assert_equal Time.local(2008, 6, 25, 7, 16), next_minute.begin
15
+ assert_equal Time.local(2008, 6, 25, 7, 17), next_minute.end
16
+
17
+ next_next_minute = minutes.next(:future)
18
+ assert_equal Time.local(2008, 6, 25, 7, 17), next_next_minute.begin
19
+ assert_equal Time.local(2008, 6, 25, 7, 18), next_next_minute.end
20
+ end
21
+
22
+ def test_next_past
23
+ minutes = Chronic::RepeaterMinute.new(:minute)
24
+ minutes.start = @now
25
+
26
+ prev_minute = minutes.next(:past)
27
+ assert_equal Time.local(2008, 6, 25, 7, 14), prev_minute.begin
28
+ assert_equal Time.local(2008, 6, 25, 7, 15), prev_minute.end
29
+
30
+ prev_prev_minute = minutes.next(:past)
31
+ assert_equal Time.local(2008, 6, 25, 7, 13), prev_prev_minute.begin
32
+ assert_equal Time.local(2008, 6, 25, 7, 14), prev_prev_minute.end
33
+ end
34
+ end
@@ -1,47 +1,46 @@
1
- require 'chronic'
2
- require 'test/unit'
1
+ require File.join(File.dirname(__FILE__), *%w[helper])
3
2
 
4
3
  class TestRepeaterMonth < Test::Unit::TestCase
5
-
4
+
6
5
  def setup
7
6
  # Wed Aug 16 14:00:00 2006
8
7
  @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
9
8
  end
10
-
9
+
11
10
  def test_offset_by
12
11
  # future
13
-
12
+
14
13
  time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 1, :future)
15
14
  assert_equal Time.local(2006, 9, 16, 14), time
16
-
15
+
17
16
  time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 5, :future)
18
17
  assert_equal Time.local(2007, 1, 16, 14), time
19
-
18
+
20
19
  # past
21
-
20
+
22
21
  time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 1, :past)
23
22
  assert_equal Time.local(2006, 7, 16, 14), time
24
-
23
+
25
24
  time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 10, :past)
26
25
  assert_equal Time.local(2005, 10, 16, 14), time
27
26
  end
28
-
27
+
29
28
  def test_offset
30
29
  # future
31
-
30
+
32
31
  span = Chronic::Span.new(@now, @now + 60)
33
32
  offset_span = Chronic::RepeaterMonth.new(:month).offset(span, 1, :future)
34
-
33
+
35
34
  assert_equal Time.local(2006, 9, 16, 14), offset_span.begin
36
35
  assert_equal Time.local(2006, 9, 16, 14, 1), offset_span.end
37
-
36
+
38
37
  # past
39
-
38
+
40
39
  span = Chronic::Span.new(@now, @now + 60)
41
40
  offset_span = Chronic::RepeaterMonth.new(:month).offset(span, 1, :past)
42
-
41
+
43
42
  assert_equal Time.local(2006, 7, 16, 14), offset_span.begin
44
43
  assert_equal Time.local(2006, 7, 16, 14, 1), offset_span.end
45
44
  end
46
-
47
- end
45
+
46
+ end