mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-19 13:26:39 +00:00
restructured Redistat::Date a bit
This commit is contained in:
@@ -18,33 +18,14 @@ module Redistat
|
||||
end
|
||||
end
|
||||
|
||||
def from_time(input)
|
||||
[:year, :month, :day, :hour, :min, :sec].each do |k|
|
||||
send("#{k}=", input.send(k))
|
||||
end
|
||||
end
|
||||
|
||||
def to_time
|
||||
::Time.local(@year, @month, @day, @hour, @min, @sec)
|
||||
end
|
||||
|
||||
def from_date(input)
|
||||
[:year, :month, :day].each do |k|
|
||||
send("#{k}=", input.send(k))
|
||||
end
|
||||
[:hour, :min, :sec].each do |k|
|
||||
send("#{k}=", 0)
|
||||
end
|
||||
end
|
||||
|
||||
def to_date
|
||||
::Date.civil(@year, @month, @day)
|
||||
end
|
||||
|
||||
def from_string(input)
|
||||
from_time(::Time.parse(input))
|
||||
end
|
||||
|
||||
def to_string(depth = :sec)
|
||||
output = ""
|
||||
[:year, :month, :day, :hour, :min, :sec].each_with_index do |current, i|
|
||||
@@ -55,9 +36,31 @@ module Redistat
|
||||
output
|
||||
end
|
||||
|
||||
alias :to_s :to_string
|
||||
alias :to_t :to_time
|
||||
alias :to_d :to_date
|
||||
alias :to_s :to_string
|
||||
|
||||
|
||||
private
|
||||
|
||||
def from_time(input)
|
||||
[:year, :month, :day, :hour, :min, :sec].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].each do |k|
|
||||
send("#{k}=", 0)
|
||||
end
|
||||
end
|
||||
|
||||
def from_string(input)
|
||||
from_time(::Time.parse(input))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,14 +4,14 @@ describe Redistat::Date do
|
||||
|
||||
it "should initialize from Time object" do
|
||||
now = Time.now
|
||||
[Redistat::Date.new(now), now.to_redistat].each do |rdate|
|
||||
[Redistat::Date.new(now), now.to_rs].each do |rdate|
|
||||
[:year, :month, :day, :hour, :min, :sec].each { |k| rdate.send(k).should == now.send(k) }
|
||||
end
|
||||
end
|
||||
|
||||
it "should initialize from Date object" do
|
||||
today = Date.today
|
||||
[Redistat::Date.new(today), today.to_redistat].each do |rdate|
|
||||
[Redistat::Date.new(today), today.to_rs].each do |rdate|
|
||||
[:year, :month, :day].each { |k| rdate.send(k).should == today.send(k) }
|
||||
[:hour, :min, :sec].each { |k| rdate.send(k).should == 0 }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user