Module: File::Visitor::TimeUtils
- Included in:
- Filter::Mtime
- Defined in:
- lib/file/visitor/time_utils.rb
Constant Summary collapse
- UNIT_SEC =
{ :sec => 1, :min => 60, :hour => 60 * 60, :day => 60 * 60 * 24, :month => 60 * 60 * 24 * 31, :year => 60 * 60 * 24 * 365 }
- COMPARATOR =
[ :equals_to, :==, :is_greater_than, :is_younger_than, :>, :is_greater_than=, :is_younger_than=, :>=, :is_less_than, :is_older_than, :<, :is_less_than=, :is_older_than=, :<=, ]
Instance Method Summary collapse
- #compare_time(time1, comparator, time2) ⇒ Object
- #to_time(time) ⇒ Object
- #unitexp2sec(count, unit_name) ⇒ Object
Instance Method Details
#compare_time(time1, comparator, time2) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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) ⇒ Object
87 88 89 90 91 |
# 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) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# 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 |