summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 07:10:25 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-28 07:10:25 +0000
commit040ae9104055d7a4789b46ead215287d5d6ff305 (patch)
tree2acb3ab276b28320e9a84f8f23988d96e13ded76
parent4191a6b90d3eeb63a31609dba29a1904efee3738 (diff)
* 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--ChangeLog5
-rw-r--r--lib/net/ftp.rb23
-rw-r--r--test/net/ftp/test_ftp.rb38
3 files changed, 53 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index 6ca1cbcd79..f384beb83e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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|