diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-23 02:46:47 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-23 02:46:47 +0000 |
commit | abb24b484fcb306be17dd06a8a2d12ca5a035aeb (patch) | |
tree | 96de38f875c4048b27e6fae61de0d00111a66014 /activesupport | |
parent | 2e7b2f03448e5759113ce0c182ed99897342c7a9 (diff) | |
download | rails-abb24b484fcb306be17dd06a8a2d12ca5a035aeb.tar.gz rails-abb24b484fcb306be17dd06a8a2d12ca5a035aeb.tar.bz2 rails-abb24b484fcb306be17dd06a8a2d12ca5a035aeb.zip |
Adding Time and DateTime #formatted_offset, for outputting +HH:MM utc offset strings with cross-platform consistency
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8698 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
6 files changed, 47 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d2d8353cd8..f7ab486ad7 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Adding Time and DateTime #formatted_offset, for outputting +HH:MM utc offset strings with cross-platform consistency [Geoff Buesing] + * Adding alternate_utc_string option to TimeZone#formatted_offset. Removing unneeded TimeZone#offset. [Geoff Buesing] * Introduce ActiveSupport::TimeWithZone, for wrapping Time instances with a TimeZone. Introduce instance methods to Time for creating TimeWithZone instances, and class methods for managing a global time zone. [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index f9645fd3cf..59d742cde3 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -52,6 +52,15 @@ module ActiveSupport #:nodoc: end end + # Returns the utc_offset as an +HH:MM formatted string. Examples: + # + # datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24)) + # datetime.formatted_offset # => "-06:00" + # datetime.formatted_offset(false) # => "-0600" + def formatted_offset(colon = true, alternate_utc_string = nil) + utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon) + end + # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000" def readable_inspect to_s(:rfc822) diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index ffc9b05a8c..466a20c3bc 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -55,6 +55,14 @@ module ActiveSupport #:nodoc: to_default_s end end + + # Returns the utc_offset as an +HH:MM formatted string. Examples: + # + # Time.local(2000).formatted_offset # => "-06:00" + # Time.local(2000).formatted_offset(false) # => "-0600" + def formatted_offset(colon = true, alternate_utc_string = nil) + utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon) + end # Convert a Time object to a Date, dropping hour, minute, and second precision. # diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb index 1636b875e3..4f964a92e0 100644 --- a/activesupport/lib/active_support/json/encoders/time.rb +++ b/activesupport/lib/active_support/json/encoders/time.rb @@ -1,5 +1,5 @@ class Time def to_json(options = nil) #:nodoc: - %("#{strftime("%Y/%m/%d %H:%M:%S")} #{utc_offset.to_utc_offset_s(false)}") + %("#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") end end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index f69bdba127..768ea281d3 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -241,6 +241,18 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase 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 end + + def test_formatted_offset_with_utc + assert_equal '+00:00', DateTime.civil(2000).formatted_offset + assert_equal '+0000', DateTime.civil(2000).formatted_offset(false) + assert_equal 'UTC', DateTime.civil(2000).formatted_offset(true, 'UTC') + end + + def test_formatted_offset_with_local + dt = DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)) + assert_equal '-05:00', dt.formatted_offset + assert_equal '-0500', dt.formatted_offset(false) + end protected def with_timezone(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 5970411233..e9c7a1ed79 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -409,6 +409,21 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_acts_like_time assert Time.new.acts_like_time? end + + def test_formatted_offset_with_utc + assert_equal '+00:00', Time.utc(2000).formatted_offset + assert_equal '+0000', Time.utc(2000).formatted_offset(false) + assert_equal 'UTC', Time.utc(2000).formatted_offset(true, 'UTC') + end + + def test_formatted_offset_with_local + with_timezone 'US/Eastern' do + assert_equal '-05:00', Time.local(2000).formatted_offset + assert_equal '-0500', Time.local(2000).formatted_offset(false) + assert_equal '-04:00', Time.local(2000, 7).formatted_offset + assert_equal '-0400', Time.local(2000, 7).formatted_offset(false) + end + end protected def with_timezone(new_tz = 'US/Eastern') |