public
Fork of mojombo/chronic
Description: "Chronic is a pure Ruby natural language date parser." + improvements, corrections, speedups, and additions
Homepage: http://chronic.rubyforge.org
Clone URL: git://github.com/jf/chronic.git
Search Repo:
weekend support
mojombo (author)
Mon Jan 15 22:33:39 -0800 2007
commit  fc507350c9dabd55415c4d6b93e8c376f8c9b90f
tree    cbe1896ab9ea6b3076e7fb96a70afea156b03d0d
parent  7f3625b55aa203738dcdeaabe0588135ab44b594
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+= 0.1.6 2006-01-15
0
+
0
+* added 'weekend' support (eventualbuddha)
0
+
0
 = 0.1.5 2006-12-20
0
 
0
 * fixed 'aug 20' returning next year if current month is august
...
34
35
36
37
 
38
39
40
...
34
35
36
 
37
38
39
40
0
@@ -34,7 +34,7 @@ require 'chronic/ordinal'
0
 require 'chronic/separator'
0
 
0
 module Chronic
0
- VERSION = "0.1.5"
0
+ VERSION = "0.1.6"
0
   
0
   def self.debug; false; end
0
 end
...
72
73
74
75
 
76
77
78
...
104
105
106
107
 
108
109
110
...
72
73
74
 
75
76
77
78
...
104
105
106
 
107
108
109
110
0
@@ -72,7 +72,7 @@ class Chronic::Repeater < Chronic::Tag #:nodoc:
0
                /^months?$/ => :month,
0
                /^fortnights?$/ => :fortnight,
0
                /^weeks?$/ => :week,
0
- /^weekends?$/ => :weekends,
0
+ /^weekends?$/ => :weekend,
0
                /^days?$/ => :day,
0
                /^hours?$/ => :hour,
0
                /^minutes?$/ => :minute,
0
@@ -104,7 +104,7 @@ class Chronic::Repeater < Chronic::Tag #:nodoc:
0
   end
0
   
0
   def this(pointer)
0
- !@now.nil? || raise("Start point must be set before calling #next")
0
+ !@now.nil? || raise("Start point must be set before calling #this")
0
     [:future, :past, :none].include?(pointer) || raise("First argument 'pointer' must be one of :past, :future, :none")
0
   end
0
   
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
0
@@ -1,6 +1,55 @@
0
 class Chronic::RepeaterWeekend < Chronic::Repeater #:nodoc:
0
   WEEKEND_SECONDS = 172_800 # (2 * 24 * 60 * 60)
0
   
0
+ def next(pointer)
0
+ super
0
+
0
+ if !@current_week_start
0
+ case pointer
0
+ when :future
0
+ saturday_repeater = Chronic::RepeaterDayName.new(:saturday)
0
+ saturday_repeater.start = @now
0
+ next_saturday_span = saturday_repeater.next(:future)
0
+ @current_week_start = next_saturday_span.begin
0
+ when :past
0
+ saturday_repeater = Chronic::RepeaterDayName.new(:saturday)
0
+ saturday_repeater.start = (@now + Chronic::RepeaterDay::DAY_SECONDS)
0
+ last_saturday_span = saturday_repeater.next(:past)
0
+ @current_week_start = last_saturday_span.begin
0
+ end
0
+ else
0
+ direction = pointer == :future ? 1 : -1
0
+ @current_week_start += direction * Chronic::RepeaterWeek::WEEK_SECONDS
0
+ end
0
+
0
+ Chronic::Span.new(@current_week_start, @current_week_start + WEEKEND_SECONDS)
0
+ end
0
+
0
+ def this(pointer = :future)
0
+ super
0
+
0
+ case pointer
0
+ when :future, :none
0
+ saturday_repeater = Chronic::RepeaterDayName.new(:saturday)
0
+ saturday_repeater.start = @now
0
+ this_saturday_span = saturday_repeater.this(:future)
0
+ Chronic::Span.new(this_saturday_span.begin, this_saturday_span.begin + WEEKEND_SECONDS)
0
+ when :past
0
+ saturday_repeater = Chronic::RepeaterDayName.new(:saturday)
0
+ saturday_repeater.start = @now
0
+ last_saturday_span = saturday_repeater.this(:past)
0
+ Chronic::Span.new(last_saturday_span.begin, last_saturday_span.begin + WEEKEND_SECONDS)
0
+ end
0
+ end
0
+
0
+ def offset(span, amount, pointer)
0
+ direction = pointer == :future ? 1 : -1
0
+ weekend = Chronic::RepeaterWeekend.new(:weekend)
0
+ weekend.start = span.begin
0
+ start = weekend.next(pointer).begin + (amount - 1) * direction * Chronic::RepeaterWeek::WEEK_SECONDS
0
+ Chronic::Span.new(start, start + (span.end - span.begin))
0
+ end
0
+
0
   def width
0
     WEEKEND_SECONDS
0
   end
...
254
255
256
 
 
 
 
 
 
 
 
 
 
 
257
258
259
...
386
387
388
 
 
 
389
390
391
...
418
419
420
 
 
 
 
 
 
421
422
423
...
492
493
494
 
 
 
 
495
496
497
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
...
397
398
399
400
401
402
403
404
405
...
432
433
434
435
436
437
438
439
440
441
442
443
...
512
513
514
515
516
517
518
519
520
521
0
@@ -254,6 +254,17 @@ class TestParsing < Test::Unit::TestCase
0
     time = Chronic.parse("this week", :now => @time_2006_08_16_14_00_00, :context => :past)
0
     assert_equal Time.local(2006, 8, 14, 19), time
0
     
0
+ # weekend
0
+
0
+ time = Chronic.parse("this weekend", :now => @time_2006_08_16_14_00_00)
0
+ assert_equal Time.local(2006, 8, 20), time
0
+
0
+ time = Chronic.parse("this weekend", :now => @time_2006_08_16_14_00_00, :context => :past)
0
+ assert_equal Time.local(2006, 8, 13), time
0
+
0
+ time = Chronic.parse("last weekend", :now => @time_2006_08_16_14_00_00)
0
+ assert_equal Time.local(2006, 8, 13), time
0
+
0
     # day
0
     
0
     time = Chronic.parse("this day", :now => @time_2006_08_16_14_00_00)
0
@@ -386,6 +397,9 @@ class TestParsing < Test::Unit::TestCase
0
     time = Chronic.parse("3 weeks ago", :now => @time_2006_08_16_14_00_00)
0
     assert_equal Time.local(2006, 7, 26, 14, 30, 30), time
0
     
0
+ time = Chronic.parse("2 weekends ago", :now => @time_2006_08_16_14_00_00)
0
+ assert_equal Time.local(2006, 8, 5), time
0
+
0
     time = Chronic.parse("3 days ago", :now => @time_2006_08_16_14_00_00)
0
     assert_equal Time.local(2006, 8, 13, 14), time
0
     
0
@@ -418,6 +432,12 @@ class TestParsing < Test::Unit::TestCase
0
     time = Chronic.parse("1 week from now", :now => @time_2006_08_16_14_00_00)
0
     assert_equal Time.local(2006, 8, 23, 14, 0, 0), time
0
     
0
+ time = Chronic.parse("1 weekend from now", :now => @time_2006_08_16_14_00_00)
0
+ assert_equal Time.local(2006, 8, 19), time
0
+
0
+ time = Chronic.parse("2 weekends from now", :now => @time_2006_08_16_14_00_00)
0
+ assert_equal Time.local(2006, 8, 26), time
0
+
0
     time = Chronic.parse("1 day hence", :now => @time_2006_08_16_14_00_00)
0
     assert_equal Time.local(2006, 8, 17, 14), time
0
     
0
@@ -492,6 +512,10 @@ class TestParsing < Test::Unit::TestCase
0
     span = Chronic.parse("november", :now => @time_2006_08_16_14_00_00, :guess => false)
0
     assert_equal Time.local(2006, 11), span.begin
0
     assert_equal Time.local(2006, 12), span.end
0
+
0
+ span = Chronic.parse("weekend" , :now => @time_2006_08_16_14_00_00, :guess => false)
0
+ assert_equal Time.local(2006, 8, 19), span.begin
0
+ assert_equal Time.local(2006, 8, 21), span.end
0
   end
0
   
0
   def test_argument_validation

Comments

    No one has commented yet.