diff options
author | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-09-28 07:10:25 +0000 |
---|---|---|
committer | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-09-28 07:10:25 +0000 |
commit | 8085eba3a203959588129eaced349c55a3a97377 (patch) | |
tree | 2acb3ab276b28320e9a84f8f23988d96e13ded76 | |
parent | 6b3b9965421f8a8320af0f5c10d285f5a2739d27 (diff) | |
download | ruby-8085eba3a203959588129eaced349c55a3a97377.tar.gz |
* 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
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | lib/net/ftp.rb | 23 | ||||
-rw-r--r-- | test/net/ftp/test_ftp.rb | 38 |
3 files changed, 53 insertions, 13 deletions
@@ -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. diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index fd11acb8b5..795e1cbdc3 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -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 - end + 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 # diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb index 9fc163bc7b..c84dbda10f 100644 --- a/test/net/ftp/test_ftp.rb +++ b/test/net/ftp/test_ftp.rb @@ -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| |