diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-07-01 09:11:21 +0100 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-07-01 09:11:21 +0100 |
commit | 98b46bf5e201307cae56ee14bf41363a539779c5 (patch) | |
tree | 8a362b8d7b3fbe9d2800580f1e962a99efb0fe2d /activesupport/lib/active_support | |
parent | 73bddd12a4311debf264e8beb226b296ef568dd6 (diff) | |
download | rails-98b46bf5e201307cae56ee14bf41363a539779c5.tar.gz rails-98b46bf5e201307cae56ee14bf41363a539779c5.tar.bz2 rails-98b46bf5e201307cae56ee14bf41363a539779c5.zip |
Make Time#change work with offsets other than UTC or local
Use Time.new to create times where the current offset is not zero or
not in the local time zone - closes #4847 and #6651.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index e8623a9c54..0a71fc117c 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -86,16 +86,21 @@ class Time # (hour, min, sec, usec) reset cascadingly, so if only the hour is passed, then minute, sec, and usec is set to 0. If the hour and # minute is passed, then sec and usec is set to 0. def change(options) - ::Time.send( - utc? ? :utc_time : :local_time, - options.fetch(:year, year), - options.fetch(:month, month), - options.fetch(:day, day), - options.fetch(:hour, hour), - options.fetch(:min, options[:hour] ? 0 : min), - options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec), - options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000)) - ) + new_year = options.fetch(:year, year) + new_month = options.fetch(:month, month) + new_day = options.fetch(:day, day) + new_hour = options.fetch(:hour, hour) + new_min = options.fetch(:min, options[:hour] ? 0 : min) + new_sec = options.fetch(:sec, (options[:hour] || options[:min]) ? 0 : sec) + new_usec = options.fetch(:usec, (options[:hour] || options[:min] || options[:sec]) ? 0 : Rational(nsec, 1000)) + + if utc? + ::Time.utc(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) + elsif zone + ::Time.local(new_year, new_month, new_day, new_hour, new_min, new_sec, new_usec) + else + ::Time.new(new_year, new_month, new_day, new_hour, new_min, new_sec + (new_usec.to_r / 1000000), utc_offset) + end end # Uses Date to provide precise Time calculations for years, months, and days. |