Created
January 10, 2012 01:01
-
-
Save mattfitzgerald/1586104 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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