Sha256: b16cb35a0eaebb42c42d674bef685b61d65a6441333de4822b60cf431f595c15
Contents?: true
Size: 2 KB
Versions: 6
Compression:
Stored size: 2 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) 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 module DateHelper def to_redistat(depth = nil) Redistat::Date.new(self, depth) end alias :to_rs :to_redistat end end
Version data entries
6 entries across 6 versions & 1 rubygems