In Files

  • rss/rss.rb

Parent

Methods

Class/Module Index [+]

Quicksearch

Time

Public Class Methods

w3cdtf(date) click to toggle source

This method converts a W3CDTF string date/time format to Time object.

The W3CDTF format is defined here: 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/rss.rb, line 15
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
            

Public Instance Methods

w3cdtf() click to toggle source

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"
 
               # File rss/rss.rb, line 53
def w3cdtf
  if usec.zero?
    fraction_digits = 0
  else
    fraction_digits = strftime('%6N').index(/0*\z/)
  end
  xmlschema(fraction_digits)
end
            

Additional notes

There are two Time classes. There's one that is part of core Ruby and there is an additional Time class that is part of the standard library.

The standard library Time class extends the core Time class by adding some methods. If you are using the standard library Time class and cannot find documentation for a method, look at the API docs for the core Time class.

Common questions

Q: Given some number of seconds, how can that be converted into minutes and remainder seconds?

A: There are a few ways. Assume we start with 1234 seconds.

    Time.at(1234).strftime "%M:%S"   # This returns a String 

If you want numerical values, you can do some basic math:

    m, s = 1234/60, 1234%60

That second example may be a little too terse for general use. You can wrap it in a method:

    
  def minutes_and_remainder_seconds seconds
    [seconds/60, seconds%60]
  end

  minutes, seconds = *minutes_and_remainder_seconds(1234)
 

You may want to make this available to multiple classes. You can put it in a module ...

  module TimeUtilities

    def minutes_and_remainder_seconds seconds
      [seconds/60, seconds%60]
    end
    
  end
 

... and then include that module in those classes that use it:


   class Foo
     include TimeUtilities

     #  additional class stuff ....
   end
 

Thanks to Andrey Andreevich Ostapchuck and J Bolton for their suggestions on this.