1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Geoff Buesing 2008-01-23 02:46:47 +00:00
parent 2e7b2f0344
commit abb24b484f
6 changed files with 47 additions and 1 deletions

View file

@ -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]

View file

@ -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)

View file

@ -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.
#

View file

@ -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

View file

@ -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')

View file

@ -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')