Skip to content

Instantly share code, notes, and snippets.

@kennystone
Created February 1, 2011 03:47
Show Gist options
  • Save kennystone/805387 to your computer and use it in GitHub Desktop.
Save kennystone/805387 to your computer and use it in GitHub Desktop.
mongoid datetime resolution less than a second
require 'rubygems'
require "test/unit"
require 'mongoid'
class DT
include Mongoid::Document
field :birthday, :type => DateTime
end
class TestDT < Test::Unit::TestCase
def test_mongoid_date_time
t = Time.utc(2011, 1, 8, 12, 45, 55, 555000) ## 555 milliseconds
doc = DT.new( :birthday => t )
assert_equal t.to_f, doc.birthday.to_f
end
end
<<HERE
$ ruby dt_test.rb
1) Failure:
<1294490755.555> expected but was
<1294490755.0>.
HERE
@kennystone
Copy link
Author

mongoid shaves off milliseconds, microseconds, and nanoseconds from time. this does not require a db save to occur.

@bikash119
Copy link

any workaround for this issue?

@kennystone
Copy link
Author

Not that I know about.

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