Ruby 1.9 compat: normalize date and time xmlschema to match Ruby's formatting [chuyeow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8398 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
3d90733e93
commit
bb152cd8ca
@ -20,6 +20,9 @@ def self.included(base) #:nodoc:
|
||||
|
||||
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
||||
remove_method :to_time if base.instance_methods.include?(:to_time)
|
||||
|
||||
# Ruby 1.9 has Date#xmlschema which converts to a string without the time component.
|
||||
remove_method :xmlschema if base.instance_methods.include?(:xmlschema)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -9,6 +9,10 @@ def self.included(base)
|
||||
alias_method :to_s, :to_formatted_s
|
||||
alias_method :default_inspect, :inspect
|
||||
alias_method :inspect, :readable_inspect
|
||||
|
||||
# Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows
|
||||
# DateTimes outside the range of what can be created with Time.
|
||||
remove_method :to_time if base.instance_methods.include?(:to_time)
|
||||
end
|
||||
end
|
||||
|
||||
@ -38,16 +42,16 @@ def to_date
|
||||
# If self has an offset other than 0, self will just be returned unaltered, since there's no clean way to map it to a Time
|
||||
def to_time
|
||||
self.offset == 0 ? ::Time.utc_time(year, month, day, hour, min, sec) : self
|
||||
end
|
||||
end
|
||||
|
||||
# To be able to keep Times, Dates and DateTimes interchangeable on conversions
|
||||
def to_datetime
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
def xmlschema
|
||||
strftime("%Y-%m-%dT%H:%M:%S#{offset == 0 ? 'Z' : '%Z'}")
|
||||
end
|
||||
strftime("%Y-%m-%dT%H:%M:%S%Z")
|
||||
end if RUBY_VERSION < '1.9'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -32,7 +32,6 @@ def test_to_datetime
|
||||
assert_equal DateTime.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_datetime
|
||||
end
|
||||
|
||||
# FIXME: ruby 1.9 compat
|
||||
def test_to_time
|
||||
assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0, 0).to_time
|
||||
assert_equal Time.utc_time(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0, 0).to_time
|
||||
@ -200,9 +199,9 @@ def test_last_month_on_31st
|
||||
end
|
||||
|
||||
def test_xmlschema
|
||||
assert_equal '1880-02-28T15:15:10Z', DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema
|
||||
assert_equal '1980-02-28T15:15:10Z', DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema
|
||||
assert_equal '2080-02-28T15:15:10Z', DateTime.civil(2080, 2, 28, 15, 15, 10).xmlschema
|
||||
assert_match(/^1880-02-28T15:15:10\+00:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10).xmlschema)
|
||||
assert_match(/^1980-02-28T15:15:10\+00:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10).xmlschema)
|
||||
assert_match(/^2080-02-28T15:15:10\+00:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10).xmlschema)
|
||||
assert_match(/^1880-02-28T15:15:10-06:?00$/, DateTime.civil(1880, 2, 28, 15, 15, 10, -0.25).xmlschema)
|
||||
assert_match(/^1980-02-28T15:15:10-06:?00$/, DateTime.civil(1980, 2, 28, 15, 15, 10, -0.25).xmlschema)
|
||||
assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema)
|
||||
|
Loading…
Reference in New Issue
Block a user