lib/redistat/date.rb in redistat-0.3.0 vs lib/redistat/date.rb in redistat-0.4.0
- old
+ new
@@ -1,19 +1,19 @@
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)
@@ -24,26 +24,26 @@
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?
@@ -55,13 +55,13 @@
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
@@ -72,17 +72,17 @@
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