Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Created January 10, 2012 01:01
Show Gist options
  • Save mattfitzgerald/1586104 to your computer and use it in GitHub Desktop.
Save mattfitzgerald/1586104 to your computer and use it in GitHub Desktop.
# in a module
def SmsExchange.convert_time_safely_from_int_millis input
begin
return nil if input.to_i == 0
Time.at(input.to_i/1000)
rescue
return nil
end
end
# with tests
it "should correctly convert integers to times" do
SmsExchange.convert_time_safely_from_int_millis(10000).should == Time.utc(1970,1,1,0,0,10)
SmsExchange.convert_time_safely_from_int_millis("10000").should == Time.utc(1970,1,1,0,0,10)
SmsExchange.convert_time_safely_from_int_millis(nil).should == nil
SmsExchange.convert_time_safely_from_int_millis("999999999999999999999999999999").should == nil
SmsExchange.convert_time_safely_from_int_millis(Time.now.to_s).should == nil
SmsExchange.convert_time_safely_from_int_millis("Tuesday January 10 11:19:39").should == nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment