Date#since, #ago, #beginning_of_day, #end_of_day, #xmlschema return TimeWithZone when Time.zone_default is set

Signed-off-by: Xavier Noria <fxn@hashref.com>
This commit is contained in:
Geoff Buesing
2010-06-08 21:35:39 -05:00
committed by Xavier Noria
parent 1a5654851e
commit 4146a443b6
3 changed files with 65 additions and 6 deletions

View File

@@ -57,19 +57,19 @@ class Date
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
# and then subtracts the specified number of seconds # and then subtracts the specified number of seconds
def ago(seconds) def ago(seconds)
to_time.since(-seconds) to_time_in_current_zone.since(-seconds)
end end
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
# and then adds the specified number of seconds # and then adds the specified number of seconds
def since(seconds) def since(seconds)
to_time.since(seconds) to_time_in_current_zone.since(seconds)
end end
alias :in :since alias :in :since
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
def beginning_of_day def beginning_of_day
to_time to_time_in_current_zone
end end
alias :midnight :beginning_of_day alias :midnight :beginning_of_day
alias :at_midnight :beginning_of_day alias :at_midnight :beginning_of_day
@@ -77,7 +77,7 @@ class Date
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59) # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
def end_of_day def end_of_day
to_time.end_of_day to_time_in_current_zone.end_of_day
end end
def plus_with_duration(other) #:nodoc: def plus_with_duration(other) #:nodoc:

View File

@@ -82,6 +82,16 @@ class Date
::Time.send("#{form}_time", year, month, day) ::Time.send("#{form}_time", year, month, day)
end end
# Converts Date to a TimeWithZone in the current zone if Time.zone_default is set,
# otherwise converts Date to a Time via Date#to_time
def to_time_in_current_zone
if ::Time.zone_default
::Time.zone.local(year, month, day)
else
to_time
end
end
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day # Converts a Date instance to a DateTime, where the time is set to the beginning of the day
# and UTC offset is set to 0. # and UTC offset is set to 0.
# #
@@ -94,6 +104,6 @@ class Date
end if RUBY_VERSION < '1.9' end if RUBY_VERSION < '1.9'
def xmlschema def xmlschema
to_time.xmlschema to_time_in_current_zone.xmlschema
end end
end end

View File

@@ -287,18 +287,58 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45) assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45)
end end
def test_since_when_zone_default_is_set
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do
with_tz_default zone do
assert_equal zone.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45)
assert_equal zone, Date.new(2005,2,21).since(45).time_zone
end
end
end
def test_ago def test_ago
assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45) assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45)
end end
def test_ago_when_zone_default_is_set
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do
with_tz_default zone do
assert_equal zone.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45)
assert_equal zone, Date.new(2005,2,21).ago(45).time_zone
end
end
end
def test_beginning_of_day def test_beginning_of_day
assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day
end end
def test_beginning_of_day_when_zone_default_is_set
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do
with_tz_default zone do
assert_equal zone.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day
assert_equal zone, Date.new(2005,2,21).beginning_of_day.time_zone
end
end
end
def test_end_of_day def test_end_of_day
assert_equal Time.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day assert_equal Time.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day
end end
def test_end_of_day_when_zone_default_is_set
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do
with_tz_default zone do
assert_equal zone.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day
assert_equal zone, Date.new(2005,2,21).end_of_day.time_zone
end
end
end
def test_xmlschema def test_xmlschema
with_env_tz 'US/Eastern' do with_env_tz 'US/Eastern' do
assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema) assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema)
@@ -311,6 +351,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end end
end end
def test_xmlschema_when_zone_default_is_set
with_env_tz 'UTC' do
with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5
assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema)
assert_match(/^1980-06-28T00:00:00-04:?00$/, Date.new(1980, 6, 28).xmlschema)
end
end
end
def test_today def test_today
Date.stubs(:current).returns(Date.new(2000, 1, 1)) Date.stubs(:current).returns(Date.new(2000, 1, 1))
assert_equal false, Date.new(1999, 12, 31).today? assert_equal false, Date.new(1999, 12, 31).today?