Sha256: f6125316838f0b3bfb4f806776f007c86d526038a179005c8b95f7d21d9da409

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require 'rubygems'
require "active_support/all"

class TimeDifference

  def self.between(start_time, end_time)
  	@time_diff = end_time - start_time
  	self
  end

  class << self

  	[:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
  		self.class.instance_eval do
  			define_method("in_#{time_component}") do
  				if time_component == :months
  					((@time_diff/(1.days * 30.42)).round(2)).abs
  				else
  			 		((@time_diff/1.send(time_component)).round(2)).abs
  			 	end
  			end
  		end
  	end

  end

  def self.in_each_component
    time_in_each_component = {}
    [:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
      if time_component == :months
        time_in_each_component[time_component] = ((@time_diff/(1.days * 30.42)).round(2)).abs
      else
        time_in_each_component[time_component] = ((@time_diff/1.send(time_component)).round(2)).abs
      end
    end
    time_in_each_component
  end

  def self.in_general
  	result = {}
	  [:years, :months, :weeks, :days, :hours, :minutes, :seconds].each do |time_component|
  		result[time_component] = (@time_diff/1.send(time_component)).floor
  		@time_diff = (@time_diff - result[time_component].send(time_component))
  	end
  	result
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
time_difference-0.3.2 lib/time_difference.rb
time_difference-0.3.1 lib/time_difference.rb
time_difference-0.3.0 lib/time_difference.rb