rizwanreza-chronic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.rdoc +188 -0
  2. data/lib/chronic.rb +57 -0
  3. data/lib/chronic/blunt.rb +234 -0
  4. data/lib/chronic/chronic.rb +326 -0
  5. data/lib/chronic/grabber.rb +26 -0
  6. data/lib/chronic/handlers.rb +549 -0
  7. data/lib/chronic/ordinal.rb +39 -0
  8. data/lib/chronic/pointer.rb +29 -0
  9. data/lib/chronic/repeater.rb +139 -0
  10. data/lib/chronic/repeaters/repeater_day.rb +52 -0
  11. data/lib/chronic/repeaters/repeater_day_name.rb +53 -0
  12. data/lib/chronic/repeaters/repeater_day_portion.rb +94 -0
  13. data/lib/chronic/repeaters/repeater_decade.rb +23 -0
  14. data/lib/chronic/repeaters/repeater_fortnight.rb +70 -0
  15. data/lib/chronic/repeaters/repeater_hour.rb +58 -0
  16. data/lib/chronic/repeaters/repeater_minute.rb +57 -0
  17. data/lib/chronic/repeaters/repeater_month.rb +66 -0
  18. data/lib/chronic/repeaters/repeater_month_name.rb +98 -0
  19. data/lib/chronic/repeaters/repeater_season.rb +150 -0
  20. data/lib/chronic/repeaters/repeater_season_name.rb +45 -0
  21. data/lib/chronic/repeaters/repeater_second.rb +41 -0
  22. data/lib/chronic/repeaters/repeater_time.rb +124 -0
  23. data/lib/chronic/repeaters/repeater_week.rb +73 -0
  24. data/lib/chronic/repeaters/repeater_weekday.rb +77 -0
  25. data/lib/chronic/repeaters/repeater_weekend.rb +65 -0
  26. data/lib/chronic/repeaters/repeater_year.rb +64 -0
  27. data/lib/chronic/scalar.rb +76 -0
  28. data/lib/chronic/separator.rb +91 -0
  29. data/lib/chronic/time_zone.rb +26 -0
  30. data/lib/core_ext/object.rb +7 -0
  31. data/lib/core_ext/time.rb +74 -0
  32. data/lib/numerizer/numerizer.rb +98 -0
  33. data/test/test_Chronic.rb +75 -0
  34. data/test/test_DaylightSavings.rb +119 -0
  35. data/test/test_Handler.rb +110 -0
  36. data/test/test_Numerizer.rb +54 -0
  37. data/test/test_RepeaterDayName.rb +52 -0
  38. data/test/test_RepeaterDecade.rb +46 -0
  39. data/test/test_RepeaterFortnight.rb +63 -0
  40. data/test/test_RepeaterHour.rb +68 -0
  41. data/test/test_RepeaterMinute.rb +35 -0
  42. data/test/test_RepeaterMonth.rb +47 -0
  43. data/test/test_RepeaterMonthName.rb +57 -0
  44. data/test/test_RepeaterSeason.rb +43 -0
  45. data/test/test_RepeaterTime.rb +72 -0
  46. data/test/test_RepeaterWeek.rb +63 -0
  47. data/test/test_RepeaterWeekday.rb +56 -0
  48. data/test/test_RepeaterWeekend.rb +75 -0
  49. data/test/test_RepeaterYear.rb +63 -0
  50. data/test/test_Span.rb +33 -0
  51. data/test/test_Time.rb +50 -0
  52. data/test/test_Token.rb +26 -0
  53. data/test/test_parsing.rb +809 -0
  54. metadata +118 -0
data/README.rdoc ADDED
@@ -0,0 +1,188 @@
1
+ = Chronic
2
+
3
+ http://chronic.rubyforge.org/
4
+
5
+ by Tom Preston-Werner
6
+
7
+ = WARNING:
8
+
9
+ If you haven't noticed already, this is a fork of evaryont's (Colin's) fork of
10
+ mojombo's (Tom's) stable chronic. I decided on my own volition that the 40-some
11
+ (as reported by Github) network should be merged together. I got it to run, but
12
+ quite haphazardly. There are a lot of new features (mostly undocumented except the
13
+ git logs) so be a little flexible in your language passed to Chronic.
14
+
15
+ Given that, if there is a bug, more than likely it's my own fault, not
16
+ mojombo's or evaryont's and therefore bug reports should be sent to my fork, not his.
17
+
18
+ Enjoy Chronic!
19
+
20
+ = ADDITIONS:
21
+
22
+ There are some notes in the history file to show what I have worked on, but primarily I have
23
+ worked on fixing a few bugs that I found and adding some functionality around the strip_tags method
24
+ and the new date_string method.
25
+
26
+ == DESCRIPTION:
27
+
28
+ Chronic is a natural language date/time parser written in pure Ruby. See below for the wide variety of formats Chronic will parse.
29
+
30
+ == INSTALLATION:
31
+
32
+ Chronic can be installed via RubyGems:
33
+
34
+ $ sudo gem install evaryont-chronic
35
+
36
+ == CODE:
37
+
38
+ Browse the code and get an RSS feed of the commit log at:
39
+
40
+ http://github.com/evaryont/chronic.git
41
+
42
+ You can grab the code (and help with development) via git:
43
+
44
+ $ git clone git://github.com/evaryont/chronic.git
45
+
46
+ == USAGE:
47
+
48
+ You can parse strings containing a natural language date using the Chronic.parse method.
49
+
50
+ require 'rubygems'
51
+ require 'chronic'
52
+
53
+ Time.now #=> Sun Aug 27 23:18:25 PDT 2006
54
+
55
+ #---
56
+
57
+ Chronic.parse('tomorrow')
58
+ #=> Mon Aug 28 12:00:00 PDT 2006
59
+
60
+ Chronic.parse('monday', :context => :past)
61
+ #=> Mon Aug 21 12:00:00 PDT 2006
62
+
63
+ Chronic.parse('this tuesday 5:00')
64
+ #=> Tue Aug 29 17:00:00 PDT 2006
65
+
66
+ Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
67
+ #=> Tue Aug 29 05:00:00 PDT 2006
68
+
69
+ Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
70
+ #=> Sat May 27 12:00:00 PDT 2000
71
+
72
+ Chronic.parse('may 27th', :guess => false)
73
+ #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007
74
+
75
+ See Chronic.parse for detailed usage instructions.
76
+
77
+ == EXAMPLES:
78
+
79
+ Chronic can parse a huge variety of date and time formats. Following is a small sample of strings that will be properly parsed. Parsing is case insensitive and will handle common abbreviations and misspellings.
80
+
81
+ Simple
82
+
83
+ thursday
84
+ november
85
+ summer
86
+ friday 13:00
87
+ mon 2:35
88
+ 4pm
89
+ 6 in the morning
90
+ friday 1pm
91
+ sat 7 in the evening
92
+ yesterday
93
+ today
94
+ tomorrow
95
+ this tuesday
96
+ next month
97
+ last winter
98
+ this morning
99
+ last night
100
+ this second
101
+ yesterday at 4:00
102
+ last friday at 20:00
103
+ last week tuesday
104
+ tomorrow at 6:45pm
105
+ afternoon yesterday
106
+ thursday last week
107
+
108
+ Complex
109
+
110
+ 3 years ago
111
+ 5 months before now
112
+ 7 hours ago
113
+ 7 days from now
114
+ 1 week hence
115
+ in 3 hours
116
+ 1 year ago tomorrow
117
+ 3 months ago saturday at 5:00 pm
118
+ 7 hours before tomorrow at noon
119
+ 3rd wednesday in november
120
+ 3rd month next year
121
+ 3rd thursday this september
122
+ 4th day last week
123
+
124
+ Specific Dates
125
+
126
+ January 5
127
+ dec 25
128
+ may 27th
129
+ October 2006
130
+ oct 06
131
+ jan 3 2010
132
+ february 14, 2004
133
+ 3 jan 2000
134
+ 17 april 85
135
+ 5/27/1979
136
+ 27/5/1979
137
+ 05/06
138
+ 1979-05-27
139
+ Friday
140
+ 5
141
+ 4:00
142
+ 17:00
143
+ 0800
144
+
145
+ Specific Times (many of the above with an added time)
146
+
147
+ January 5 at 7pm
148
+ 1979-05-27 05:00:00
149
+ etc
150
+
151
+ == TIME ZONES:
152
+
153
+ Chronic allows you to set which Time class to use when constructing times. By default, the built in Ruby time class creates times in your system's
154
+ local time zone. You can set this to something like ActiveSupport's TimeZone class to get full time zone support.
155
+
156
+ >> Time.zone = "UTC"
157
+ >> Chronic.time_class = Time.zone
158
+ >> Chronic.parse("June 15 2006 at 5:45 AM")
159
+ => Thu, 15 Jun 2006 05:45:00 UTC +00:00
160
+
161
+ == LIMITATIONS:
162
+
163
+ Chronic uses Ruby's built in Time class for all time storage and computation. Because of this, only times that the Time class can handle will be properly parsed. Parsing for times outside of this range will simply return nil. Support for a wider range of times is planned for a future release.
164
+
165
+ == LICENSE:
166
+
167
+ (The MIT License)
168
+
169
+ Copyright (c) 2008 Tom Preston-Werner
170
+
171
+ Permission is hereby granted, free of charge, to any person obtaining
172
+ a copy of this software and associated documentation files (the
173
+ "Software"), to deal in the Software without restriction, including
174
+ without limitation the rights to use, copy, modify, merge, publish,
175
+ distribute, sublicense, and/or sell copies of the Software, and to
176
+ permit persons to whom the Software is furnished to do so, subject to
177
+ the following conditions:
178
+
179
+ The above copyright notice and this permission notice shall be
180
+ included in all copies or substantial portions of the Software.
181
+
182
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
183
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
184
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
185
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
186
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
187
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
188
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/chronic.rb ADDED
@@ -0,0 +1,57 @@
1
+ #=============================================================================
2
+ #
3
+ # Name: Chronic
4
+ # Author: Tom Preston-Werner
5
+ # Purpose: Parse natural language dates and times into Time or
6
+ # Chronic::Span objects
7
+ #
8
+ #=============================================================================
9
+
10
+ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
11
+
12
+ require 'core_ext/object'
13
+ require 'core_ext/time'
14
+
15
+ require 'chronic/chronic'
16
+ require 'chronic/handlers'
17
+
18
+ require 'chronic/repeater'
19
+ require 'chronic/repeaters/repeater_year'
20
+ require 'chronic/repeaters/repeater_season'
21
+ require 'chronic/repeaters/repeater_season_name'
22
+ require 'chronic/repeaters/repeater_month'
23
+ require 'chronic/repeaters/repeater_month_name'
24
+ require 'chronic/repeaters/repeater_fortnight'
25
+ require 'chronic/repeaters/repeater_week'
26
+ require 'chronic/repeaters/repeater_weekend'
27
+ require 'chronic/repeaters/repeater_weekday'
28
+ require 'chronic/repeaters/repeater_day'
29
+ require 'chronic/repeaters/repeater_day_name'
30
+ require 'chronic/repeaters/repeater_day_portion'
31
+ require 'chronic/repeaters/repeater_decade'
32
+ require 'chronic/repeaters/repeater_hour'
33
+ require 'chronic/repeaters/repeater_minute'
34
+ require 'chronic/repeaters/repeater_second'
35
+ require 'chronic/repeaters/repeater_time'
36
+
37
+ require 'chronic/grabber'
38
+ require 'chronic/pointer'
39
+ require 'chronic/scalar'
40
+ require 'chronic/ordinal'
41
+ require 'chronic/separator'
42
+ require 'chronic/time_zone'
43
+ require 'chronic/blunt.rb'
44
+
45
+ require 'numerizer/numerizer'
46
+
47
+ module Chronic
48
+ VERSION = "0.3.0.2"
49
+
50
+ class << self
51
+ attr_accessor :debug
52
+ attr_accessor :time_class
53
+ end
54
+
55
+ self.debug = false
56
+ self.time_class = Time
57
+ end
@@ -0,0 +1,234 @@
1
+ # date range support and parsing of strings calling for a sequence of dates
2
+ # (currently only 'nth Tuesday in June [2009]' etc).
3
+ #
4
+ # My first take -- not stable; a lot of fat to cut out.
5
+ #
6
+ # Chronic::Blunt.dates(Date.today..Date.today+100, {:day => "Wednesday", :week => 0})
7
+ # Chronic::Blunt.range(dates_array,{:csv => ','})
8
+ # Chronic::Blunt.parse("second Saturday in April 2010")
9
+ #
10
+ # Blunt b/c it's err, a blunt instrument :)
11
+ #
12
+ # ...with the exception of brainopia's Array method (which he very
13
+ # kindly wrote while I was troubleshooting a more verbose version
14
+ # on #ruby-lang). (github.com/brainopia)
15
+ #
16
+
17
+ require 'rubygems'
18
+ require 'active_support'
19
+
20
+ class Object
21
+ def tap
22
+ yield self; self
23
+ end
24
+ end
25
+
26
+ class Array #all credit to brainopia for this
27
+ def group_by_range
28
+ uniq.sort.inject([]) do |result, it|
29
+ result << [] unless result.last && result.last.last.next == it
30
+ result.tap {|ar| ar.last << it }
31
+ end
32
+ end
33
+ end
34
+
35
+ class Date
36
+ def self.parse_dmy(it)
37
+ Date.parse(it.split(/\D/).delete_if {|i| i.size < 1}.reverse.join('-'))
38
+ end
39
+ end
40
+
41
+ class Chronic::Blunt
42
+
43
+ ORDINALS = %w[first second third fourth fifth]
44
+
45
+ attr_accessor :result
46
+
47
+ def initialize
48
+ @result = []
49
+ end
50
+
51
+
52
+ def self.compare(dates0,dates1)
53
+ ranges = []
54
+ incongruous = []
55
+ dates0.each {|it|
56
+ incongruous << it unless dates1.include?(it)
57
+ }
58
+ ranges << Chronic::Blunt.range(incongruous)
59
+
60
+ incongruous = []
61
+ dates1.each {|it|
62
+ incongruous << it unless dates0.include?(it)
63
+ }
64
+ ranges << Chronic::Blunt.range(incongruous)
65
+
66
+ @result = ranges
67
+ end
68
+
69
+
70
+ def self.dates(passed_range, specified_options={})
71
+ default_options = {:day => false,
72
+ :week => :every,
73
+ :hour => false}
74
+ options = default_options.merge specified_options
75
+ specified_options.keys.each do |key|
76
+ default_options.keys.include?(key) || raise(Chronic::InvalidArgumentException, "#{key} is not a valid option key.")
77
+ end
78
+
79
+ if passed_range.class == Array
80
+ ranges = passed_range
81
+ else
82
+ ranges = [passed_range]
83
+ end
84
+
85
+ @result = []
86
+
87
+ ranges.each do |range|
88
+ first = get_date(range.first)-1
89
+ last = get_date(range.last)
90
+ @result << (first += 1) while first < last
91
+ end
92
+ if options[:day]
93
+ find_dates(@result,options[:day].to_s,options[:hour],options[:week])
94
+ @result
95
+ end
96
+ @result
97
+ end
98
+
99
+ def self.parse(input)
100
+ string = input.dup.downcase.gsub(/\s{1,}/,' ')
101
+ if (string =~ /first|second|third|fourth|last|every/) == 0
102
+ query = string.gsub(/in/){}.downcase.split(/\s+/)
103
+
104
+ if query[0] == 'last'
105
+ week = -1
106
+ elsif query[0] == 'every'
107
+ week = :every
108
+ else
109
+ week = ORDINALS.index(query.first)
110
+ end
111
+ day = query[1].titleize
112
+ month = Date.parse("#{query[3]}/#{query[2]}")
113
+ dates = self.dates(month..month.end_of_month, {:day => day, :week => week})
114
+ if dates.size < 2
115
+ @result = dates.first
116
+ else
117
+ @result = date
118
+ end
119
+ elsif (string =~ /[0-9]{1,2}\s/) == 0 #&& (string =~ /am|pm|hr|/) > 3
120
+ @result = Chronic.parse(Date.parse(string).strftime("%Y-%m-%d"))
121
+ else
122
+ @result = Chronic.parse(string)
123
+ end
124
+ if @result.nil?
125
+ Chronic.parse(Date.parse(string).strftime("%Y-%m-%d"))
126
+ else
127
+ @result
128
+ end
129
+ end
130
+
131
+
132
+
133
+ def self.range(dates, specified_options={})
134
+ default_options = {:csv => false,
135
+ :start_hrs => 0,
136
+ :end_hrs => 0,
137
+ :time => false,
138
+ :day => false,
139
+ :period => false}
140
+ options = default_options.merge specified_options
141
+
142
+ specified_options.keys.each do |key|
143
+ default_options.keys.include?(key) || raise(Chronic::InvalidArgumentException, "#{key} is not a valid option key.")
144
+ end
145
+
146
+ ranges = dates.flatten.map! {|it| get_date it}.group_by_range.map! do |it|
147
+ if options[:time]
148
+ Time.parse(it.first.strftime("%Y-%m-%d")).advance(:hours => options[:start_hrs])..Time.parse(it.last.strftime("%Y-%m-%d")).advance(:hours => options[:end_hrs])
149
+ else
150
+ it.first..it.last
151
+ end
152
+ end
153
+
154
+ if options[:csv]
155
+ if options[:csv] == true
156
+ @result = to_csv(ranges,'|')
157
+ else
158
+ @result = to_csv(ranges,options[:csv].to_s)
159
+ end
160
+ else
161
+ @result = ranges
162
+ end
163
+
164
+ if options[:day]
165
+ start_dates = Chronic::Blunt.dates(@result, :day => options[:day])
166
+ periods =[]
167
+ start_dates.each do |it|
168
+ first = it.to_time.advance(:hours => options[:start_hrs])
169
+ last = it.to_time.advance(:days => options[:period], :hours => options[:end_hrs])
170
+ available = false
171
+ ranges.each {|it| available = true if it.include?(first..last)}
172
+ periods << (first..last) if available
173
+ end
174
+ @result = periods
175
+ end
176
+ return @result
177
+ end
178
+
179
+ private
180
+
181
+ def self.to_csv(ranges,delimiter)
182
+ d = delimiter
183
+ csv = []
184
+ ranges.each {|range| csv << "#{range.first}#{d}#{range.last}"}
185
+ csv
186
+ end
187
+
188
+
189
+ def self.find_dates(dates,query,hour,week)
190
+ if query.size == 1
191
+ day = query.to_i
192
+ else
193
+ day = Date::DAYNAMES.index(query)
194
+ end
195
+ results = []
196
+ dates.each{|d|
197
+ if hour
198
+ results << (d.to_time + hour.hours) if d.wday == day
199
+ else
200
+ results << d if d.wday == day
201
+ end
202
+ }
203
+ if week != :every
204
+ months = []
205
+ results.each {|day| months << Date.parse("#{day.year}/#{day.mon}")}
206
+ months.uniq!
207
+ months.sort!
208
+ month_days = []
209
+ months.each do |month|
210
+ days = []
211
+ results.each {|day| days << day if day.mon == month.mon}
212
+ if hour
213
+ month_days << days.slice(week).to_time + hour.hours
214
+ else
215
+ month_days << days.slice(week)
216
+ end
217
+ end
218
+ @result = month_days
219
+ else
220
+ @result = results
221
+ end
222
+ end
223
+
224
+ def self.get_date(it)
225
+ case it.class.to_s
226
+ when "Date"
227
+ date = it
228
+ when "Time"
229
+ date = Date.parse(it.strftime("%Y-%m-%d"))
230
+ else
231
+ date = Date.parse(Chronic.parse(it).strftime("%Y-%m-%d"))
232
+ end
233
+ end
234
+ end