mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
fix some timezone handling and add specs
This commit is contained in:
parent
b668a6f22e
commit
fa2736feb3
@ -4,7 +4,7 @@ module ValidatesTimeliness
|
|||||||
def type_cast_value(value, type)
|
def type_cast_value(value, type)
|
||||||
return nil if value.nil?
|
return nil if value.nil?
|
||||||
|
|
||||||
value.in_time_zone if value.acts_like?(:time) && @timezone_aware
|
value = value.in_time_zone if value.acts_like?(:time) && @timezone_aware
|
||||||
value = case type
|
value = case type
|
||||||
when :time
|
when :time
|
||||||
dummy_time(value)
|
dummy_time(value)
|
||||||
@ -14,7 +14,7 @@ module ValidatesTimeliness
|
|||||||
value.to_time
|
value.to_time
|
||||||
end
|
end
|
||||||
if options[:ignore_usec] && value.is_a?(Time)
|
if options[:ignore_usec] && value.is_a?(Time)
|
||||||
ValidatesTimeliness::Parser.make_time(Array(value).reverse[4..9])
|
ValidatesTimeliness::Parser.make_time(Array(value).reverse[4..9], @timezone_aware)
|
||||||
else
|
else
|
||||||
value
|
value
|
||||||
end
|
end
|
||||||
@ -22,6 +22,7 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
def dummy_time(value)
|
def dummy_time(value)
|
||||||
time = if value.acts_like?(:time)
|
time = if value.acts_like?(:time)
|
||||||
|
value = value.in_time_zone if @timezone_aware
|
||||||
[value.hour, value.min, value.sec]
|
[value.hour, value.min, value.sec]
|
||||||
else
|
else
|
||||||
[0,0,0]
|
[0,0,0]
|
||||||
|
|||||||
@ -55,6 +55,14 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
it "should return as Time with same component values" do
|
it "should return as Time with same component values" do
|
||||||
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
|
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return same Time in correct zone if timezone aware" do
|
||||||
|
@timezone_aware = true
|
||||||
|
value = Time.utc(2010, 1, 1, 12, 34, 56)
|
||||||
|
result = type_cast_value(value, :datetime)
|
||||||
|
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
|
||||||
|
result.zone.should == 'EST'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "ignore_usec option" do
|
describe "ignore_usec option" do
|
||||||
@ -64,6 +72,14 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
|
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
|
||||||
type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
|
type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should ignore usec and return time in correct zone if timezone aware" do
|
||||||
|
@timezone_aware = true
|
||||||
|
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
|
||||||
|
result = type_cast_value(value, :datetime)
|
||||||
|
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
|
||||||
|
result.zone.should == 'EST'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,6 +92,11 @@ describe ValidatesTimeliness::Conversion do
|
|||||||
dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
|
dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should return time component values shifted to current zone if timezone aware' do
|
||||||
|
@timezone_aware = true
|
||||||
|
dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.zone.local(2000, 1, 1, 23, 34, 56)
|
||||||
|
end
|
||||||
|
|
||||||
it 'should return base dummy time value for Date value' do
|
it 'should return base dummy time value for Date value' do
|
||||||
dummy_time(Date.new(2010, 11, 22)).should == Time.utc(2000, 1, 1, 0, 0, 0)
|
dummy_time(Date.new(2010, 11, 22)).should == Time.utc(2000, 1, 1, 0, 0, 0)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user