diff options
Diffstat (limited to 'activesupport/lib/active_support')
3 files changed, 18 insertions, 1 deletions
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 |