Module: ApexCharts::Utils::DateTime

Included in:
OptionsBuilder
Defined in:
lib/apexcharts/utils/date_time.rb

Class Method Summary collapse

Class Method Details

.convert(input) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/apexcharts/utils/date_time.rb', line 8

def convert(input)
  case input
  when Time
    (input.to_f * 1000).round
  when ::DateTime, Date
    (input.to_time.to_f * 1000).round
  else
    if (dt = ::DateTime.iso8601(input)).iso8601 == input
      (dt.to_time.to_f * 1000).round
    elsif (d = Date.iso8601(input)).iso8601 == input
      (d.to_time.to_f * 1000).round
    end
  end
rescue StandardError
  input
end

.convert_range(input) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apexcharts/utils/date_time.rb', line 25

def convert_range(input)
  input_first, input_last = input.first, input.last
  case input_first
  when Time
    to_milliseconds_range(input_first, input_last)
  when ::DateTime, Date
    to_milliseconds_range(input_first.to_time, input_last.to_time)
  else
    if (datetime = ::DateTime.iso8601(input_first)).iso8601 == input_first
      to_milliseconds_range(datetime.to_time, ::DateTime.iso8601(input_last).to_time)
    elsif (date = Date.iso8601(input.first)).iso8601 == input_first
      to_milliseconds_range(date.to_time, ::Date.iso8601(input_last).to_time)
    end
  end
rescue StandardError
  input
end

.to_milliseconds(input) ⇒ Object



43
44
45
# File 'lib/apexcharts/utils/date_time.rb', line 43

def to_milliseconds(input)
  (input.to_f * 1000).round
end

.to_milliseconds_range(first, last) ⇒ Object



47
48
49
# File 'lib/apexcharts/utils/date_time.rb', line 47

def to_milliseconds_range(first, last)
  to_milliseconds(first)..to_milliseconds(last)
end

.type(input) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/apexcharts/utils/date_time.rb', line 51

def type(input)
  case input
  when Time, ::DateTime, Date
    'datetime'
  else
    if [
      ::DateTime.iso8601(input).iso8601,
      Date.iso8601(input).iso8601
    ].include? input
      'datetime'
    end
  end
rescue StandardError
  nil
end