Skip to content

Instantly share code, notes, and snippets.

@fj
Created Oct 31, 2014
Embed
What would you like to do?
perplexing round-trip Time instance behavior
t1 = Time.now
# => 2014-10-31 13:48:12 -0400
t2 = Time.at(t1.to_i)
# => 2014-10-31 13:48:12 -0400
t1.to_a
# => [12, 48, 13, 31, 10, 2014, 5, 304, true, "EDT"]
t2.to_a
# => [12, 48, 13, 31, 10, 2014, 5, 304, true, "EDT"]
t1.to_a == t2.to_a
# => true
t1 == t2
# => false # !!!!!
@fj
Copy link
Author

fj commented Oct 31, 2014

This happens because Time is tracking subsecond precision, but that's not reflected in the array or in any representation of its state:

t1.subsec.to_f
# => 0.349962996

t2.subsec.to_f
# => 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment