Skip to content

Commit f556846

Browse files
authored
MONGOID-5295 Use Time.zone.now instead of Time.now(.utc) (#5260)
* MONGOID-5295 make time zone changes * MONGOID-5295 add tests
1 parent 12e0278 commit f556846

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

lib/mongoid/timestamps/created.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Created
2323
# person.set_created_at
2424
def set_created_at
2525
if !timeless? && !created_at
26-
time = Time.now.utc
26+
time = Time.configured.now
2727
self.updated_at = time if is_a?(Updated) && !updated_at_changed?
2828
self.created_at = time
2929
end

lib/mongoid/timestamps/updated.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Updated
2424
# person.set_updated_at
2525
def set_updated_at
2626
if able_to_set_updated_at?
27-
self.updated_at = Time.now.utc unless updated_at_changed?
27+
self.updated_at = Time.configured.now unless updated_at_changed?
2828
end
2929

3030
clear_timeless_option

lib/mongoid/touchable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module InstanceMethods
2222
# @return [ true/false ] false if record is new_record otherwise true.
2323
def touch(field = nil)
2424
return false if _root.new_record?
25-
current = Time.now
25+
current = Time.configured.now
2626
field = database_field_name(field)
2727
write_attribute(:updated_at, current) if respond_to?("updated_at=")
2828
write_attribute(field, current) if field

spec/mongoid/timestamps_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
Dokument.fields
1515
end
1616

17+
let(:time_zone) { "Pacific Time (US & Canada)" }
18+
19+
around do |ex|
20+
z = Time.zone
21+
Time.zone = time_zone
22+
ex.run
23+
Time.zone = z
24+
end
25+
1726
before do
1827
document.run_callbacks(:create)
1928
document.run_callbacks(:save)
@@ -35,6 +44,14 @@
3544
expect(document.updated_at).to be_within(10).of(Time.now.utc)
3645
end
3746

47+
it "sets the created_at to the correct time zone" do
48+
expect(document.created_at.time_zone.name).to eq(time_zone)
49+
end
50+
51+
it "sets the updated_at to the correct time zone" do
52+
expect(document.updated_at.time_zone.name).to eq(time_zone)
53+
end
54+
3855
it "ensures created_at equals updated_at on new records" do
3956
expect(document.updated_at).to eq(document.created_at)
4057
end

0 commit comments

Comments
 (0)