类 Time

公共类方法

w3cdtf(date) 点击切换源代码

此方法将 W3CDTF 字符串日期/时间格式转换为 Time 对象。

W3CDTF 格式定义在此处:www.w3.org/TR/NOTE-datetime

Time.w3cdtf('2003-02-15T13:50:05-05:00')
# => 2003-02-15 10:50:05 -0800
Time.w3cdtf('2003-02-15T13:50:05-05:00').class
# => Time
# File rss-0.3.1/lib/rss/rss.rb, line 18
def w3cdtf(date)
  if /\A\s*
      (-?\d+)-(\d\d)-(\d\d)
      (?:T
      (\d\d):(\d\d)(?::(\d\d))?
      (\.\d+)?
      (Z|[+-]\d\d:\d\d)?)?
      \s*\z/ix =~ date and (($5 and $8) or (!$5 and !$8))
    datetime = [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i]
    usec = 0
    usec = $7.to_f * 1000000 if $7
    zone = $8
    if zone
      off = zone_offset(zone, datetime[0])
      datetime = apply_offset(*(datetime + [off]))
      datetime << usec
      time = Time.utc(*datetime)
      force_zone!(time, zone, off)
      time
    else
      datetime << usec
      Time.local(*datetime)
    end
  else
    raise ArgumentError.new("invalid date: #{date.inspect}")
  end
end

公共实例方法

w3cdtf() 点击切换源代码

此方法将 Time 对象转换为字符串。字符串包含 W3CDTF 日期/时间格式的时间。

W3CDTF 格式定义在此处:www.w3.org/TR/NOTE-datetime

Time.now.w3cdtf
# => "2013-08-26T14:12:10.817124-07:00"
# File rss-0.3.1/lib/rss/rss.rb, line 56
def w3cdtf
  if usec.zero?
    fraction_digits = 0
  else
    fraction_digits = strftime('%6N').index(/0*\z/)
  end
  xmlschema(fraction_digits)
end