擅长:python、mysql、java
<p><em>这个答案是基于python和Django的。</em><br/>在使用<code>generation_time</code>之前,请注意<code>generation_time</code>将被<strong>转换为UTC(世界时),它可能在前面或后面,这取决于您的当前时区,因此如果您在项目设置中使用<strong>本地时间来保存对象的时间戳,您可能会看到时间上的差异。对我来说,差5.5个小时。所以根据你的时区调整时间。</p>
<p><strong>示例:</strong>
对于<code>ObjectId('5c51aca67c76124020edbbaf')</code>,这个对象的实际创建时间是<code>datetime.datetime(2019, 1, 30, 19, 24, 28, 73000)</code>
但当我用<code>generation_time</code>计算出它的时间时,它落后了5.5个小时。</p>
<pre><code>In [10]: from bson import ObjectId
In [11]: ObjectId('5c51aca67c76124020edbbaf').generation_time
Out[11]: datetime.datetime(2019, 1, 30, 13, 54, 46, tzinfo=<bson.tz_util.FixedOffset object at 0x102461128>)
</code></pre>