Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,48 @@ def httpdate(date)
# You must require 'time' to use this method.
#
def xmlschema(time)
if /\A\s*
pattern = /\A\s*
(-?\d+)-(\d\d)-(\d\d)
T
(\d\d):(\d\d):(\d\d)
(\.\d+)?
(Z|[+-]\d\d(?::?\d\d)?)?
\s*\z/ix =~ time
\s*\z/ix
_xmlschema(pattern, time)
end
alias iso8601 xmlschema

#
# Parses +time+ as a dateTime defined by RFC3339 and converts it to
# a Time object.
#
# ArgumentError is raised if +time+ is not compliant with the format or if
# the Time class cannot represent the specified time.
#
# See #xmlschema for more information on this format.
#
# require 'time'
#
# Time.rfc3339("2011-10-05T22:26:12-04:00")
# #=> 2011-10-05 22:26:12-04:00
#
# You must require 'time' to use this method.
#
def rfc3339(time)
pattern = /\A\s*
(-?\d{4})-(\d\d)-(\d\d)
[T\s]
(\d\d):(\d\d):(\d\d)
(\.\d+)?
(Z|[+-]\d\d(?::?\d\d)?)?
\s*\z/ix
_xmlschema(pattern, time)
end

private

def _xmlschema(pattern, time)
if pattern =~ time
year = $1.to_i
mon = $2.to_i
day = $3.to_i
Expand All @@ -656,7 +691,7 @@ def xmlschema(time)
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
end
end
alias iso8601 xmlschema

end # class << self

#
Expand Down Expand Up @@ -731,4 +766,5 @@ def xmlschema(fraction_digits=0)
end
end
alias iso8601 xmlschema unless method_defined?(:iso8601)
alias rfc3339 xmlschema
end
1 change: 1 addition & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ def test_huge_precision
test = $1
define_method(test) {__send__(sub, :xmlschema)}
define_method(test.sub(/xmlschema/, 'iso8601')) {__send__(sub, :iso8601)}
define_method(test.sub(/xmlschema/, 'rfc3339')) {__send__(sub, :rfc3339)}
end

def test_parse_with_various_object
Expand Down