Class: XlsFunction::Evaluators::Functions::TimeFn

Inherits:
XlsFunction::Evaluators::FunctionEvaluator show all
Defined in:
lib/xls_function/evaluators/functions/time.rb

Constant Summary collapse

ARG_LIMIT =
32_767

Instance Method Summary collapse

Methods inherited from XlsFunction::Evaluators::FunctionEvaluator

#arg_list, #before_eval, #convert_to, create, #detect_error, #error?, #error_message, #eval_arglist, #eval_or_map_eval, #evaluate, #evaluate_or_self, #initialize, #map_eval, to_h, #to_proc, to_proc, translated_description, #variant_context

Methods included from ClassDictionary

included

Methods included from ErrorDetector

#class_info, #class_name, included, #rescue_with

Methods included from ArgumentsDefinable

included

Methods included from Evaluable

#evaluate, included

Constructor Details

This class inherits a constructor from XlsFunction::Evaluators::FunctionEvaluator

Instance Method Details

#evalObject



15
16
17
18
19
20
21
22
# File 'lib/xls_function/evaluators/functions/time.rb', line 15

def eval
  return ::XlsFunction::ErrorValue.num!(out_of_range) unless validate_args

  adjusted = ::XlsFunction::Converters::TimeConverter.adjust_elapsed_time(hour_i, minute_i, second_i)
  time = Time.new(Time.now.year, nil, nil, adjusted[:hour], adjusted[:minute], adjusted[:second])

  time.to_serial(except_date: true)
end

#hour_iObject



35
36
37
# File 'lib/xls_function/evaluators/functions/time.rb', line 35

def hour_i
  @hour_i ||= hour.to_i
end

#minute_iObject



39
40
41
# File 'lib/xls_function/evaluators/functions/time.rb', line 39

def minute_i
  @minute_i ||= minute.to_i
end

#out_of_rangeObject



47
48
49
50
# File 'lib/xls_function/evaluators/functions/time.rb', line 47

def out_of_range
  message = error_message(:out_of_range, value: "#{hour}, #{minute}, #{second}")
  class_info(message)
end

#second_iObject



43
44
45
# File 'lib/xls_function/evaluators/functions/time.rb', line 43

def second_i
  @second_i ||= second.to_i
end

#validate_argsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/xls_function/evaluators/functions/time.rb', line 24

def validate_args
  return false if hour_i > ARG_LIMIT || minute_i > ARG_LIMIT || second_i > ARG_LIMIT

  # raise error when negative converted to seconds
  hour_sec = hour_i * 60 * 60
  minute_sec = minute_i * 60
  return false if (hour_sec + minute_sec + second_i).negative?

  true
end