Added generation_time method on ObjectID

This commit is contained in:
Kyle Banker 2009-12-03 17:45:53 -05:00
parent 0161ea3962
commit 4550389480
2 changed files with 12 additions and 0 deletions

View File

@ -124,6 +124,12 @@ module Mongo
legacy legacy
end end
# Returns the utc time at which this ObjectID was generated. This may
# be used in lieu of a created_at timestamp.
def generation_time
Time.at(@data.pack("C4").unpack("N")[0])
end
private private
begin begin

View File

@ -121,4 +121,10 @@ class ObjectIDTest < Test::Unit::TestCase
assert_equal s, ObjectID.legacy_string_convert(l) assert_equal s, ObjectID.legacy_string_convert(l)
end end
def test_generation_time
time = Time.now
id = ObjectID.new
assert_in_delta time.to_i, id.generation_time.to_i, 2
end
end end