Module: Dsu::Support::CommandOptions::DsuTimes

Defined in:
lib/dsu/support/command_options/dsu_times.rb

Class Method Summary collapse

Class Method Details

.dsu_from_time_for(from_option:) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/dsu/support/command_options/dsu_times.rb', line 28

def dsu_from_time_for(from_option:)
  return if from_option.nil?

  from_time = if TimeMnemonic.time_mnemonic?(from_option)
    TimeMnemonic.time_from_mnemonic(command_option: from_option)
  end
  from_time || Time.time_from_date_string(command_option: from_option)
end

.dsu_times_for(from_option:, to_option:) ⇒ Object

Returns an array of Time objects. The first element is the “from” time. The second element is the “to” time. Both arguments are expected to be command options that are time strings, time or relative time mnemonics.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dsu/support/command_options/dsu_times.rb', line 15

def dsu_times_for(from_option:, to_option:)
  from_time = dsu_from_time_for(from_option: from_option)
  to_time = dsu_to_time_for(to_option: to_option, from_time: from_time)

  errors = []
  errors << I18n.t('errors.from_option_invalid', from_option: from_option) if from_time.nil?
  errors << I18n.t('errors.to_option_invalid', to_option: to_option) if to_time.nil?
  return [[], errors] if errors.any?

  min_time, max_time = [from_time, to_time].minmax
  [(min_time.to_date..max_time.to_date).map(&:to_time), []]
end

.dsu_to_time_for(to_option:, from_time:) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/dsu/support/command_options/dsu_times.rb', line 37

def dsu_to_time_for(to_option:, from_time:)
  to_time = if TimeMnemonic.relative_time_mnemonic?(to_option)
    TimeMnemonic.time_from_mnemonic(command_option: to_option, relative_time: from_time)
  elsif TimeMnemonic.time_mnemonic?(to_option)
    TimeMnemonic.time_from_mnemonic(command_option: to_option)
  end
  to_time || Time.time_from_date_string(command_option: to_option)
end