Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/rss/rss.rb

Instance Method Summary collapse

Instance Method Details

#w3cdtfObject

This method converts a Time object to a String. The String contains the time in W3CDTF date/time format.

The W3CDTF format is defined here: www.w3.org/TR/NOTE-datetime

Time.now.w3cdtf
# => "2013-08-26T14:12:10.817124-07:00"


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File '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