Sha256: 2713c57fae699306e8c4236461934ea3295e5453e7a7c50ec966a81736552ee6

Contents?: true

Size: 1.65 KB

Versions: 8

Compression:

Stored size: 1.65 KB

Contents

class String
  # 清除干扰数据
  # 清除包含: 空格,回车
  #
  def jagger_del_inter
    self.gsub(/(?:\n|\t|\r| | | |)/, "")
  end

  # 创建时间: 2019/5/6 18:11
  # 更新时间: 2019/5/6
  # 作者: Jagger
  # 方法名称: jagger_to_array
  # 方法说明: 字符串分割成数组
  # 调用方式: #jagger_to_array
  #
  # @return Array
  #
  def jagger_to_array
    self.split(/(?:\n|\t|\r| | | )+/)
  end

  # 转换成时间格式
  def jagger_to_time

    # 然后先遍历所有格式
    # 模糊匹配格式,放前面的优先匹配
    # 如果 "%Y年%m月%d日%H:%M" 在 "%Y年%m月%d日%H:%M:%S" 前面
    # 则 "2018年01月01日12:01:30".jagger_to_time => 2018-01-01 12:01:00 +0800
    # 秒会被去掉
    [
        "%Y年%m月%d日%H:%M:%S",
        "%Y年%m月%d日%H:%M",


        "%Y年%m月%d日 %H:%M:%S",
        "%Y年%m月%d日 %H:%M",

        "%Y-%m-%d %H:%M:%S",
        "%Y-%m-%d %H:%M",

        "%Y-%m-%d%H:%M:%S",
        "%Y-%m-%d%H:%M",

        "%Y%m%d%H%M%S",
        "%Y%m%d%H%M",

        "%m月%d日 %H%M%S",
        "%m月%d日 %H%M",

        "%m月%d日%H%M%S",
        "%m月%d日%H%M",

        "%m月%d日 %H:%M:%S",
        "%m月%d日 %H:%M",

        "%m月%d日%H:%M:%S",
        "%m月%d日%H:%M",

        "%Y-%m-%d",
        "%Y%m%d",

        "%Y年%m月%d日",

        "%Y年%m月",
        "%m月%d日",

    ].each do |v|
      begin
        return Time.strptime(self, v)
      rescue => error

      end
    end

    return Time.at(self.to_i / 1000.0) if self.length == 13
    return Time.at(self.to_i) if self.length == 10

    # 最后用 Time通用类型尝试
    return Time.parse(self)
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
http_crawler-0.3.1.30 lib/http_crawler/common/string.rb
http_crawler-0.3.1.29 lib/http_crawler/common/string.rb
http_crawler-0.3.1.28 lib/http_crawler/common/string.rb
http_crawler-0.3.1.27 lib/http_crawler/common/string.rb
http_crawler-0.3.1.26 lib/http_crawler/common/string.rb
http_crawler-0.3.1.25 lib/http_crawler/common/string.rb
http_crawler-0.3.1.24 lib/http_crawler/common/string.rb
http_crawler-0.3.1.23 lib/http_crawler/common/string.rb