-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
(Note: I opened MONGOID-2094 a bit ago thinking I had tracked this issue down, but that turned out to be a bug in my reproduction and I closed it. I think I've got it now though.)
When you provide Mongoid 3 with a DateTime, it converts it to UTC if you're using use_utc. However, the conversion seems to ignore whether the time is already in UTC:
Mongoid.use_utc = true class Post include Mongoid::Document field :time, type: DateTime end puts "Current time in UTC: #{DateTime.now.utc}" Post.create(time: DateTime.now).save! an_hour_ago = DateTime.now - 1.hour #express the time in the local zone puts Post.where(time: {"$gt" => an_hour_ago}).empty? #be explicit about being in UTC puts Post.where(time: {"$gt" => an_hour_ago.utc}).empty?
In 2.4, I get this output:
Current time in UTC: 2012-06-11T03:33:21+00:00 MONGODB (0ms) repro2['system.namespaces'].find({}) MONGODB (0ms) repro2['$cmd'].find({:create=>"posts"}).limit(-1) MONGODB (0ms) repro2['posts'].insert([{"_id"=>BSON::ObjectId('4fd567018ace235ee7000001'), "time"=>2012-06-11 03:33:21 UTC}]) MONGODB (0ms) repro2['posts'].find({:time=>{"$gt"=>2012-06-11 02:33:21 UTC}}, {:_id=>1}).limit(-1) false MONGODB (0ms) repro2['posts'].find({:time=>{"$gt"=>2012-06-11 02:33:21 UTC}}, {:_id=>1}).limit(-1) false
It converts the local time into UTC time and leaves the UTC time as-is. But in 3.0.rc, I get this output:
Current time in UTC: 2012-06-11T03:32:59+00:00 MOPED: localhost:27017 INSERT database=repro3 collection=posts documents=[{"_id"=>4fd566eb8ace23735e000001, "time"=>2012-06-11 03:32:59 UTC}] flags=[] (0.0ms) MOPED: localhost:27017 COMMAND database=repro3 command={:count=>:posts, :query=>{"time"=>{"$gt"=>2012-06-11 02:32:59 UTC}}} (0.0ms) false MOPED: localhost:27017 COMMAND database=repro3 command={:count=>:posts, :query=>{"time"=>{"$gt"=>2012-06-11 06:32:59 UTC}}} (0.0ms) true
So it seems to be adding 4 hours (my UTC offset) to the time that's already in UTC, meaning it ends up querying for several hours ahead of what it should be.