Skip to content

Commit 05c86f1

Browse files
MONGOID-5488 - Bugfix: Date#__evolve_time__ should use the configured zone instead of Time.local (i.e. respecting Mongoid.use_activesupport_time_zone) (#5471)
* Date#__evolve_time__ should use the configured zone. Currently it uses "local" (not "UTC") which is not affected by Rails configuration. * Make consistent with Mongoize * Add changelog * Update docs/release-notes/mongoid-9.0.txt * Update docs/release-notes/mongoid-9.0.txt * Update mongoid-9.0.txt Co-authored-by: shields <shields@tablecheck.com> Co-authored-by: Oleg Pudeyev <39304720+p-mongo@users.noreply.github.com>
1 parent 2d590d9 commit 05c86f1

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

docs/release-notes/mongoid-9.0.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,36 @@ any Mongoid-specific usages of this class, and change them to
4040
``Mongoid::Errors::AttributeNotLoaded``. Note additionally that
4141
``AttributeNotLoaded`` inherits from ``Mongoid::Errors::MongoidError``,
4242
while ``ActiveModel::MissingAttributeError`` does not.
43+
44+
45+
Use configured time zone to typecast Date to Time in queries
46+
-------------------------------------------------------------
47+
48+
When querying for a Time field using a Date value, Mongoid now correctly
49+
considers the ``Mongoid.use_activesupport_time_zone`` configuration option
50+
to perform type conversion.
51+
52+
.. code-block:: ruby
53+
54+
Mongoid.use_activesupport_time_zone = true
55+
56+
class Magazine
57+
include Mongoid::Document
58+
59+
field :published_at, type: Time
60+
end
61+
62+
Time.zone = 'Asia/Tokyo'
63+
64+
Magazine.gte(published_at: Date.parse('2022-09-26'))
65+
#=> will return all results on or after Sept 26th, 2022
66+
# at 0:00 in Asia/Tokyo time zone.
67+
68+
In prior Mongoid versions, the above code would ignore the
69+
``Mongoid.use_activesupport_time_zone`` setting and behave as if
70+
it were false, i.e. always using the system time zone to perform
71+
the type conversion.
72+
73+
Note that in prior Mongoid versions, typecasting Date to Time during
74+
persistence operations was already correctly using the
75+
``Mongoid.use_activesupport_time_zone`` setting.

lib/mongoid/criteria/queryable/extensions/date.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def __evolve_date__
2323
# @example Evolve the date.
2424
# date.__evolve_time__
2525
#
26-
# @return [ Time ] The date as a local time.
26+
# @return [ Time | ActiveSupport::TimeWithZone ] The date as a local time.
2727
def __evolve_time__
28-
::Time.local(year, month, day)
28+
::Time.configured.local(year, month, day)
2929
end
3030

3131
module ClassMethods

spec/mongoid/criteria/queryable/extensions/date_spec.rb

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,54 @@
2525

2626
describe "#__evolve_time__" do
2727

28-
let(:date) do
29-
Date.new(2010, 1, 1)
30-
end
28+
context "when using ActiveSupport's time zone" do
29+
include_context 'using AS time zone'
3130

32-
let(:evolved) do
33-
date.__evolve_time__
34-
end
31+
let(:date) do
32+
Date.new(2010, 1, 1)
33+
end
3534

36-
let(:expected) do
37-
Time.local(2010, 1, 1, 0, 0, 0)
35+
let(:expected_time) do
36+
Time.zone.local(2010, 1, 1, 0, 0, 0, 0)
37+
end
38+
39+
let(:evolved) do
40+
date.__evolve_time__
41+
end
42+
43+
it 'is an AS::TimeWithZone' do
44+
expect(evolved.class).to eq(ActiveSupport::TimeWithZone)
45+
end
46+
47+
it 'is equal to expected time' do
48+
expect(expected_time).to be_a(ActiveSupport::TimeWithZone)
49+
expect(evolved).to eq(expected_time)
50+
end
3851
end
3952

40-
it "returns the time" do
41-
expect(evolved).to eq(expected)
53+
context "when not using ActiveSupport's time zone" do
54+
include_context 'not using AS time zone'
55+
56+
let(:date) do
57+
Date.new(2010, 1, 1)
58+
end
59+
60+
let(:expected_time) do
61+
Time.local(2010, 1, 1, 0, 0, 0, 0)
62+
end
63+
64+
let(:evolved) do
65+
date.__evolve_time__
66+
end
67+
68+
it 'is a Time' do
69+
expect(evolved.class).to eq(Time)
70+
end
71+
72+
it 'is equal to expected time' do
73+
expect(expected_time).to be_a(Time)
74+
expect(evolved).to eq(expected_time)
75+
end
4276
end
4377
end
4478

0 commit comments

Comments
 (0)