Skip to content

Instantly share code, notes, and snippets.

@nna774
Created July 16, 2026 06:58
Show Gist options
  • Select an option

  • Save nna774/c4bc295be967f6982f5b639405d7d31c to your computer and use it in GitHub Desktop.

Select an option

Save nna774/c4bc295be967f6982f5b639405d7d31c to your computer and use it in GitHub Desktop.
BSON::ObjectId.from_timeの挙動の差
require 'bson'
# 期待: from_time(t) で作った ObjectId の generation_time は t と一致する。
# unique: true でも「一意にする」だけで、タイムスタンプ部は t のままのはず。
t = Time.utc(2003, 4, 7)
plain = BSON::ObjectId.from_time(t)
unique = BSON::ObjectId.from_time(t, unique: true)
puts "bson #{BSON::VERSION}"
puts "requested : #{t.utc.iso8601}"
puts "from_time(t) : #{plain} -> #{plain.generation_time.utc.iso8601}"
puts "from_time(t, unique:true): #{unique} -> #{unique.generation_time.utc.iso8601}"
puts
puts "plain == t ? #{plain.generation_time == t}"
puts "unique == t ? #{unique.generation_time == t}" # bson 5 では false(現在時刻になる)。bson 4.15.0 では true。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment