Sha256: 1762f2c1bb63a0c2507d566b55599e29e80496b9c1e9f64b677114c30c15b7bf

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 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日",

        "%Y/%m/%d",
        "%Y/%m",

    ].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

3 entries across 3 versions & 1 rubygems

Version Path
http_crawler-0.3.2.1 lib/http_crawler/common/string.rb
http_crawler-0.3.2.0 lib/http_crawler/common/string.rb
http_crawler-0.3.1.31 lib/http_crawler/common/string.rb