Browse files

Merge branch 'master' of git://github.com/rails/rails

  • Loading branch information...
2 parents 342dcfe + 9620372 commit be7fd8168aa4776dccfe2e670cb9451204449dd4 @dhh dhh committed Apr 13, 2008
View
8 activesupport/CHANGELOG
@@ -1,5 +1,13 @@
*SVN*
+* Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller]
+
+* Add #getutc alias for DateTime#utc [Geoff Buesing]
+
+* Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing]
+
+* TimeWithZone respects config.active_support.use_standard_json_time_format [Geoff Buesing]
+
* Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. [rick]
* Improve documentation. [Xavier Noria]
View
1 activesupport/lib/active_support/core_ext/date_time/calculations.rb
@@ -86,6 +86,7 @@ def end_of_day
def utc
new_offset(0)
end
+ alias_method :getutc, :utc
# Returns true if offset == 0
def utc?
View
13 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -88,18 +88,21 @@ def advance(options)
end
# Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension
- # Do not use this method in combination with x.months, use months_ago instead!
def ago(seconds)
self.since(-seconds)
end
# Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around
- # the Numeric extension. Do not use this method in combination with x.months, use months_since instead!
+ # the Numeric extension.
def since(seconds)
- initial_dst = self.dst? ? 1 : 0
f = seconds.since(self)
- final_dst = f.dst? ? 1 : 0
- (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
+ if ActiveSupport::Duration === seconds
+ f
+ else
+ initial_dst = self.dst? ? 1 : 0
+ final_dst = f.dst? ? 1 : 0
+ (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f
+ end
rescue
self.to_datetime.since(seconds)
end
View
33 activesupport/lib/active_support/time_with_zone.rb
@@ -77,7 +77,11 @@ def xmlschema
alias_method :iso8601, :xmlschema
def to_json(options = nil)
- %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
+ if ActiveSupport.use_standard_json_time_format
+ utc.xmlschema.inspect
+ else
+ %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}")
+ end
end
def to_yaml(options = {})
@@ -130,7 +134,8 @@ def eql?(other)
# If wrapped #time is a DateTime, use DateTime#since instead of #+
# Otherwise, just pass on to #method_missing
def +(other)
- time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other)
+ result = utc.acts_like?(:date) ? utc.since(other) : utc + other
+ result.in_time_zone(time_zone)
end
# If a time-like object is passed in, compare it with #utc
@@ -140,10 +145,23 @@ def -(other)
if other.acts_like?(:time)
utc - other
else
- time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other)
+ result = utc.acts_like?(:date) ? utc.ago(other) : utc - other
+ result.in_time_zone(time_zone)
end
end
+ def since(other)
+ utc.since(other).in_time_zone(time_zone)
+ end
+
+ def ago(other)
+ utc.ago(other).in_time_zone(time_zone)
+ end
+
+ def advance(options)
+ utc.advance(options).in_time_zone(time_zone)
+ end
+
def usec
time.respond_to?(:usec) ? time.usec : 0
end
@@ -204,13 +222,8 @@ def respond_to?(sym)
# Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone
def method_missing(sym, *args, &block)
- if %w(+ - since ago advance).include?(sym.to_s)
- result = utc.__send__(sym, *args, &block)
- result.acts_like?(:time) ? result.in_time_zone(time_zone) : result
- else
- result = time.__send__(sym, *args, &block)
- result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
- end
+ result = time.__send__(sym, *args, &block)
+ result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result
end
private
View
1 activesupport/test/core_ext/date_time_ext_test.rb
@@ -240,6 +240,7 @@ def test_utc
assert_equal DateTime.civil(2005, 2, 21, 15, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc
assert_equal DateTime.civil(2005, 2, 21, 10, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc
assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).utc
+ assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).getutc
end
def test_formatted_offset_with_utc
View
55 activesupport/test/core_ext/time_ext_test.rb
@@ -205,6 +205,31 @@ def test_daylight_savings_time_crossings_backward_end
end
end
+ def test_daylight_savings_time_crossings_backward_start_1day
+ with_env_tz 'US/Eastern' do
+ # dt: US: 2005 April 3rd 4:18am
+ assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(1.day), 'dt-1.day=>st'
+ assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(1.day), 'st-1.day=>st'
+ end
+ with_env_tz 'NZ' do
+ # dt: New Zealand: 2006 October 1st 4:18am
+ assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(1.day), 'dt-1.day=>st'
+ assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(1.day), 'st-1.day=>st'
+ end
+ end
+
+ def test_daylight_savings_time_crossings_backward_end_1day
+ with_env_tz 'US/Eastern' do
+ # st: US: 2005 October 30th 4:03am
+ assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(1.day), 'st-1.day=>dt'
+ assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(1.day), 'dt-1.day=>dt'
+ end
+ with_env_tz 'NZ' do
+ # st: New Zealand: 2006 March 19th 4:03am
+ assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(1.day), 'st-1.day=>dt'
+ assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt'
+ end
+ end
def test_since
assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1)
assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600)
@@ -227,6 +252,19 @@ def test_daylight_savings_time_crossings_forward_start
end
end
+ def test_daylight_savings_time_crossings_forward_start_1day
+ with_env_tz 'US/Eastern' do
+ # st: US: 2005 April 2nd 7:27pm
+ assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(1.day), 'st+1.day=>dt'
+ assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(1.day), 'dt+1.day=>dt'
+ end
+ with_env_tz 'NZ' do
+ # st: New Zealand: 2006 September 30th 7:27pm
+ assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(1.day), 'st+1.day=>dt'
+ assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(1.day), 'dt+1.day=>dt'
+ end
+ end
+
def test_daylight_savings_time_crossings_forward_start_tomorrow
with_env_tz 'US/Eastern' do
# st: US: 2005 April 2nd 7:27pm
@@ -240,7 +278,7 @@ def test_daylight_savings_time_crossings_forward_start_tomorrow
end
end
- def test_daylight_savings_time_crossings_forward_start_yesterday
+ def test_daylight_savings_time_crossings_backward_start_yesterday
with_env_tz 'US/Eastern' do
# st: US: 2005 April 2nd 7:27pm
assert_equal Time.local(2005,4,2,19,27,0), Time.local(2005,4,3,19,27,0).yesterday, 'dt-1.day=>st'
@@ -266,6 +304,19 @@ def test_daylight_savings_time_crossings_forward_end
end
end
+ def test_daylight_savings_time_crossings_forward_end_1day
+ with_env_tz 'US/Eastern' do
+ # dt: US: 2005 October 30th 12:45am
+ assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(1.day), 'dt+1.day=>st'
+ assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(1.day), 'st+1.day=>st'
+ end
+ with_env_tz 'NZ' do
+ # dt: New Zealand: 2006 March 19th 1:45am
+ assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(1.day), 'dt+1.day=>st'
+ assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(1.day), 'st+1.day=>st'
+ end
+ end
+
def test_daylight_savings_time_crossings_forward_end_tomorrow
with_env_tz 'US/Eastern' do
# dt: US: 2005 October 30th 12:45am
@@ -279,7 +330,7 @@ def test_daylight_savings_time_crossings_forward_end_tomorrow
end
end
- def test_daylight_savings_time_crossings_forward_end_yesterday
+ def test_daylight_savings_time_crossings_backward_end_yesterday
with_env_tz 'US/Eastern' do
# dt: US: 2005 October 30th 12:45am
assert_equal Time.local(2005,10,30,0,45,0), Time.local(2005,10,31,0,45,0).yesterday, 'st-1.day=>dt'
View
7 activesupport/test/core_ext/time_with_zone_test.rb
@@ -67,6 +67,13 @@ def test_to_json
assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json
end
end
+
+ def test_to_json_with_use_standard_json_time_format_config_set_to_true
+ old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, true
+ assert_equal "\"2000-01-01T00:00:00Z\"", @twz.to_json
+ ensure
+ ActiveSupport.use_standard_json_time_format = old
+ end
def test_strftime
silence_warnings do # silence warnings raised by tzinfo gem
View
2 railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* rake time:zones:local finds correct base utc offset for zones in the Southern Hemisphere [Geoff Buesing]
+
* Don't require rails/gem_builder during rails initialization, it's only needed for the gems:build task. [rick]
* script/performance/profiler compatibility with the ruby-prof >= 0.5.0. Closes #9176. [Catfish]
View
5 railties/lib/tasks/misc.rake
@@ -24,7 +24,10 @@ namespace :time do
desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time'
task :local do
- build_time_zone_list(:all, Time.now.beginning_of_year.utc_offset)
+ jan_offset = Time.now.beginning_of_year.utc_offset
+ jul_offset = Time.now.beginning_of_year.change(:month => 7).utc_offset
+ offset = jan_offset < jul_offset ? jan_offset : jul_offset
+ build_time_zone_list(:all, offset)
end
# to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600

0 comments on commit be7fd81

Please sign in to comment.