lib/chronic/repeaters/repeater_year.rb in chronic-0.4.3 vs lib/chronic/repeaters/repeater_year.rb in chronic-0.4.4
- old
+ new
@@ -2,11 +2,10 @@
class RepeaterYear < Repeater #:nodoc:
YEAR_SECONDS = 31536000 # 365 * 24 * 60 * 60
def initialize(type)
super
- @current_year_start = nil
end
def next(pointer)
super
@@ -43,24 +42,36 @@
Span.new(this_year_start, this_year_end)
end
def offset(span, amount, pointer)
direction = pointer == :future ? 1 : -1
-
- sb = span.begin
- new_begin = Time.construct(sb.year + (amount * direction), sb.month, sb.day, sb.hour, sb.min, sb.sec)
-
- se = span.end
- new_end = Time.construct(se.year + (amount * direction), se.month, se.day, se.hour, se.min, se.sec)
-
+ new_begin = build_offset_time(span.begin, amount, direction)
+ new_end = build_offset_time(span.end, amount, direction)
Span.new(new_begin, new_end)
end
def width
YEAR_SECONDS
end
def to_s
super << '-year'
+ end
+
+ private
+
+ def build_offset_time(time, amount, direction)
+ year = time.year + (amount * direction)
+ days = month_days(year, time.month)
+ day = time.day > days ? days : time.day
+ Time.construct(year, time.month, day, time.hour, time.min, time.sec)
+ end
+
+ def month_days(year, month)
+ if Date.leap?(year)
+ RepeaterMonth::MONTH_DAYS_LEAP[month - 1]
+ else
+ RepeaterMonth::MONTH_DAYS[month - 1]
+ end
end
end
end
\ No newline at end of file