Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Created January 10, 2012 00:22
Show Gist options
  • Save mattfitzgerald/1585903 to your computer and use it in GitHub Desktop.
Save mattfitzgerald/1585903 to your computer and use it in GitHub Desktop.
def convert_time_safely time_input
begin
return nil if time_input.to_i == 0
Time.at(time_input.to_i/1000)
rescue
return nil
end
end
puts "10000 -> #{convert_time_safely(10000)}"
puts "'10000' -> #{convert_time_safely('10000')}"
puts "nil -> #{convert_time_safely(nil)}"
puts "overflow -> #{convert_time_safely("999999999999999999999999999999")}"
puts "Time.now -> #{Time.now}"
puts "Time.now.to_s -> #{convert_time_safely(Time.now.to_s)}"
puts "Time.now.to_s -> #{convert_time_safely(Time.now.to_s)}"
puts "'Tuesday January 10 11:19:39' -> #{convert_time_safely('Tuesday January 10 11:19:39')}"
=begin
10000 -> Thu Jan 01 10:00:10 +1000 1970
'10000' -> Thu Jan 01 10:00:10 +1000 1970
nil ->
overflow ->
Time.now -> Tue Jan 10 11:21:10 +1100 2012
Time.now.to_s ->
Time.now.to_s ->
'Tuesday January 10 11:19:39' ->
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment