Merge branch 'gem'
[fuzed.git] / helloworld / vendor / gems / chronic-0.2.2 / lib / chronic / repeaters / repeater_time.rb
blobc35c7780a68cfa72cb6ee34b84b715f9671e16c7
1 class Chronic::RepeaterTime < Chronic::Repeater #:nodoc:
2   class Tick #:nodoc:
3     attr_accessor :time
4     
5     def initialize(time, ambiguous = false)
6       @time = time
7       @ambiguous = ambiguous
8     end
9     
10     def ambiguous?
11       @ambiguous
12     end
13     
14     def *(other)
15       Tick.new(@time * other, @ambiguous)
16     end
17     
18     def to_f
19       @time.to_f
20     end
21     
22     def to_s
23       @time.to_s + (@ambiguous ? '?' : '')
24     end
25   end
26   
27   def initialize(time, options = {})
28     t = time.gsub(/\:/, '')
29     @type = 
30     if (1..2) === t.size
31       Tick.new(t.to_i * 60 * 60, true)
32     elsif t.size == 3
33       Tick.new((t[0..0].to_i * 60 * 60) + (t[1..2].to_i * 60), true)
34     elsif t.size == 4
35       ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
36       Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60, ambiguous)
37     elsif t.size == 5
38       Tick.new(t[0..0].to_i * 60 * 60 + t[1..2].to_i * 60 + t[3..4].to_i, true)
39     elsif t.size == 6
40       ambiguous = time =~ /:/ && t[0..0].to_i != 0 && t[0..1].to_i <= 12
41       Tick.new(t[0..1].to_i * 60 * 60 + t[2..3].to_i * 60 + t[4..5].to_i, ambiguous)
42     else
43       raise("Time cannot exceed six digits")
44     end
45   end
46   
47   # Return the next past or future Span for the time that this Repeater represents
48   #   pointer - Symbol representing which temporal direction to fetch the next day
49   #             must be either :past or :future
50   def next(pointer)
51     super
52     
53     half_day = 60 * 60 * 12
54     full_day = 60 * 60 * 24
55     
56     first = false
57     
58     unless @current_time
59       first = true
60       midnight = Time.local(@now.year, @now.month, @now.day)
61       yesterday_midnight = midnight - full_day
62       tomorrow_midnight = midnight + full_day
64       catch :done do
65         if pointer == :future
66           if @type.ambiguous?
67             [midnight + @type, midnight + half_day + @type, tomorrow_midnight + @type].each do |t|
68               (@current_time = t; throw :done) if t > @now
69             end
70           else
71             [midnight + @type, tomorrow_midnight + @type].each do |t|
72               (@current_time = t; throw :done) if t > @now
73             end
74           end
75         else # pointer == :past
76           if @type.ambiguous?
77             [midnight + half_day + @type, midnight + @type, yesterday_midnight + @type * 2].each do |t|
78               (@current_time = t; throw :done) if t < @now
79             end
80           else
81             [midnight + @type, yesterday_midnight + @type].each do |t|
82               (@current_time = t; throw :done) if t < @now
83             end
84           end
85         end
86       end
87       
88       @current_time || raise("Current time cannot be nil at this point")
89     end
90     
91     unless first
92       increment = @type.ambiguous? ? half_day : full_day
93       @current_time += pointer == :future ? increment : -increment
94     end
95     
96     Chronic::Span.new(@current_time, @current_time + width)
97   end
98   
99   def this(context = :future)
100     super
101     
102     context = :future if context == :none
103     
104     self.next(context)
105   end
106   
107   def width
108     1
109   end
110   
111   def to_s
112     super << '-time-' << @type.to_s
113   end