Module: L43::Time::FormatTime

Includes:
Tools::MapOk
Included in:
L43::Time
Defined in:
lib/l43/time/format_time.rb

Defined Under Namespace

Modules: Formatter

Constant Summary collapse

LegalFormats =
{
  ds: :_decimal_seconds,
  dms: :_decimal_milliseconds,
  dmcs: :_decimal_microseconds,
  dμs: :_decimal_microseconds,

  xs: :_hex_seconds,
  xms: :_hex_milliseconds,
  xmcs: :_hex_microseconds,
  xμs: :_hex_microseconds,

  zs: :_base36_seconds,
  zms: :_base36_milliseconds,
  zmcs: :_base36_microseconds,
  zμs: :_base36_microseconds,

  date: :_date,
  long: :_long,
  iso: :_iso,
  time: :_time,
}

Instance Method Summary collapse

Methods included from Tools::MapOk

#map_ok

Instance Method Details

#format_time(fmt, time: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/l43/time/format_time.rb', line 62

def format_time(fmt, time: nil)
  time ||= ::Time.now
  case map_ok(exception: KeyError) {
    Formatter.send LegalFormats.fetch(fmt.to_sym), time
  }
  in :error, _
    [:error, ":#{fmt} is not in one of the supported formats: #{LegalFormats.keys.inspect}"]
  in result
    result
  end
end

#format_time!(fmt, time: nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/l43/time/format_time.rb', line 53

def format_time!(fmt, time: nil)
  case format_time(fmt, time:)
  in :ok, result
    result
  in :error, msg
    raise BadFormat, msg
  end
end