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

* lib/net/ftp.rb (mtime): parse decimal fractions of a second as

specified in RFC 3659.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51963 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2015-09-28 07:10:25 +00:00
parent 4191a6b90d
commit 040ae91040
3 changed files with 52 additions and 12 deletions

View file

@ -1,3 +1,8 @@
Mon Sep 28 16:07:08 2015 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/ftp.rb (mtime): parse decimal fractions of a second as
specified in RFC 3659.
Mon Sep 28 10:31:12 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* test/test_forwardable.rb: Write basic tests for lib/forwardable.

View file

@ -888,14 +888,15 @@ module Net
CASE_INDEPENDENT_PARSER = ->(value) { value.downcase }
DECIMAL_PARSER = ->(value) { value.to_i }
OCTAL_PARSER = ->(value) { value.to_i(8) }
TIME_PARSER = ->(value) {
t = Time.strptime(value.sub(/\.\d+\z/, "") + "Z", "%Y%m%d%H%M%S%z")
fractions = value.slice(/\.(\d+)\z/, 1)
if fractions
t + fractions.to_i.quo(10 ** fractions.size)
else
t
TIME_PARSER = ->(value, local = false) {
unless /\A(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})
(?<hour>\d{2})(?<min>\d{2})(?<sec>\d{2})
(\.(?<fractions>\d+))?/x =~ value
raise FTPProtoError, "invalid time-val: #{value}"
end
usec = fractions.to_i * 10 ** (6 - fractions.to_s.size)
Time.send(local ? :local : :utc,
year, month, day, hour, min, sec, fractions)
}
FACT_PARSERS = Hash.new(CASE_DEPENDENT_PARSER)
FACT_PARSERS["size"] = DECIMAL_PARSER
@ -1028,16 +1029,12 @@ module Net
end
end
MDTM_REGEXP = /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ # :nodoc:
#
# Returns the last modification time of the (remote) file. If +local+ is
# +true+, it is returned as a local time, otherwise it's a UTC time.
#
def mtime(filename, local = false)
str = mdtm(filename)
ary = str.scan(MDTM_REGEXP)[0].collect {|i| i.to_i}
return local ? Time.local(*ary) : Time.gm(*ary)
return TIME_PARSER.(mdtm(filename), local)
end
#

View file

@ -1097,6 +1097,44 @@ EOF
end
end
def test_mtime
commands = []
server = create_ftp_server { |sock|
sock.print("220 (test_ftp).\r\n")
commands.push(sock.gets)
sock.print("213 20150910161739\r\n")
commands.push(sock.gets)
sock.print("213 20150910161739\r\n")
commands.push(sock.gets)
sock.print("213 20150910161739.123456\r\n")
commands.push(sock.gets)
sock.print("213 2015091016173\r\n")
}
begin
begin
ftp = Net::FTP.new
ftp.connect(SERVER_ADDR, server.port)
assert_equal(Time.utc(2015, 9, 10, 16, 17, 39), ftp.mtime("foo.txt"))
assert_equal(Time.local(2015, 9, 10, 16, 17, 39),
ftp.mtime("foo.txt", true))
assert_equal(Time.utc(2015, 9, 10, 16, 17, 39, 123456),
ftp.mtime("bar.txt"))
assert_raise(Net::FTPProtoError) do
ftp.mtime("quux.txt")
end
assert_match("MDTM foo.txt\r\n", commands.shift)
assert_match("MDTM foo.txt\r\n", commands.shift)
assert_match("MDTM bar.txt\r\n", commands.shift)
assert_match("MDTM quux.txt\r\n", commands.shift)
assert_equal(nil, commands.shift)
ensure
ftp.close if ftp
end
ensure
server.close
end
end
def test_system
commands = []
server = create_ftp_server { |sock|