Permalink
Browse files

bug fix: now supports ruby1.9.2-p180

  • Loading branch information...
1 parent aea50ca commit f83fb30241d3c3d920d5ac0fe9a1ebc57f1a10c3 Your Name committed Nov 11, 2011
Showing with 45 additions and 31 deletions.
  1. +2 −1 chronic_between.gemspec
  2. +4 −3 lib/chronic_between.rb
  3. +39 −27 lib/chronic_between.rb~
View
@@ -1,8 +1,9 @@
Gem::Specification.new do |s|
s.name = 'chronic_between'
- s.version = '0.2.6'
+ s.version = '0.2.9'
s.summary = 'chronic_between'
s.files = Dir['lib/**/*.rb']
+ s.authors = ['James Robertson']
s.add_dependency('chronic')
s.add_dependency('app-routes')
end
View
@@ -81,13 +81,13 @@ def ranges(params, date)
end
# e.g. 3:45-5:15
- get %r{^(\d[\w:]*)-(\d[\w:]*)(?=\s+(daily|every day))?} do
+ get %r{^(\d[\w:]*)-(\d[\w:]*)(?=\s*(daily|every day)?)} do
t1, t2 = params[:captures]
time_range(date, t1, t2)
end
# e.g. 3:45 to 5:15
- get %r{^(\d[\w:]*)\s+to\s+(\d[\w:]*)(?=\s+(daily|every day))?} do
+ get %r{^(\d[\w:]*)\s+to\s+(\d[\w:]*)(?=\s*(daily|every day)?)} do
t1, t2 = params[:captures]
time_range(date, t1, t2)
end
@@ -193,7 +193,8 @@ def cdatetime_range(date, d1, t1, t2)
end
def latest_date_and_ndays(date, d1, d2)
- raw_date1, raw_date2 = [d1,d2].map {|d| Chronic.parse(d)}
+ raw_date1 = Chronic.parse(d1, :context => :past)
+ raw_date2 = Chronic.parse(d2, :now => raw_date1)
cdate2 = Chronic.parse(d2, now: (date - 1))
n_days = ((raw_date2 - raw_date1) / 86400).to_i
View
@@ -6,12 +6,13 @@ require 'chronic'
require 'date'
require 'app-routes'
+
class ChronicBetween
include AppRoutes
def initialize(x)
- @times = x.is_a?(String) ? x.split(/[,;&]/) : x
+ @times = x.is_a?(String) ? x.split(/[,;&]/).map(&:strip) : x
@route = {}; @params = {}
end
@@ -20,16 +21,27 @@ class ChronicBetween
dates = []
ranges(@params, date)
+
negatives = '(not|except)\s+'
times, closed_times = @times.partition {|x| !x[/^#{negatives}/]}
closed_times.map!{|x| x[/^#{negatives}(.*)/,2]}
dates = build times
- positive_flag = dates.any? {|d1, d2| date.between? d1, d2}
+ inside_range = dates.detect {|d1, d2| date.between? d1, d2}
+
neg_dates = build closed_times
- negative_flag = neg_dates.any? {|d1, d2| date.between? d1, d2}
+ inside_restrictions = neg_dates.detect {|d1, d2| date.between? d1, d2}
+
+ if inside_restrictions then
+ return false
+ elsif closed_times.any? and times.empty? then
+ return true
+ elsif inside_range then
+ return true
+ else
+ return false
+ end
- positive_flag and not negative_flag
end
private
@@ -69,39 +81,39 @@ class ChronicBetween
end
# e.g. 3:45-5:15
- get %r{^(\d[\w:]*)-(\d[\w:]*)(?=\s+(daily|every day))} do
+ get %r{^(\d[\w:]*)-(\d[\w:]*)(?=\s*(daily|every day)?)} do
t1, t2 = params[:captures]
- time_range(t1, t2)
+ time_range(date, t1, t2)
end
# e.g. 3:45 to 5:15
- get %r{^(\d[\w:]*)\s+to\s+(\d[\w:]*)(?=\s+(daily|every day))} do
+ get %r{^(\d[\w:]*)\s+to\s+(\d[\w:]*)(?=\s*(daily|every day)?)} do
t1, t2 = params[:captures]
- time_range(t1, t2)
+ time_range(date, t1, t2)
end
# e.g. Mon 3:45-5:15
get %r{^(\w+)\s+(\d[\w:]*)-(\d[\w:]*)$} do
d1, t1, t2 = params[:captures]
- date_with_time_range(date, d1, t1, t2)
+ cdatetime_range(date, d1, t1, t2)
end
# e.g. Mon 3:45 to 5:15
get %r{^(\w+)\s+(\d[\w:]*)\s+to\s+(\d[\w:]*)$} do
d1, t1, t2 = params[:captures]
- date_with_time_range(date, d1, t1, t2)
+ cdatetime_range(date, d1, t1, t2)
end
# e.g. 3:45-5:15 Mon
get %r{^(\d[\w:]*)-(\d[\w:]*)\s+(\w+)$} do
t1, t2, d1 = params[:captures]
- date_with_time_range(date, d1, t1, t2)
+ cdatetime_range(date, d1, t1, t2)
end
# e.g. 3:45 to 5:15 Mon
get %r{^(\d[\w:]*)\s+to\s+(\d[\w:]*)\s+(\w+)$} do
t1, t2, d1 = params[:captures]
- date_with_time_range(date, d1, t1, t2)
+ cdatetime_range(date, d1, t1, t2)
end
# e.g. Mon-Wed
@@ -116,16 +128,16 @@ class ChronicBetween
# e.g. after 6pm
get %r{^after\s+(\d[\w:]*)$} do
t1 = params[:captures].first
- date1 = Chronic.parse(t1).to_datetime
- date2 = DateTime.parse(date1.strftime("%d-%b-%y") + ' 00:00') + 1
+ date1 = DateTime.parse(date.strftime("%d-%b-%y ") + t1)
+ date2 = DateTime.parse((date + 1).strftime("%d-%b-%y ") + '00:00')
[date1, date2]
end
# e.g. before 9pm
get %r{^before\s+(\d[\w:]*)$} do
t1 = params[:captures].first
- date2 = Chronic.parse(t1).to_datetime
- date1 = DateTime.parse(date2.strftime("%d-%b-%y") + ' 00:00') - 1
+ date2 = DateTime.parse(date.strftime("%d-%b-%y ") + t1)
+ date1 = DateTime.parse(date.strftime("%d-%b-%y") + ' 00:00')
[date1, date2]
end
@@ -135,8 +147,8 @@ class ChronicBetween
cdate1, cdate2 = [d1,d2].map {|d| Chronic.parse(d)}
n_days = ((cdate2 - cdate1) / 86400).to_i
dates = 0.upto(n_days).map do |n|
- x = (cdate1.to_date + n).strftime("%d-%b-%y ")
- datetime_range(x+t1, x+t2)
+ x = cdate1.to_datetime + n
+ datetime_range(x, t1, t2)
end
end
@@ -147,7 +159,7 @@ class ChronicBetween
n_days = ((cdate2 - cdate1) / 86400).to_i
date1 = DateTime.parse(cdate1.strftime("%d-%b-%y") + ' 00:00')
- [date1, date1 + n_days]
+ [date1, date1 + n_days + 1]
end
# e.g. April 5th
@@ -157,6 +169,7 @@ class ChronicBetween
date1 = DateTime.parse(cdate1.strftime("%d-%b-%y") + ' 00:00')
[date1, date1 + 1]
end
+
end
def build(times)
@@ -169,14 +182,14 @@ class ChronicBetween
def date_range_time_range(date, d1, d2, t1, t2)
cdate2, n_days = latest_date_and_ndays(date, d1, d2)
dates = (n_days).downto(0).map do |n|
- x = (cdate2.to_date - n).strftime("%d-%b-%y ")
- datetime_range(x+t1, x+t2)
+ x = (cdate2.to_date - n)
+ datetime_range(x, t1, t2)
end
end
- def date_with_time_range(date, d1, t1, t2)
- x = Chronic.parse(d1, now: (date - 1)).strftime("%d-%b-%y ")
- datetime_range(x+t1, x+t2)
+ def cdatetime_range(date, d1, t1, t2)
+ x = Chronic.parse(d1, now: (date - 1))
+ datetime_range(x, t1, t2)
end
def latest_date_and_ndays(date, d1, d2)
@@ -187,11 +200,10 @@ class ChronicBetween
[cdate2, n_days]
end
- def time_range(t1, t2)
- [t1,t2].map {|t| DateTime.parse(t)}
+ def time_range(date, t1, t2)
+ [t1,t2].map {|t| DateTime.parse(date.strftime("%d-%b-%y ") + t)}
end
alias datetime_range time_range
-
end

0 comments on commit f83fb30

Please sign in to comment.