Method: ICFS.time_parse

Defined in:
lib/icfs.rb

.time_parse(str, cfg) ⇒ Object

Parse a time string

Handles:

  • blank or now

  • [now] +\- <num> <type>

  • next\prev <type>

  • in <num> <type>

  • <num> <type> ago

  • a specifc parseable time

Parameters:

  • str (String)

    the time string

  • cfg (Config)

    the config



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/icfs.rb', line 190

def self.time_parse(str, cfg)
  return nil if( !str || !str.is_a?(String) )

  # empty
  if ma = TimeEmpty.match(str)
    return Time.now.to_i

  # delta
  elsif ma = TimeDelta.match(str)
    num = ma[3].to_i
    num = num * -1 if ma[2] == '-'
    type = ICFS._time_type(ma[4])
    return ICFS._time_adjust(num, type)

  # relative
  elsif ma = TimeRel.match(str)
    num = (ma[1].downcase == 'next') ? 1 : -1
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # future
  elsif ma = TimeFuture.match(str)
    num = ma[1].to_i
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # history
  elsif ma = TimeHistory.match(str)
    p ma
    num = -1 * ma[1].to_i
    type = ICFS._time_type(ma[2])
    return ICFS._time_adjust(num, type)

  # parse a time spec
  else
    ma = TimeZone.match(str)
    tstr = ma ? str : str + cfg.get('tz')
    return Time.parse(tstr).to_i

  end

rescue ArgumentError
  return nil
end