Sha256: 48fc387fec64a61c02cbcad75523cce096907ced8bc2dc33cbd44e6cc6a9c001

Contents?: true

Size: 949 Bytes

Versions: 8

Compression:

Stored size: 949 Bytes

Contents

module XML
  HourMinuteMatch = /[MH]/o 
  # returns a float object of seconds
  # doesn't support year month, etc, yet
  def self.duration_to_seconds(string)
    case x = string[0,2]
    when 'PT'
      rest = string[2..-1]
      # usually it will be this 'PT1.223434S':
      if rest !~ HourMinuteMatch
        rest[0...-1].to_f
      else
        addit = ''
        total_secs = 0
        total_secs_as_float = nil
        rest.split('').each do |let|
          case let
          when 'H'
            total_secs += addit.to_i * 3600
            addit = ''
          when 'M'
            total_secs += addit.to_i * 60
            addit = ''
          when 'S'
            total_secs_as_float = total_secs.to_f
            total_secs_as_float += addit.to_f
          else
            addit << let
          end
        end
        total_secs_as_float
      end
    else 
      abort 'need to include support for other durations'
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mspire-0.4.9 lib/xml.rb
mspire-0.3.0 lib/xml.rb
mspire-0.3.9 lib/xml.rb
mspire-0.3.1 lib/xml.rb
mspire-0.4.2 lib/xml.rb
mspire-0.4.4 lib/xml.rb
mspire-0.4.7 lib/xml.rb
mspire-0.4.5 lib/xml.rb