chronic 0.6.5 → 0.8.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 (44) hide show
  1. data/.gitignore +1 -0
  2. data/HISTORY.md +25 -0
  3. data/README.md +5 -0
  4. data/chronic.gemspec +3 -0
  5. data/lib/chronic/chronic.rb +82 -67
  6. data/lib/chronic/grabber.rb +9 -7
  7. data/lib/chronic/handler.rb +10 -12
  8. data/lib/chronic/handlers.rb +84 -11
  9. data/lib/chronic/ordinal.rb +12 -9
  10. data/lib/chronic/pointer.rb +9 -7
  11. data/lib/chronic/repeater.rb +30 -20
  12. data/lib/chronic/repeaters/repeater_day_portion.rb +25 -11
  13. data/lib/chronic/scalar.rb +30 -23
  14. data/lib/chronic/season.rb +1 -12
  15. data/lib/chronic/separator.rb +21 -15
  16. data/lib/chronic/tag.rb +5 -11
  17. data/lib/chronic/time_zone.rb +9 -7
  18. data/lib/chronic/token.rb +12 -10
  19. data/lib/chronic.rb +50 -44
  20. data/test/helper.rb +7 -1
  21. data/test/{test_Chronic.rb → test_chronic.rb} +8 -6
  22. data/test/{test_DaylightSavings.rb → test_daylight_savings.rb} +1 -1
  23. data/test/{test_Handler.rb → test_handler.rb} +1 -1
  24. data/test/{test_MiniDate.rb → test_mini_date.rb} +9 -9
  25. data/test/{test_Numerizer.rb → test_numerizer.rb} +1 -1
  26. data/test/test_parsing.rb +149 -10
  27. data/test/{test_RepeaterDayName.rb → test_repeater_day_name.rb} +1 -1
  28. data/test/test_repeater_day_portion.rb +254 -0
  29. data/test/{test_RepeaterFortnight.rb → test_repeater_fortnight.rb} +1 -1
  30. data/test/{test_RepeaterHour.rb → test_repeater_hour.rb} +1 -1
  31. data/test/{test_RepeaterMinute.rb → test_repeater_minute.rb} +1 -1
  32. data/test/{test_RepeaterMonth.rb → test_repeater_month.rb} +1 -1
  33. data/test/{test_RepeaterMonthName.rb → test_repeater_month_name.rb} +1 -1
  34. data/test/{test_RepeaterSeason.rb → test_repeater_season.rb} +1 -1
  35. data/test/{test_RepeaterTime.rb → test_repeater_time.rb} +1 -1
  36. data/test/{test_RepeaterWeek.rb → test_repeater_week.rb} +1 -1
  37. data/test/{test_RepeaterWeekday.rb → test_repeater_weekday.rb} +1 -1
  38. data/test/{test_RepeaterWeekend.rb → test_repeater_weekend.rb} +1 -1
  39. data/test/{test_RepeaterYear.rb → test_repeater_year.rb} +1 -1
  40. data/test/{test_Span.rb → test_span.rb} +1 -1
  41. data/test/{test_Token.rb → test_token.rb} +1 -1
  42. metadata +76 -44
  43. data/.gemtest +0 -0
  44. data/.yardopts +0 -3
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg
3
3
  rdoc
4
4
  .yardoc
5
5
  doc
6
+ tags
data/HISTORY.md CHANGED
@@ -1,3 +1,28 @@
1
+ # 0.8.0 / 2012-09-16
2
+
3
+ * Support parsing "<ordinal> of this month" (#109)
4
+ * Support parsing ISO 8601 format (#115)
5
+ * Support parsing "on <day>" without a timestamp (#117)
6
+ * Fix time parsing regexp (#125)
7
+ * Support time when parsing dd-mm-yyy <time> (#126)
8
+ * Allow anchor handler to accept any separators (at, on) (#128)
9
+ * Support parsing EXIF date format (#112)
10
+ * Start using minitest for testing
11
+ * Ensure periods are interpreted as colons (#81).
12
+ * Support month/day and day/month parsing (#59).
13
+ * Support day(scalar)-month(name)-year(scalar) (#99).
14
+ * Handle text starting with 'a' or 'an' (#101, @steveburkett).
15
+ * Ensure post medium timestamps are correctly formatted (#89)
16
+
17
+ # 0.6.7 / 2012-01-31
18
+
19
+ * Handle day, month names with scalar day and year (Joe Fiorini)
20
+ * Ensure 31st parses correctly with day names (Joe Fiorini)
21
+
22
+ # 0.6.6 / 2011-11-23
23
+
24
+ * `Chronic.parse('thur')` no longer returns `nil` (@harold)
25
+
1
26
  # 0.6.5 / 2011-11-04
2
27
 
3
28
  * Fix bug when parsing ordinal repeaters (#73)
data/README.md CHANGED
@@ -46,6 +46,10 @@ You can parse strings containing a natural language date using the
46
46
 
47
47
  Chronic.parse('may 27th', :guess => false)
48
48
  #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
49
+
50
+ Chronic.parse('6/4/2012', :endian_precedence => :little)
51
+ #=> Fri Apr 06 00:00:00 PDT 2012
52
+
49
53
 
50
54
  See `Chronic.parse` for detailed usage instructions.
51
55
 
@@ -86,6 +90,7 @@ Simple
86
90
  Complex
87
91
 
88
92
  * 3 years ago
93
+ * a year ago
89
94
  * 5 months before now
90
95
  * 7 hours ago
91
96
  * 7 days from now
data/chronic.gemspec CHANGED
@@ -14,4 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.extra_rdoc_files = %w[README.md HISTORY.md LICENSE]
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- test`.split("\n")
17
+
18
+ s.add_development_dependency 'rake'
19
+ s.add_development_dependency 'minitest'
17
20
  end
@@ -1,5 +1,6 @@
1
1
  module Chronic
2
2
 
3
+ # Returns a Hash of default configuration options.
3
4
  DEFAULT_OPTIONS = {
4
5
  :context => :future,
5
6
  :now => nil,
@@ -11,53 +12,43 @@ module Chronic
11
12
 
12
13
  class << self
13
14
 
14
- # Parses a string containing a natural language date or time
15
+ # Parses a string containing a natural language date or time.
15
16
  #
16
17
  # If the parser can find a date or time, either a Time or Chronic::Span
17
18
  # will be returned (depending on the value of `:guess`). If no
18
- # date or time can be found, `nil` will be returned
19
+ # date or time can be found, `nil` will be returned.
19
20
  #
20
- # @param [String] text The text to parse
21
+ # text - The String text to parse.
22
+ # opts - An optional Hash of configuration options:
23
+ # :context - If your string represents a birthday, you can set
24
+ # this value to :past and if an ambiguous string is
25
+ # given, it will assume it is in the past.
26
+ # :now - Time, all computations will be based off of time
27
+ # instead of Time.now.
28
+ # :guess - By default the parser will guess a single point in time
29
+ # for the given date or time. If you'd rather have the
30
+ # entire time span returned, set this to false
31
+ # and a Chronic::Span will be returned.
32
+ # :ambiguous_time_range - If an Integer is given, ambiguous times
33
+ # (like 5:00) will be assumed to be within the range of
34
+ # that time in the AM to that time in the PM. For
35
+ # example, if you set it to `7`, then the parser will
36
+ # look for the time between 7am and 7pm. In the case of
37
+ # 5:00, it would assume that means 5:00pm. If `:none`
38
+ # is given, no assumption will be made, and the first
39
+ # matching instance of that time will be used.
40
+ # :endian_precedence - By default, Chronic will parse "03/04/2011"
41
+ # as the fourth day of the third month. Alternatively you
42
+ # can tell Chronic to parse this as the third day of the
43
+ # fourth month by setting this to [:little, :middle].
44
+ # :ambiguous_year_future_bias - When parsing two digit years
45
+ # (ie 79) unlike Rubys Time class, Chronic will attempt
46
+ # to assume the full year using this figure. Chronic will
47
+ # look x amount of years into the future and past. If the
48
+ # two digit year is `now + x years` it's assumed to be the
49
+ # future, `now - x years` is assumed to be the past.
21
50
  #
22
- # @option opts [Symbol] :context (:future)
23
- # * If your string represents a birthday, you can set `:context` to
24
- # `:past` and if an ambiguous string is given, it will assume it is
25
- # in the past. Specify `:future` or omit to set a future context.
26
- #
27
- # @option opts [Object] :now (Time.now)
28
- # * By setting `:now` to a Time, all computations will be based off of
29
- # that time instead of `Time.now`. If set to nil, Chronic will use
30
- # `Time.now`
31
- #
32
- # @option opts [Boolean] :guess (true)
33
- # * By default, the parser will guess a single point in time for the
34
- # given date or time. If you'd rather have the entire time span
35
- # returned, set `:guess` to `false` and a {Chronic::Span} will
36
- # be returned
37
- #
38
- # @option opts [Integer] :ambiguous_time_range (6)
39
- # * If an Integer is given, ambiguous times (like 5:00) will be
40
- # assumed to be within the range of that time in the AM to that time
41
- # in the PM. For example, if you set it to `7`, then the parser
42
- # will look for the time between 7am and 7pm. In the case of 5:00, it
43
- # would assume that means 5:00pm. If `:none` is given, no
44
- # assumption will be made, and the first matching instance of that
45
- # time will be used
46
- #
47
- # @option opts [Array] :endian_precedence ([:middle, :little])
48
- # * By default, Chronic will parse "03/04/2011" as the fourth day
49
- # of the third month. Alternatively you can tell Chronic to parse
50
- # this as the third day of the fourth month by altering the
51
- # `:endian_precedence` to `[:little, :middle]`
52
- #
53
- # @option opts [Integer] :ambiguous_year_future_bias (50)
54
- # * When parsing two digit years (ie 79) unlike Rubys Time class,
55
- # Chronic will attempt to assume the full year using this figure.
56
- # Chronic will look x amount of years into the future and past. If
57
- # the two digit year is `now + x years` it's assumed to be the
58
- # future, `now - x years` is assumed to be the past
59
- #
60
- # @return [Time, Chronic::Span, nil]
51
+ # Returns a new Time object, or Chronic::Span if :guess option is false.
61
52
  def parse(text, opts={})
62
53
  options = DEFAULT_OPTIONS.merge opts
63
54
 
@@ -87,14 +78,17 @@ module Chronic
87
78
  end
88
79
  end
89
80
 
90
- # Clean up the specified text ready for parsing
81
+ # Clean up the specified text ready for parsing.
91
82
  #
92
83
  # Clean up the string by stripping unwanted characters, converting
93
84
  # idioms to their canonical form, converting number words to numbers
94
85
  # (three => 3), and converting ordinal words to numeric
95
86
  # ordinals (third => 3rd)
96
87
  #
97
- # @example
88
+ # text - The String text to normalize.
89
+ #
90
+ # Examples:
91
+ #
98
92
  # Chronic.pre_normalize('first day in May')
99
93
  # #=> "1st day in may"
100
94
  #
@@ -104,17 +98,18 @@ module Chronic
104
98
  # Chronic.pre_normalize('one hundred and thirty six days from now')
105
99
  # #=> "136 days future this second"
106
100
  #
107
- # @param [String] text The string to normalize
108
- # @return [String] A new string ready for Chronic to parse
101
+ # Returns a new String ready for Chronic to parse.
109
102
  def pre_normalize(text)
110
103
  text = text.to_s.downcase
111
- text.gsub!(/['"\.]/, '')
104
+ text.gsub!(/\./, ':')
105
+ text.gsub!(/['"]/, '')
112
106
  text.gsub!(/,/, ' ')
107
+ text.gsub!(/^second /, '2nd ')
113
108
  text.gsub!(/\bsecond (of|day|month|hour|minute|second)\b/, '2nd \1')
114
109
  text = Numerizer.numerize(text)
115
110
  text.gsub!(/ \-(\d{4})\b/, ' tzminus\1')
116
111
  text.gsub!(/([\/\-\,\@])/) { ' ' + $1 + ' ' }
117
- text.gsub!(/(?:^|\s)0(\d+:\d+\s*pm?\b)/, '\1')
112
+ text.gsub!(/(?:^|\s)0(\d+:\d+\s*pm?\b)/, ' \1')
118
113
  text.gsub!(/\btoday\b/, 'this day')
119
114
  text.gsub!(/\btomm?orr?ow\b/, 'next day')
120
115
  text.gsub!(/\byesterday\b/, 'last day')
@@ -129,23 +124,26 @@ module Chronic
129
124
  text.gsub!(/\b\d+:?\d*[ap]\b/,'\0m')
130
125
  text.gsub!(/(\d)([ap]m|oclock)\b/, '\1 \2')
131
126
  text.gsub!(/\b(hence|after|from)\b/, 'future')
127
+ text.gsub!(/^\s?an? /i, '1 ')
128
+ text.gsub!(/\b(\d{4}):(\d{2}):(\d{2})\b/, '\1 / \2 / \3') # DTOriginal
132
129
  text
133
130
  end
134
131
 
135
- # Convert number words to numbers (three => 3, fourth => 4th)
132
+ # Convert number words to numbers (three => 3, fourth => 4th).
133
+ #
134
+ # text - The String to convert.
136
135
  #
137
- # @see Numerizer.numerize
138
- # @param [String] text The string to convert
139
- # @return [String] A new string with words converted to numbers
136
+ # Returns a new String with words converted to numbers.
140
137
  def numericize_numbers(text)
141
138
  warn "Chronic.numericize_numbers will be deprecated in version 0.7.0. Please use Chronic::Numerizer.numerize instead"
142
139
  Numerizer.numerize(text)
143
140
  end
144
141
 
145
- # Guess a specific time within the given span
142
+ # Guess a specific time within the given span.
143
+ #
144
+ # span - The Chronic::Span object to calcuate a guess from.
146
145
  #
147
- # @param [Span] span
148
- # @return [Time]
146
+ # Returns a new Time object.
149
147
  def guess(span)
150
148
  if span.width > 1
151
149
  span.begin + (span.width / 2)
@@ -154,11 +152,13 @@ module Chronic
154
152
  end
155
153
  end
156
154
 
157
- # List of {Handler} definitions. See {parse} for a list of options this
158
- # method accepts
155
+ # List of Handler definitions. See #parse for a list of options this
156
+ # method accepts.
159
157
  #
160
- # @see parse
161
- # @return [Hash] A Hash of Handler definitions
158
+ # options - An optional Hash of configuration options:
159
+ # :endian_precedence -
160
+ #
161
+ # Returns A Hash of Handler definitions.
162
162
  def definitions(options={})
163
163
  options[:endian_precedence] ||= [:middle, :little]
164
164
 
@@ -168,10 +168,11 @@ module Chronic
168
168
  ],
169
169
 
170
170
  :date => [
171
- Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :repeater_time, :separator_slash_or_dash?, :time_zone, :scalar_year], :handle_rdn_rmn_sd_t_tz_sy),
171
+ Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :repeater_time, :separator_slash_or_dash?, :time_zone, :scalar_year], :handle_generic),
172
172
  Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day], :handle_rdn_rmn_sd),
173
+ Handler.new([:repeater_day_name, :repeater_month_name, :scalar_day, :scalar_year], :handle_rdn_rmn_sd_sy),
173
174
  Handler.new([:repeater_day_name, :repeater_month_name, :ordinal_day], :handle_rdn_rmn_od),
174
- Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :repeater_time, :time_zone], :handle_sy_sm_sd_t_tz),
175
+ Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :repeater_time, :time_zone], :handle_generic),
175
176
  Handler.new([:repeater_month_name, :scalar_day, :scalar_year], :handle_rmn_sd_sy),
176
177
  Handler.new([:repeater_month_name, :ordinal_day, :scalar_year], :handle_rmn_od_sy),
177
178
  Handler.new([:repeater_month_name, :scalar_day, :scalar_year, :separator_at?, 'time?'], :handle_rmn_sd_sy),
@@ -181,19 +182,23 @@ module Chronic
181
182
  Handler.new([:repeater_month_name, :ordinal_day, :separator_at?, 'time?'], :handle_rmn_od),
182
183
  Handler.new([:ordinal_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_od_rmn_sy),
183
184
  Handler.new([:ordinal_day, :repeater_month_name, :separator_at?, 'time?'], :handle_od_rmn),
185
+ Handler.new([:ordinal_day, :grabber?, :repeater_month, :separator_at?, 'time?'], :handle_od_rm),
184
186
  Handler.new([:scalar_year, :repeater_month_name, :ordinal_day], :handle_sy_rmn_od),
185
187
  Handler.new([:repeater_time, :repeater_day_portion?, :separator_on?, :repeater_month_name, :ordinal_day], :handle_rmn_od_on),
186
188
  Handler.new([:repeater_month_name, :scalar_year], :handle_rmn_sy),
187
189
  Handler.new([:scalar_day, :repeater_month_name, :scalar_year, :separator_at?, 'time?'], :handle_sd_rmn_sy),
188
190
  Handler.new([:scalar_day, :repeater_month_name, :separator_at?, 'time?'], :handle_sd_rmn),
189
191
  Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sy_sm_sd),
190
- Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_year], :handle_sm_sy)
192
+ Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_year], :handle_sm_sy),
193
+ Handler.new([:scalar_day, :separator_slash_or_dash, :repeater_month_name, :separator_slash_or_dash, :scalar_year, :repeater_time?], :handle_sm_rmn_sy),
194
+ Handler.new([:scalar_year, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :time_zone], :handle_generic)
195
+
191
196
  ],
192
197
 
193
198
  # tonight at 7pm
194
199
  :anchor => [
195
- Handler.new([:grabber?, :repeater, :separator_at?, :repeater?, :repeater?], :handle_r),
196
- Handler.new([:grabber?, :repeater, :repeater, :separator_at?, :repeater?, :repeater?], :handle_r),
200
+ Handler.new([:separator_on?, :grabber?, :repeater, :separator_at?, :repeater?, :repeater?], :handle_r),
201
+ Handler.new([:grabber?, :repeater, :repeater, :separator?, :repeater?, :repeater?], :handle_r),
197
202
  Handler.new([:repeater, :grabber, :repeater], :handle_r_g_r)
198
203
  ],
199
204
 
@@ -201,7 +206,7 @@ module Chronic
201
206
  :arrow => [
202
207
  Handler.new([:scalar, :repeater, :pointer], :handle_s_r_p),
203
208
  Handler.new([:pointer, :scalar, :repeater], :handle_p_s_r),
204
- Handler.new([:scalar, :repeater, :pointer, 'anchor'], :handle_s_r_p_a)
209
+ Handler.new([:scalar, :repeater, :pointer, :separator_at?, 'anchor'], :handle_s_r_p_a)
205
210
  ],
206
211
 
207
212
  # 3rd week in march
@@ -213,6 +218,8 @@ module Chronic
213
218
 
214
219
  endians = [
215
220
  Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sm_sd_sy),
221
+ Handler.new([:scalar_month, :separator_slash_or_dash, :scalar_day, :separator_at?, 'time?'], :handle_sm_sd),
222
+ Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_at?, 'time?'], :handle_sd_sm),
216
223
  Handler.new([:scalar_day, :separator_slash_or_dash, :scalar_month, :separator_slash_or_dash, :scalar_year, :separator_at?, 'time?'], :handle_sd_sm_sy)
217
224
  ]
218
225
 
@@ -228,9 +235,17 @@ module Chronic
228
235
  @definitions
229
236
  end
230
237
 
231
- # Construct a time Object
238
+ # Construct a new time object determining possible month overflows
239
+ # and leap years.
240
+ #
241
+ # year - Integer year.
242
+ # month - Integer month.
243
+ # day - Integer day.
244
+ # hour - Integer hour.
245
+ # minute - Integer minute.
246
+ # second - Integer second.
232
247
  #
233
- # @return [Time]
248
+ # Returns a new Time object constructed from these params.
234
249
  def construct(year, month = 1, day = 1, hour = 0, minute = 0, second = 0)
235
250
  if second >= 60
236
251
  minute += second / 60
@@ -1,20 +1,22 @@
1
1
  module Chronic
2
2
  class Grabber < Tag
3
3
 
4
- # Scan an Array of {Token}s and apply any necessary Grabber tags to
5
- # each token
4
+ # Scan an Array of Tokens and apply any necessary Grabber tags to
5
+ # each token.
6
6
  #
7
- # @param [Array<Token>] tokens Array of tokens to scan
8
- # @param [Hash] options Options specified in {Chronic.parse}
9
- # @return [Array] list of tokens
7
+ # tokens - An Array of Token objects to scan.
8
+ # options - The Hash of options specified in Chronic::parse.
9
+ #
10
+ # Returns an Array of Token objects.
10
11
  def self.scan(tokens, options)
11
12
  tokens.each do |token|
12
13
  if t = scan_for_all(token) then token.tag(t); next end
13
14
  end
14
15
  end
15
16
 
16
- # @param [Token] token
17
- # @return [Grabber, nil]
17
+ # token - The Token object to scan.
18
+ #
19
+ # Returns a new Grabber object.
18
20
  def self.scan_for_all(token)
19
21
  scan_for token, self,
20
22
  {
@@ -1,25 +1,22 @@
1
1
  module Chronic
2
2
  class Handler
3
3
 
4
- # @return [Array] A list of patterns
5
4
  attr_reader :pattern
6
5
 
7
- # @return [Symbol] The method which handles this list of patterns.
8
- # This method should exist inside the {Handlers} module
9
6
  attr_reader :handler_method
10
7
 
11
- # @param [Array] pattern A list of patterns to match tokens against
12
- # @param [Symbol] handler_method The method to be invoked when patterns
13
- # are matched. This method should exist inside the {Handlers} module
8
+ # pattern - An Array of patterns to match tokens against.
9
+ # handler_method - A Symbole representing the method to be invoked
10
+ # when patterns are matched.
14
11
  def initialize(pattern, handler_method)
15
12
  @pattern = pattern
16
13
  @handler_method = handler_method
17
14
  end
18
15
 
19
- # @param [Array] tokens
20
- # @param [Hash] definitions
21
- # @return [Boolean]
22
- # @see Chronic.tokens_to_span
16
+ # tokens - An Array of tokens to process.
17
+ # definitions - A Hash of definitions to check against.
18
+ #
19
+ # Returns true if a match is found.
23
20
  def match(tokens, definitions)
24
21
  token_index = 0
25
22
 
@@ -70,8 +67,9 @@ module Chronic
70
67
  Handlers.send(@handler_method, tokens, options)
71
68
  end
72
69
 
73
- # @param [Handler] The handler to compare
74
- # @return [Boolean] True if these handlers match
70
+ # other - The other Handler object to compare.
71
+ #
72
+ # Returns true if these Handlers match.
75
73
  def ==(other)
76
74
  @pattern == other.pattern
77
75
  end
@@ -49,6 +49,13 @@ module Chronic
49
49
  handle_m_d(month, day, tokens[2..tokens.size], options)
50
50
  end
51
51
 
52
+ # Handle ordinal this month
53
+ def handle_od_rm(tokens, options)
54
+ day = tokens[0].get_tag(OrdinalDay).type
55
+ month = tokens[2].get_tag(RepeaterMonth)
56
+ handle_m_d(month, day, tokens[3..tokens.size], options)
57
+ end
58
+
52
59
  # Handle ordinal-day/repeater-month-name
53
60
  def handle_od_rmn(tokens, options)
54
61
  month = tokens[1].get_tag(RepeaterMonthName)
@@ -124,13 +131,7 @@ module Chronic
124
131
  end
125
132
 
126
133
  # Handle generic timestamp (ruby 1.8)
127
- def handle_rdn_rmn_sd_t_tz_sy(tokens, options)
128
- t = Chronic.time_class.parse(options[:text])
129
- Span.new(t, t + 1)
130
- end
131
-
132
- # Handle generic timestamp (ruby 1.9)
133
- def handle_sy_sm_sd_t_tz(tokens, options)
134
+ def handle_generic(tokens, options)
134
135
  t = Chronic.time_class.parse(options[:text])
135
136
  Span.new(t, t + 1)
136
137
  end
@@ -224,6 +225,30 @@ module Chronic
224
225
  handle_sm_sd_sy(new_tokens + time_tokens, options)
225
226
  end
226
227
 
228
+ # Handle scalar-month/scalar-day
229
+ def handle_sm_sd(tokens, options)
230
+ month = tokens[0].get_tag(ScalarMonth).type
231
+ day = tokens[1].get_tag(ScalarDay).type
232
+ year = Chronic.now.year
233
+ time_tokens = tokens.last(tokens.size - 2)
234
+
235
+ return if month_overflow?(year, month, day)
236
+
237
+ begin
238
+ day_start = Chronic.time_class.local(year, month, day)
239
+ day_or_time(day_start, time_tokens, options)
240
+ rescue ArgumentError
241
+ nil
242
+ end
243
+ end
244
+
245
+ # Handle scalar-day/scalar-month
246
+ def handle_sd_sm(tokens, options)
247
+ new_tokens = [tokens[1], tokens[0]]
248
+ time_tokens = tokens.last(tokens.size - 2)
249
+ handle_sm_sd(new_tokens + time_tokens, options)
250
+ end
251
+
227
252
  # Handle scalar-month/scalar-year
228
253
  def handle_sm_sy(tokens, options)
229
254
  month = tokens[0].get_tag(ScalarMonth).type
@@ -255,7 +280,7 @@ module Chronic
255
280
 
256
281
  begin
257
282
  start_time = Chronic.time_class.local(year, month.index, day)
258
- end_time = Chronic.time_class.local(year, month.index, day + 1)
283
+ end_time = time_with_rollover(year, month.index, day + 1)
259
284
  Span.new(start_time, end_time)
260
285
  rescue ArgumentError
261
286
  nil
@@ -272,13 +297,46 @@ module Chronic
272
297
 
273
298
  begin
274
299
  start_time = Chronic.time_class.local(year, month.index, day)
275
- end_time = Chronic.time_class.local(year, month.index, day + 1)
300
+ end_time = time_with_rollover(year, month.index, day + 1)
301
+ Span.new(start_time, end_time)
302
+ rescue ArgumentError
303
+ nil
304
+ end
305
+ end
306
+
307
+ # Handle RepeaterDayName RepeaterMonthName ScalarDay ScalarYear
308
+ def handle_rdn_rmn_sd_sy(tokens, options)
309
+ month = tokens[1].get_tag(RepeaterMonthName)
310
+ day = tokens[2].get_tag(ScalarDay).type
311
+ year = tokens[3].get_tag(ScalarYear).type
312
+
313
+ return if month_overflow?(year, month.index, day)
314
+
315
+ begin
316
+ start_time = Chronic.time_class.local(year, month.index, day)
317
+ end_time = time_with_rollover(year, month.index, day + 1)
276
318
  Span.new(start_time, end_time)
277
319
  rescue ArgumentError
278
320
  nil
279
321
  end
280
322
  end
281
323
 
324
+ def handle_sm_rmn_sy(tokens, options)
325
+ day = tokens[0].get_tag(ScalarDay).type
326
+ month = tokens[1].get_tag(RepeaterMonthName).index
327
+ year = tokens[2].get_tag(ScalarYear).type
328
+ if tokens.size > 3
329
+ time = get_anchor([tokens.last], options).begin
330
+ h, m, s = time.hour, time.min, time.sec
331
+ time = Chronic.time_class.local(year, month, day, h, m, s)
332
+ end_time = Chronic.time_class.local(year, month, day + 1, h, m, s)
333
+ else
334
+ time = Chronic.time_class.local(year, month, day)
335
+ end_time = Chronic.time_class.local(year, month, day + 1)
336
+ end
337
+ Span.new(time, end_time)
338
+ end
339
+
282
340
  # anchors
283
341
 
284
342
  # Handle repeaters
@@ -301,7 +359,7 @@ module Chronic
301
359
  repeater = tokens[1].get_tag(Repeater)
302
360
  pointer = tokens[2].get_tag(Pointer).type
303
361
 
304
- repeater.offset(span, distance, pointer)
362
+ repeater.offset(span, distance, pointer) if repeater.respond_to?(:offset)
305
363
  end
306
364
 
307
365
  # Handle scalar/repeater/pointer
@@ -373,7 +431,6 @@ module Chronic
373
431
  def get_anchor(tokens, options)
374
432
  grabber = Grabber.new(:this)
375
433
  pointer = :future
376
-
377
434
  repeaters = get_repeaters(tokens)
378
435
  repeaters.size.times { tokens.pop }
379
436
 
@@ -417,6 +474,8 @@ module Chronic
417
474
  else
418
475
  day > RepeaterMonth::MONTH_DAYS[month - 1]
419
476
  end
477
+ rescue ArgumentError
478
+ false
420
479
  end
421
480
 
422
481
  # Recursively finds repeaters within other repeaters.
@@ -435,6 +494,20 @@ module Chronic
435
494
  end
436
495
  end
437
496
 
497
+ def time_with_rollover(year, month, day)
498
+ date_parts =
499
+ if month_overflow?(year, month, day)
500
+ if month == 12
501
+ [year + 1, 1, 1]
502
+ else
503
+ [year, month + 1, 1]
504
+ end
505
+ else
506
+ [year, month, day]
507
+ end
508
+ Chronic.time_class.local(*date_parts)
509
+ end
510
+
438
511
  def dealias_and_disambiguate_times(tokens, options)
439
512
  # handle aliases of am/pm
440
513
  # 5:00 in the morning -> 5:00 am
@@ -1,12 +1,13 @@
1
1
  module Chronic
2
2
  class Ordinal < Tag
3
3
 
4
- # Scan an Array of {Token}s and apply any necessary Ordinal tags to
5
- # each token
4
+ # Scan an Array of Token objects and apply any necessary Ordinal
5
+ # tags to each token.
6
6
  #
7
- # @param [Array<Token>] tokens Array of tokens to scan
8
- # @param [Hash] options Options specified in {Chronic.parse}
9
- # @return [Array] list of tokens
7
+ # tokens - An Array of tokens to scan.
8
+ # options - The Hash of options specified in Chronic::parse.
9
+ #
10
+ # Returns an Array of tokens.
10
11
  def self.scan(tokens, options)
11
12
  tokens.each do |token|
12
13
  if t = scan_for_ordinals(token) then token.tag(t) end
@@ -14,14 +15,16 @@ module Chronic
14
15
  end
15
16
  end
16
17
 
17
- # @param [Token] token
18
- # @return [Ordinal, nil]
18
+ # token - The Token object we want to scan.
19
+ #
20
+ # Returns a new Ordinal object.
19
21
  def self.scan_for_ordinals(token)
20
22
  Ordinal.new($1.to_i) if token.word =~ /^(\d*)(st|nd|rd|th)$/
21
23
  end
22
24
 
23
- # @param [Token] token
24
- # @return [OrdinalDay, nil]
25
+ # token - The Token object we want to scan.
26
+ #
27
+ # Returns a new Ordinal object.
25
28
  def self.scan_for_days(token)
26
29
  if token.word =~ /^(\d*)(st|nd|rd|th)$/
27
30
  unless $1.to_i > 31 || $1.to_i < 1
@@ -1,20 +1,22 @@
1
1
  module Chronic
2
2
  class Pointer < Tag
3
3
 
4
- # Scan an Array of {Token}s and apply any necessary Pointer tags to
5
- # each token
4
+ # Scan an Array of Token objects and apply any necessary Pointer
5
+ # tags to each token.
6
6
  #
7
- # @param [Array<Token>] tokens Array of tokens to scan
8
- # @param [Hash] options Options specified in {Chronic.parse}
9
- # @return [Array] list of tokens
7
+ # tokens - An Array of tokens to scan.
8
+ # options - The Hash of options specified in Chronic::parse.
9
+ #
10
+ # Returns an Array of tokens.
10
11
  def self.scan(tokens, options)
11
12
  tokens.each do |token|
12
13
  if t = scan_for_all(token) then token.tag(t) end
13
14
  end
14
15
  end
15
16
 
16
- # @param [Token] token
17
- # @return [Pointer, nil]
17
+ # token - The Token object we want to scan.
18
+ #
19
+ # Returns a new Pointer object.
18
20
  def self.scan_for_all(token)
19
21
  scan_for token, self,
20
22
  {