mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Introducing DateTime #utc, #utc? and #utc_offset, for duck-typing compatibility with Time. Closes #10002
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8649 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
61e550ade9
commit
47ff3aa91f
3 changed files with 44 additions and 0 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Introducing DateTime #utc, #utc? and #utc_offset, for duck-typing compatibility with Time. Closes #10002 [Geoff Buesing]
|
||||
|
||||
* Time#to_json uses Numeric#to_utc_offset_s to output a cross-platform-consistent representation without having to convert to DateTime. References #9750 [Geoff Buesing]
|
||||
|
||||
* Refactor number-to-HH:MM-string conversion logic from TimeZone#formatted_offset to a reusable Numeric#to_utc_offset_s method. [Geoff Buesing]
|
||||
|
|
|
@ -71,6 +71,26 @@ module ActiveSupport #:nodoc:
|
|||
def end_of_day
|
||||
change(:hour => 23, :min => 59, :sec => 59)
|
||||
end
|
||||
|
||||
# Adjusts DateTime to UTC by adding its offset value; offset is set to 0
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)) # => Mon, 21 Feb 2005 10:11:12 -0600
|
||||
# DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc # => Mon, 21 Feb 2005 16:11:12 +0000
|
||||
def utc
|
||||
new_offset(0)
|
||||
end
|
||||
|
||||
# Returns true if offset == 0
|
||||
def utc?
|
||||
offset == 0
|
||||
end
|
||||
|
||||
# Returns the offset value in seconds
|
||||
def utc_offset
|
||||
(offset * 86400).to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -220,6 +220,28 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_utc?
|
||||
assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12).utc?
|
||||
assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc?
|
||||
assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc?
|
||||
assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc?
|
||||
end
|
||||
|
||||
def test_utc_offset
|
||||
assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12).utc_offset
|
||||
assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc_offset
|
||||
assert_equal 21600, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc_offset
|
||||
assert_equal( -21600, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc_offset )
|
||||
assert_equal( -18000, DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc_offset )
|
||||
end
|
||||
|
||||
def test_utc
|
||||
assert_equal DateTime.civil(2005, 2, 21, 16, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).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
|
||||
end
|
||||
|
||||
protected
|
||||
def with_timezone(new_tz = 'US/Eastern')
|
||||
old_tz, ENV['TZ'] = ENV['TZ'], new_tz
|
||||
|
|
Loading…
Reference in a new issue