Sha256: 1263d519a0eb9a38b011ea84ca4a2878fe978406f2a92056a8372080c5855546

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Redisrank
  class Date

    attr_accessor :year
    attr_accessor :month
    attr_accessor :day
    attr_accessor :hour
    attr_accessor :min
    attr_accessor :sec
    attr_accessor :usec
    attr_accessor :depth

    DEPTHS = [:year, :month, :day, :hour, :min, :sec, :usec]

    def initialize(input, depth = nil)
      @depth = depth
      if input.is_a?(::Time)
        from_time(input)
      elsif input.is_a?(::Date)
        from_date(input)
      elsif input.is_a?(::String)
        from_string(input)
      elsif input.is_a?(::Fixnum)
        from_integer(input)
      elsif input.is_a?(::Bignum)
        from_integer(input)
      end
    end

    def to_t
      ::Time.local(@year, @month, @day, @hour, @min, @sec, @usec)
    end
    alias :to_time :to_t

    def to_d
      ::Date.civil(@year, @month, @day)
    end
    alias :to_date :to_d

    def to_i
      to_time.to_i
    end
    alias :to_integer :to_i

    def to_s(depth = nil)
      depth ||= @depth ||= :sec
      output = ""
      DEPTHS.each_with_index do |current, i|
        break if self.send(current).nil?
        if current != :usec
          output << self.send(current).to_s.rjust((i <= 0) ? 4 : 2, '0')
        else
          output << "." + self.send(current).to_s.rjust(6, '0')
        end
        break if current == depth
      end
      output
    end
    alias :to_string :to_s

    private

    def from_time(input)
      DEPTHS.each do |k|
        send("#{k}=", input.send(k))
      end
    end

    def from_date(input)
      [:year, :month, :day].each do |k|
        send("#{k}=", input.send(k))
      end
      [:hour, :min, :sec, :usec].each do |k|
        send("#{k}=", 0)
      end
    end

    def from_integer(input)
      from_time(::Time.at(input))
    end

    def from_string(input)
      input += "19700101000000"[input.size..-1] if input =~ /^\d\d\d[\d]+$/i
      from_time(::Time.parse(input))
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redisrank-0.1.0 lib/redisrank/date.rb