Merge branch 'gem'
[fuzed.git] / helloworld / vendor / gems / chronic-0.2.2 / lib / chronic / repeaters / repeater_month.rb
blobedd89eeb28a3b1b1828531d70fceba4fa12702e5
1 class Chronic::RepeaterMonth < Chronic::Repeater #:nodoc:
2   MONTH_SECONDS = 2_592_000 # 30 * 24 * 60 * 60
3   YEAR_MONTHS = 12
4   
5   def next(pointer)
6     super
7     
8     if !@current_month_start
9       @current_month_start = offset_by(Time.construct(@now.year, @now.month), 1, pointer)
10     else
11       @current_month_start = offset_by(Time.construct(@current_month_start.year, @current_month_start.month), 1, pointer)
12     end
13     
14     Chronic::Span.new(@current_month_start, Time.construct(@current_month_start.year, @current_month_start.month + 1))
15   end
16   
17   def this(pointer = :future)
18     super
19     
20     case pointer
21     when :future
22       month_start = Time.construct(@now.year, @now.month, @now.day + 1)
23       month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
24     when :past
25       month_start = Time.construct(@now.year, @now.month)
26       month_end = Time.construct(@now.year, @now.month, @now.day)
27     when :none
28       month_start = Time.construct(@now.year, @now.month)
29       month_end = self.offset_by(Time.construct(@now.year, @now.month), 1, :future)
30     end
31     
32     Chronic::Span.new(month_start, month_end)
33   end
34   
35   def offset(span, amount, pointer)      
36     Chronic::Span.new(offset_by(span.begin, amount, pointer), offset_by(span.end, amount, pointer))
37   end
38   
39   def offset_by(time, amount, pointer) 
40     direction = pointer == :future ? 1 : -1
41     
42     amount_years = direction * amount / YEAR_MONTHS
43     amount_months = direction * amount % YEAR_MONTHS
44     
45     new_year = time.year + amount_years
46     new_month = time.month + amount_months
47     if new_month > YEAR_MONTHS
48       new_year += 1
49       new_month -= YEAR_MONTHS
50     end
51     Time.construct(new_year, new_month, time.day, time.hour, time.min, time.sec)
52   end
53   
54   def width
55     MONTH_SECONDS
56   end
57   
58   def to_s
59     super << '-month'
60   end
61 end