Module: DR::TimeParse

Extended by:
TimeParse
Included in:
TimeParse
Defined in:
lib/dr/parse/time_parse.rb

Instance Method Summary collapse

Instance Method Details

#parse(s, **opt) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dr/parse/time_parse.rb', line 11

def parse(s, **opt)
  return s if Date===s or Time===s

  !opt[:norange] and s.match(/(.*)\.\.(.*)/) do |m|
    first=m[1]
    second=m[2]
    opt[:norange]=true
    return Chronic::Span.new(self.parse(first, **opt),self.parse(second, **opt))
  end

  if s.match(/\A[[:space:]]*\z/) # blank
    t=Time.now
  elsif s[0] =~ /[+-]/
    #if s=+3.years-1.minutes
    begin
      t=eval(s)
    rescue SyntaxError
    #if s=3 years
      t=ChronicDuration.parse(s[1...s.length])
      t=-t if s[0]=='-'
    end
    case t
    when Time
    else
      t=Time.now+t
    end
    return t
  else

    chronicopts=[:context,:now,:guess,:ambiguous_time_range,:endian_precedence,:ambiguous_year_future_bias]
    chronicopt={hours24: true, ambiguous_time_range: 0, endian_precedence: [:little,:middle]}
    chronicopt[:guess]=false if opt[:range]
    chronicopt.update(opt.reject {|k,v| not chronicopts.include?(k)})
    #puts chronicopt
    t=Chronic.parse(s, chronicopt)
    t=Time.parse(s) if not t
  end
  if opt[:range] && !opt[:norange] then
    return time_to_day_range(t) unless Range === t
  end
  return t
end

#time_to_day_range(t) ⇒ Object



8
9
10
# File 'lib/dr/parse/time_parse.rb', line 8

def time_to_day_range(t)
  return Chronic.parse(t.to_date, guess:false)
end