Module: L43::Time::Parser

Includes:
Ts
Included in:
Delta, Duration
Defined in:
lib/l43/time/parser.rb

Defined Under Namespace

Modules: Helper

Constant Summary collapse

Prefix =
%r{
  (?<unit>[[:lower:]]+) [[:space:]]*:[[:space:]]* (?<value>[[:digit:]]+ (?:\.[[:digit:]]+)? )
}x
Suffix =
%r{
  (?<value>[[:digit:]]+ (?:\.[[:digit:]]+)?) [[:space:]]* (?<unit>[[:lower:]]+)
}x

Constants included from Ts

Ts::DecimalPart, Ts::Seperators

Instance Method Summary collapse

Methods included from Ts

#duration_as_string, #duration_as_string!

Instance Method Details

#format_ts(ts, **opts) ⇒ Object



28
# File 'lib/l43/time/parser.rb', line 28

def format_ts(ts, **opts) = Tools::FormatTs.format_ts(ts, **opts)

#format_ts!(ts, **opts) ⇒ Object



30
# File 'lib/l43/time/parser.rb', line 30

def format_ts!(ts, **opts) = Tools::FormatTs.format_ts!(ts, **opts)

#parse_duration(str) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/l43/time/parser.rb', line 41

def parse_duration(str)
  if str[Prefix]
    Helper.parse_prefix(str)
  else
    Helper.parse_suffix(str)
  end
end

#parse_duration!(str) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/l43/time/parser.rb', line 32

def parse_duration!(str)
  case parse_duration(str)
  in :ok, value
    value
  in :error, msg
    raise BadFormat, msg
  end
end

#parse_or_duration(str) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/l43/time/parser.rb', line 58

def parse_or_duration(str)
  case duration_as_string(str)
  in :ok, value
    [:ok, value]
  else
    parse_duration(str)
  end
end

#parse_or_duration!(str) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/l43/time/parser.rb', line 49

def parse_or_duration!(str)
  case parse_or_duration(str)
  in :ok, value
    value
  in :error, msg
    raise BadFormat, msg
  end
end