Sha256: 2aeb1aa6d1e717b84876d0424854931ea02458bd7c822bfe2ba58536414c52c1

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Chronic
  class RepeaterYear < Repeater #:nodoc:
    YEAR_SECONDS =  31536000  # 365 * 24 * 60 * 60

    def initialize(type)
      super
      @current_year_start = nil
    end

    def next(pointer)
      super

      if !@current_year_start
        case pointer
        when :future
          @current_year_start = Time.construct(@now.year + 1)
        when :past
          @current_year_start = Time.construct(@now.year - 1)
        end
      else
        diff = pointer == :future ? 1 : -1
        @current_year_start = Time.construct(@current_year_start.year + diff)
      end

      Span.new(@current_year_start, Time.construct(@current_year_start.year + 1))
    end

    def this(pointer = :future)
      super

      case pointer
      when :future
        this_year_start = Time.construct(@now.year, @now.month, @now.day) + RepeaterDay::DAY_SECONDS
        this_year_end = Time.construct(@now.year + 1, 1, 1)
      when :past
        this_year_start = Time.construct(@now.year, 1, 1)
        this_year_end = Time.construct(@now.year, @now.month, @now.day)
      when :none
        this_year_start = Time.construct(@now.year, 1, 1)
        this_year_end = Time.construct(@now.year + 1, 1, 1)
      end

      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)

      Span.new(new_begin, new_end)
    end

    def width
      (365 * 24 * 60 * 60)
    end

    def to_s
      super << '-year'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronic-0.4.0 lib/chronic/repeaters/repeater_year.rb