Skip to content

Instantly share code, notes, and snippets.

@fj
Created October 31, 2014 17:51
Show Gist options
  • Select an option

  • Save fj/d29795586fccc744bdf5 to your computer and use it in GitHub Desktop.

Select an option

Save fj/d29795586fccc744bdf5 to your computer and use it in GitHub Desktop.
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

fj commented Oct 31, 2014

Copy link
Copy Markdown
Author

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