ruby-changes:39882
From: shugo <ko1@a...>
Date: Mon, 28 Sep 2015 16:10:49 +0900 (JST)
Subject: [ruby-changes:39882] shugo:r51963 (trunk): * lib/net/ftp.rb (mtime): parse decimal fractions of a second as
shugo 2015-09-28 16:10:25 +0900 (Mon, 28 Sep 2015) New Revision: 51963 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51963 Log: * lib/net/ftp.rb (mtime): parse decimal fractions of a second as specified in RFC 3659. Modified files: trunk/ChangeLog trunk/lib/net/ftp.rb trunk/test/net/ftp/test_ftp.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 51962) +++ ChangeLog (revision 51963) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Sep 28 16:07:08 2015 Shugo Maeda <shugo@r...> + + * 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@r...> * test/test_forwardable.rb: Write basic tests for lib/forwardable. Index: lib/net/ftp.rb =================================================================== --- lib/net/ftp.rb (revision 51962) +++ lib/net/ftp.rb (revision 51963) @@ -888,14 +888,15 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L888 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 https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L1029 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 # Index: test/net/ftp/test_ftp.rb =================================================================== --- test/net/ftp/test_ftp.rb (revision 51962) +++ test/net/ftp/test_ftp.rb (revision 51963) @@ -1097,6 +1097,44 @@ EOF https://github.com/ruby/ruby/blob/trunk/test/net/ftp/test_ftp.rb#L1097 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| -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/