title: Why is there a difference between the stored and queried time in Mongo database using PyMongo client?
body: <p>I have a MongoDB database with <code>foo</code> objects. I'd like to record the time that they are added to the database. To do this, my approach is to store a time, add the <code>foo</code> to the database and set the start time. When I get the <code>foo</code> from the database, the start time attribute should match the time when I added it.</p>
  
  <p>Below is my test which reflects this. The functions are not terribly important as they're essentially <code>db.collection.insert</code> or <code>db.collection.find_one</code> calls.</p>
  
  <pre><code>def test_set_foo_start_time(self):
      import datetime
      t = datetime.datetime.utcnow()
      foo_id = "42"
      print("\nAdding {0} to the database.".format(t))
      add_foo_to_database(foo_id)
      self.assertIsNotNone(get_foo_from_database(foo_id))
      set_foo_start_time(foo_id, t)
      time = get_foo_from_database(foo_id)['start_time']
      print("Extracting {0} from the database.".format(time))
      self.assertEqual(t, time)
  </code></pre>
  
  <p>When I run my test, I'm surprised to see that it fails:</p>
  
  <pre><code>test_set_foo_start_time (test_foo_misc.FooMiscellanyTestCase) ... 
  Adding 2016-10-10 17:01:16.559332 to the database.
  Extracting 2016-10-10 17:01:16.559000+00:00 from the database.
  FAIL
  </code></pre>
  
  <p>where the assertion error is:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/erip/Code/proj/tests/test_foo_misc.py", line 336, in test_set_foo_start_time
      self.assertEqual(t, time)
  AssertionError: datet[36 chars], 559332) != datet[36 chars], 559000, tzinfo=&lt;bson.tz_util.FixedOffset obj[15 chars]240&gt;)
  </code></pre>
  
  <p>Why is there a difference in the times that are being stored and extracted? I could add a delta to check a range of times, but I'd prefer to store the exact time. How can I fix my test to account for these microsecond differences?</p>