Module: Grok

Defined in:
lib/grok/time.rb

Overview

This bit of code has been shamelessly stolen from rufus-scheduler because it’s awesome.

Class Method Summary collapse

Class Method Details

.parse_time_string(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grok/time.rb', line 5

def Grok.parse_time_string(string)
  string = string.strip
  index = -1
  result = 0.0

  number = ''
  loop do
    index = index+1

    if index >= string.length
      result += (Float(number) / 1000.0) if number.length > 0
      break
    end

    c = string[index, 1]

    if (c >= '0' and c <= '9')
      number += c
      next
    end

    value = Integer(number)
    number = ''
    multiplier = DURATIONS[c]

    raise "unknown time char '#{c}'" unless multiplier

    result += (value * multiplier)
  end

  result
end