module File::Visitor::TimeUtils

Constants

COMPARATOR
UNIT_SEC

Public Instance Methods

compare_time(time1, comparator, time2) click to toggle source
# File lib/file/visitor/time_utils.rb, line 61
def compare_time(time1, comparator, time2)
  time1 = to_time(time1)
  time2 = to_time(time2)

  unless COMPARATOR.include?(comparator)
    raise ArgumentError,
      "invalid comparator: #{comparator.to_s}"
  end

  case comparator
  when :equals_to, :==
    return time1 == time2
  when :is_greater_than, :is_younger_than, :>
    return time1 > time2
  when :is_greater_than=, :is_younger_than=, :>=
    return time1 >= time2
  when :is_less_than, :is_older_than, :<
    return time1 < time2
  when :is_less_than=, :is_older_than=, :<=
    return time1 <= time2
  else
    raise RuntimeError,
      "invalid comparator: #{comparator.to_s}"
  end
end
to_time(time) click to toggle source
# File lib/file/visitor/time_utils.rb, line 87
def to_time(time)
  return time if time.is_a?(Time)
  return Time.parse(time) if time.is_a?(String)
  raise ArgumentError, "invalid time: #{time.inspect}"
end
unitexp2sec(count, unit_name) click to toggle source
# File lib/file/visitor/time_utils.rb, line 50
def unitexp2sec(count, unit_name)
  if (!count.is_a?(Fixnum) || count < 0)
    raise ArgumentError,
      "time count must be positive fixnum: #{count}"
  end
  if !UNIT_SEC[unit_name]
    raise ArgumentError, "unknown time unit: #{unit_name}"
  end
  count * UNIT_SEC[unit_name]
end