chronic 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +5 -0
  3. data/.yardopts +3 -0
  4. data/HISTORY.md +49 -2
  5. data/README.md +8 -2
  6. data/Rakefile +9 -101
  7. data/chronic.gemspec +12 -78
  8. data/lib/chronic/chronic.rb +260 -101
  9. data/lib/chronic/grabber.rb +12 -3
  10. data/lib/chronic/handler.rb +90 -0
  11. data/lib/chronic/handlers.rb +189 -301
  12. data/lib/chronic/mini_date.rb +15 -5
  13. data/lib/chronic/numerizer.rb +3 -2
  14. data/lib/chronic/ordinal.rb +15 -4
  15. data/lib/chronic/pointer.rb +13 -5
  16. data/lib/chronic/repeater.rb +31 -15
  17. data/lib/chronic/repeaters/repeater_day.rb +6 -7
  18. data/lib/chronic/repeaters/repeater_day_name.rb +1 -2
  19. data/lib/chronic/repeaters/repeater_day_portion.rb +8 -9
  20. data/lib/chronic/repeaters/repeater_fortnight.rb +2 -3
  21. data/lib/chronic/repeaters/repeater_hour.rb +7 -8
  22. data/lib/chronic/repeaters/repeater_minute.rb +6 -7
  23. data/lib/chronic/repeaters/repeater_month.rb +22 -11
  24. data/lib/chronic/repeaters/repeater_month_name.rb +16 -24
  25. data/lib/chronic/repeaters/repeater_season.rb +10 -29
  26. data/lib/chronic/repeaters/repeater_season_name.rb +2 -4
  27. data/lib/chronic/repeaters/repeater_second.rb +0 -1
  28. data/lib/chronic/repeaters/repeater_time.rb +1 -2
  29. data/lib/chronic/repeaters/repeater_week.rb +0 -1
  30. data/lib/chronic/repeaters/repeater_weekday.rb +1 -2
  31. data/lib/chronic/repeaters/repeater_weekend.rb +0 -1
  32. data/lib/chronic/repeaters/repeater_year.rb +29 -18
  33. data/lib/chronic/scalar.rb +29 -1
  34. data/lib/chronic/season.rb +37 -0
  35. data/lib/chronic/separator.rb +24 -7
  36. data/lib/chronic/tag.rb +12 -1
  37. data/lib/chronic/time_zone.rb +12 -3
  38. data/lib/chronic/token.rb +14 -4
  39. data/lib/chronic.rb +66 -89
  40. data/test/test_Chronic.rb +97 -3
  41. data/test/test_DaylightSavings.rb +1 -1
  42. data/test/test_Handler.rb +1 -6
  43. data/test/test_MiniDate.rb +3 -3
  44. data/test/test_Numerizer.rb +1 -1
  45. data/test/test_RepeaterDayName.rb +1 -1
  46. data/test/test_RepeaterFortnight.rb +1 -1
  47. data/test/test_RepeaterHour.rb +1 -1
  48. data/test/test_RepeaterMinute.rb +1 -1
  49. data/test/test_RepeaterMonth.rb +5 -1
  50. data/test/test_RepeaterMonthName.rb +1 -1
  51. data/test/test_RepeaterSeason.rb +40 -0
  52. data/test/test_RepeaterTime.rb +1 -1
  53. data/test/test_RepeaterWeek.rb +1 -1
  54. data/test/test_RepeaterWeekday.rb +1 -1
  55. data/test/test_RepeaterWeekend.rb +1 -1
  56. data/test/test_RepeaterYear.rb +8 -1
  57. data/test/test_Span.rb +1 -1
  58. data/test/test_Token.rb +1 -1
  59. data/test/test_parsing.rb +167 -115
  60. metadata +32 -32
  61. data/Manifest.txt +0 -59
  62. data/benchmark/benchmark.rb +0 -13
  63. data/test/test_Time.rb +0 -49
data/test/test_Chronic.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestChronic < Test::Unit::TestCase
4
4
 
@@ -21,7 +21,7 @@ class TestChronic < Test::Unit::TestCase
21
21
 
22
22
  assert_equal :morning, tokens[1].tags[0].type
23
23
 
24
- tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
24
+ tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
25
25
 
26
26
  assert_equal :am, tokens[1].tags[0].type
27
27
  assert_equal 2, tokens.size
@@ -34,7 +34,7 @@ class TestChronic < Test::Unit::TestCase
34
34
 
35
35
  assert_equal :morning, tokens[1].tags[0].type
36
36
 
37
- tokens = Chronic.dealias_and_disambiguate_times(tokens, {})
37
+ tokens = Chronic::Handlers.dealias_and_disambiguate_times(tokens, {})
38
38
 
39
39
  assert_equal :morning, tokens[1].tags[0].type
40
40
  assert_equal 2, tokens.size
@@ -51,4 +51,98 @@ class TestChronic < Test::Unit::TestCase
51
51
  assert_equal Time.local(2006, 11, 16), Chronic.guess(span)
52
52
  end
53
53
 
54
+ def test_now
55
+ Chronic.parse('now', :now => Time.local(2006, 01))
56
+ assert_equal Time.local(2006, 01), Chronic.now
57
+
58
+ Chronic.parse('now', :now => Time.local(2007, 01))
59
+ assert_equal Time.local(2007, 01), Chronic.now
60
+ end
61
+
62
+ def test_endian_definitions
63
+ # middle, little
64
+ endians = [
65
+ Chronic::Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
66
+ Chronic::Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
67
+ ]
68
+
69
+ assert_equal endians, Chronic.definitions[:endian]
70
+
71
+ defs = Chronic.definitions(:endian_precedence => :little)
72
+ assert_equal endians.reverse, defs[:endian]
73
+
74
+ defs = Chronic.definitions(:endian_precedence => [:little, :middle])
75
+ assert_equal endians.reverse, defs[:endian]
76
+
77
+ assert_raises(ArgumentError) do
78
+ Chronic.definitions(:endian_precedence => :invalid)
79
+ end
80
+ end
81
+
82
+ def test_passing_options
83
+ assert_raises(ArgumentError) do
84
+ Chronic.parse('now', :invalid => :option)
85
+ end
86
+
87
+ assert_raises(ArgumentError) do
88
+ Chronic.parse('now', :context => :invalid_context)
89
+ end
90
+ end
91
+
92
+ def test_debug
93
+ require 'stringio'
94
+ $stdout = StringIO.new
95
+ Chronic.debug = true
96
+
97
+ Chronic.parse 'now'
98
+ assert $stdout.string.include?('this(grabber-this)')
99
+ ensure
100
+ $stdout = STDOUT
101
+ Chronic.debug = false
102
+ end
103
+
104
+ # Chronic.construct
105
+
106
+ def test_normal
107
+ assert_equal Time.local(2006, 1, 2, 0, 0, 0), Chronic.construct(2006, 1, 2, 0, 0, 0)
108
+ assert_equal Time.local(2006, 1, 2, 3, 0, 0), Chronic.construct(2006, 1, 2, 3, 0, 0)
109
+ assert_equal Time.local(2006, 1, 2, 3, 4, 0), Chronic.construct(2006, 1, 2, 3, 4, 0)
110
+ assert_equal Time.local(2006, 1, 2, 3, 4, 5), Chronic.construct(2006, 1, 2, 3, 4, 5)
111
+ end
112
+
113
+ def test_second_overflow
114
+ assert_equal Time.local(2006, 1, 1, 0, 1, 30), Chronic.construct(2006, 1, 1, 0, 0, 90)
115
+ assert_equal Time.local(2006, 1, 1, 0, 5, 0), Chronic.construct(2006, 1, 1, 0, 0, 300)
116
+ end
117
+
118
+ def test_minute_overflow
119
+ assert_equal Time.local(2006, 1, 1, 1, 30), Chronic.construct(2006, 1, 1, 0, 90)
120
+ assert_equal Time.local(2006, 1, 1, 5), Chronic.construct(2006, 1, 1, 0, 300)
121
+ end
122
+
123
+ def test_hour_overflow
124
+ assert_equal Time.local(2006, 1, 2, 12), Chronic.construct(2006, 1, 1, 36)
125
+ assert_equal Time.local(2006, 1, 7), Chronic.construct(2006, 1, 1, 144)
126
+ end
127
+
128
+ def test_day_overflow
129
+ assert_equal Time.local(2006, 2, 1), Chronic.construct(2006, 1, 32)
130
+ assert_equal Time.local(2006, 3, 5), Chronic.construct(2006, 2, 33)
131
+ assert_equal Time.local(2004, 3, 4), Chronic.construct(2004, 2, 33)
132
+ assert_equal Time.local(2000, 3, 4), Chronic.construct(2000, 2, 33)
133
+
134
+ assert_nothing_raised do
135
+ Chronic.construct(2006, 1, 56)
136
+ end
137
+
138
+ assert_raise(RuntimeError) do
139
+ Chronic.construct(2006, 1, 57)
140
+ end
141
+ end
142
+
143
+ def test_month_overflow
144
+ assert_equal Time.local(2006, 1), Chronic.construct(2005, 13)
145
+ assert_equal Time.local(2005, 12), Chronic.construct(2000, 72)
146
+ end
147
+
54
148
  end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestDaylightSavings < Test::Unit::TestCase
4
4
 
data/test/test_Handler.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestHandler < Test::Unit::TestCase
4
4
 
@@ -101,9 +101,4 @@ class TestHandler < Test::Unit::TestCase
101
101
  assert handler.match(tokens, Chronic.definitions)
102
102
  end
103
103
 
104
- def test_constantize
105
- handler = Chronic::Handler.new([], :handler)
106
- assert_equal Chronic::RepeaterTime, handler.constantize(:repeater_time)
107
- end
108
-
109
104
  end
@@ -1,9 +1,9 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestMiniDate < Test::Unit::TestCase
4
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) }
5
+ assert_raise(ArgumentError){ Chronic::MiniDate.new(0,12) }
6
+ assert_raise(ArgumentError){ Chronic::MiniDate.new(13,1) }
7
7
  end
8
8
 
9
9
  def test_is_between
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class ParseNumbersTest < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterDayName < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterFortnight < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterHour < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterMinute < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterMonth < Test::Unit::TestCase
4
4
 
@@ -23,6 +23,10 @@ class TestRepeaterMonth < Test::Unit::TestCase
23
23
 
24
24
  time = Chronic::RepeaterMonth.new(:month).offset_by(@now, 10, :past)
25
25
  assert_equal Time.local(2005, 10, 16, 14), time
26
+
27
+ time = Chronic::RepeaterMonth.new(:month).offset_by(Time.local(2010, 3, 29), 1, :past)
28
+ assert_equal 2, time.month
29
+ assert_equal 28, time.day
26
30
  end
27
31
 
28
32
  def test_offset
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterMonthName < Test::Unit::TestCase
4
4
 
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ class TestRepeaterSeason < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @now = Time.local(2006, 8, 16, 14, 0, 0, 0)
7
+ end
8
+
9
+ def test_next_future
10
+ seasons = Chronic::RepeaterSeason.new(:season)
11
+ seasons.start = @now
12
+
13
+ next_season = seasons.next(:future)
14
+ assert_equal Time.local(2006, 9, 23), next_season.begin
15
+ assert_equal Time.local(2006, 12, 21), next_season.end
16
+ end
17
+
18
+ def test_next_past
19
+ seasons = Chronic::RepeaterSeason.new(:season)
20
+ seasons.start = @now
21
+
22
+ last_season = seasons.next(:past)
23
+ assert_equal Time.local(2006, 3, 20), last_season.begin
24
+ assert_equal Time.local(2006, 6, 20), last_season.end
25
+ end
26
+
27
+ def test_this
28
+ seasons = Chronic::RepeaterSeason.new(:season)
29
+ seasons.start = @now
30
+
31
+ this_season = seasons.this(:future)
32
+ assert_equal Time.local(2006, 8, 17), this_season.begin
33
+ assert_equal Time.local(2006, 9, 22), this_season.end
34
+
35
+ this_season = seasons.this(:past)
36
+ assert_equal Time.local(2006, 6, 21), this_season.begin
37
+ assert_equal Time.local(2006, 8, 16), this_season.end
38
+ end
39
+
40
+ end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterTime < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterWeek < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterWeekday < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterWeekend < Test::Unit::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestRepeaterYear < Test::Unit::TestCase
4
4
 
@@ -57,6 +57,13 @@ class TestRepeaterYear < Test::Unit::TestCase
57
57
 
58
58
  assert_equal Time.local(1996, 8, 16, 14), offset_span.begin
59
59
  assert_equal Time.local(1996, 8, 16, 14, 0, 1), offset_span.end
60
+
61
+ now = Time.local(2008, 2, 29)
62
+ span = Chronic::Span.new(now, now + 1)
63
+ offset_span = Chronic::RepeaterYear.new(:year).offset(span, 1, :past)
64
+
65
+ assert_equal Time.local(2007, 2, 28), offset_span.begin
66
+ assert_equal Time.local(2007, 2, 28, 0, 0, 1), offset_span.end
60
67
  end
61
68
 
62
69
  end
data/test/test_Span.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestSpan < Test::Unit::TestCase
4
4
 
data/test/test_Token.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), *%w[helper])
1
+ require 'helper'
2
2
 
3
3
  class TestToken < Test::Unit::TestCase
4
4