diff --git a/lib/split/experiment.rb b/lib/split/experiment.rb index 3534cf92..5b2a8843 100644 --- a/lib/split/experiment.rb +++ b/lib/split/experiment.rb @@ -65,7 +65,7 @@ def save if new_record? Split.redis.sadd(:experiments, name) - Split.redis.hset(:experiment_start_times, @name, Time.now) + Split.redis.hset(:experiment_start_times, @name, Time.now.to_i) @alternatives.reverse.each {|a| Split.redis.lpush(name, a.name)} @goals.reverse.each {|a| Split.redis.lpush(goals_key, a)} unless @goals.nil? else @@ -157,7 +157,14 @@ def reset_winner def start_time t = Split.redis.hget(:experiment_start_times, @name) - Time.parse(t) if t + if t + # Check if stored time is an integer + if t =~ /^[-+]?[0-9]+$/ + t = Time.at(t.to_i) + else + t = Time.parse(t) + end + end end def next_alternative diff --git a/spec/experiment_spec.rb b/spec/experiment_spec.rb index 03e9c612..5edbb2b4 100644 --- a/spec/experiment_spec.rb +++ b/spec/experiment_spec.rb @@ -44,7 +44,7 @@ def alternative(color) end it "should save the start time to redis" do - experiment_start_time = Time.parse("Sat Mar 03 14:01:03") + experiment_start_time = Time.at(1372167761) Time.stub(:now => experiment_start_time) experiment.save @@ -59,6 +59,15 @@ def alternative(color) Split::Experiment.find('basket_text').algorithm.should == experiment_algorithm end + it "should handle having a start time stored as a string" do + experiment_start_time = Time.parse("Sat Mar 03 14:01:03") + Time.stub(:now => experiment_start_time) + experiment.save + Split.redis.hset(:experiment_start_times, experiment.name, experiment_start_time) + + Split::Experiment.find('basket_text').start_time.should == experiment_start_time + end + it "should handle not having a start time" do experiment_start_time = Time.parse("Sat Mar 03 14:01:03") Time.stub(:now => experiment_start_time)