From 040ae9104055d7a4789b46ead215287d5d6ff305 Mon Sep 17 00:00:00 2001 From: shugo Date: Mon, 28 Sep 2015 07:10:25 +0000 Subject: * 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 --- lib/net/ftp.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'lib') 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(?\d{4})(?\d{2})(?\d{2}) + (?\d{2})(?\d{2})(?\d{2}) + (\.(?\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 # -- cgit v1.2.3