(Note: I opened #2093 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?
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.
(Note: I opened #2093 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:In 2.4, I get this output:
It converts the local time into UTC time and leaves the UTC time as-is. But in 3.0.rc, I get this output:
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.