Skip to content

Commit

Permalink
added support for ActiveSupport::TimeZone
Browse files Browse the repository at this point in the history
  • Loading branch information
sskirby committed Sep 24, 2010
1 parent 835a503 commit ca7db51
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
29 changes: 29 additions & 0 deletions lib/mongoid/config.rb
Expand Up @@ -48,6 +48,35 @@ def use_utc=(value)
attr_reader :use_utc
alias_method :use_utc?, :use_utc

# Sets whether the times returned from the database use the ruby or
# the ActiveSupport time zone.
# If you omit this setting, then times will use the ruby time zone.
#
# Example:
#
# <tt>Config.use_activesupport_time_zone = true</tt>
#
# Returns:
#
# A boolean
def use_activesupport_time_zone=(value)
@use_activesupport_time_zone = value || false
end

# Sets whether the times returned from the database use the ruby or
# the ActiveSupport time zone.
# If the setting is false, then times will use the ruby time zone.
#
# Example:
#
# <tt>Config.use_activesupport_time_zone</tt>
#
# Returns:
#
# A boolean
attr_reader :use_activesupport_time_zone
alias_method :use_activesupport_time_zone?, :use_activesupport_time_zone

# Sets the Mongo::DB master database to be used. If the object trying to be
# set is not a valid +Mongo::DB+, then an error will be raised.
#
Expand Down
11 changes: 6 additions & 5 deletions lib/mongoid/extensions/time_conversions.rb
Expand Up @@ -9,11 +9,12 @@ def set(value)

def get(value)
return nil if value.blank?
if Mongoid::Config.instance.use_utc?
value
else
value.getlocal
value = value.getlocal unless Mongoid::Config.instance.use_utc?
if Mongoid::Config.instance.use_activesupport_time_zone?
time_zone = Mongoid::Config.instance.use_utc? ? 'UTC' : Time.zone
value = value.in_time_zone(time_zone)
end
value
end

protected
Expand All @@ -32,4 +33,4 @@ def convert_to_time(value)
end
end
end
end
end
63 changes: 62 additions & 1 deletion spec/unit/mongoid/extensions/time_conversions_spec.rb
Expand Up @@ -39,6 +39,7 @@
context "when given a Time" do
it "converts to a utc time" do
Time.set(@time).utc_offset.should == 0
Time.set(@time).utc_offset.should == 0
end

it "strips miliseconds" do
Expand Down Expand Up @@ -98,14 +99,73 @@
Time.get(@time).should == @time
end
end

context "when using the ActiveSupport time zone" do
before do
Mongoid::Config.instance.use_activesupport_time_zone = true
Time.zone = "Stockholm"
end
after do
Time.zone = nil
Mongoid::Config.instance.use_activesupport_time_zone = false
end

it "returns an ActiveSupport::TimeWithZone" do
Time.get(@time).class.should == ActiveSupport::TimeWithZone
end

context "when the local time is not observing daylight saving" do
before { @time = Time.utc(2010, 11, 19, 12) }

it "returns the local time" do
Time.get(@time).should == Time.zone.local(2010, 11, 19, 13)
end
end

context "when the local time is observing daylight saving" do
before { @time = Time.utc(2010, 9, 19, 12) }

it "returns the local time" do
Time.get(@time).should == Time.zone.local(2010, 9, 19, 14)
end
end

context "when we have a time close to midnight" do
before { @time = Time.utc(2010, 11, 19, 0, 30) }

it "change it back to the equivalent local time" do
Time.get(@time).should == Time.zone.local(2010, 11, 19, 1, 30)
end
end
end
end

context "when the time zone is defined as UTC" do
before { Mongoid::Config.instance.use_utc = true }
after { Mongoid::Config.instance.use_utc = false }

it "returns utc" do
Time.get(@time.dup.utc).utc_offset.should == 0
Time.get(@time.dup.utc).utc_offset.should == 0
end

context "when using the ActiveSupport time zone" do
before do
Mongoid::Config.instance.use_activesupport_time_zone = true
Time.zone = "Stockholm"
@time = Time.utc(2010, 11, 19, 0, 30)
end
after do
Time.zone = nil
Mongoid::Config.instance.use_activesupport_time_zone = false
end

it "returns utc" do
Time.get(@time).should == ActiveSupport::TimeZone['UTC'].local(2010, 11, 19, 0, 30)
end

it "returns an ActiveSupport::TimeWithZone" do
Time.get(@time).class.should == ActiveSupport::TimeWithZone
end
end
end

Expand All @@ -114,6 +174,7 @@
Time.get(nil).should be_nil
end
end

end

describe "round trip - set then get" do
Expand Down

0 comments on commit ca7db51

Please sign in to comment.