Sha256: b2b28641603188149ce29af5786e14ad0bab121f18d68b45153a192986abf575

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

module Redistat
  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

2 entries across 2 versions & 1 rubygems

Version Path
redistat-0.5.0 lib/redistat/date.rb
redistat-0.4.0 lib/redistat/date.rb