Module: L43::Time::ToTime

Extended by:
ToTime
Includes:
L43::Time::Tools::MapOk
Included in:
L43::Time, ToTime
Defined in:
lib/l43/time/to_time.rb

Constant Summary collapse

T =
::Time

Instance Method Summary collapse

Methods included from L43::Time::Tools::MapOk

#map_ok

Instance Method Details

#help_str_to_time(indent: 3, name_col: :blue, val_col: [:bold, :magenta]) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/l43/time/to_time.rb', line 34

def help_str_to_time(indent: 3, name_col: :blue, val_col: [:bold, :magenta])
  [
    [:bold, "the following formats of the parameter to ", name_col, "str_to_time", :reset, :bold,
     " are translated as follows to ", name_col, "Time ", :reset, :bold, "objects", :reset, nil],
     [1, :bold, "Absolute dates", :reset],
     [2, "14 digits", :dim, " to YYYYMMDDhhmmss"],
     [2, "12 digits", :dim, " to YYMMDDhhmmss"],
     [2, "6 digits", :dim, " to YYMMDD000000"],
     [2, "4 digits", :dim, " to MMDD000000"],
     [2, "2 digits", :dim, " to DD000000"],
     [2, val_col, "'today'", :reset, :dim, " to midnight of current date"],
     [2, val_col, "'yesterday'|'yd'", :reset, :dim, " to midnight of yesterday"],
     "",
     [1, :bold, "Relative times"],
     [2, val_col, "'-3d2h40s'", :reset, :dim, " 3 days 2 hours and 40 seconds before now"],
     [2, val_col, "'10m4s'", :reset, :dim, " 10 minuyes 4 seconds after now"],
  ].map { [indent, *it, :reset, nil] }
end

#str_to_time(str) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/l43/time/to_time.rb', line 17

def str_to_time(str)
  case str
  when /\A\d{14}\z/
    T.strptime(str, "%Y%m%d%H%M%S")
  when /\A\d{12}\z/
    T.strptime(str, "%y%m%d%H%M%S")
  when /\A\d{6}\z/
    T.strptime(str, "%y%m%d")
  when /\A\d{4}\z/
    T.strptime(str, "%m%d")
  when /\A\d{2}\z/
    T.strptime(str, "%d")
  else
    parse_symbolic_or_duration(str)
  end
end