Skip to content
Browse files

Added module to calculate the time difference

  • Loading branch information...
1 parent f6d94c3 commit 099231b6f963d20581bf7c8391a8d1a1f95c1535 @abhidsm committed Mar 6, 2011
Showing with 62 additions and 2 deletions.
  1. +3 −0 Gemfile
  2. +24 −0 Gemfile.lock
  3. +22 −0 lib/time_diff.rb
  4. +13 −2 test/test_time_diff.rb
View
3 Gemfile
@@ -1,4 +1,7 @@
source "http://rubygems.org"
+
+gem 'activesupport'
+gem 'i18n'
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"
View
24 Gemfile.lock
@@ -0,0 +1,24 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.1)
+ git (1.2.5)
+ i18n (0.4.2)
+ jeweler (1.5.2)
+ bundler (~> 1.0.0)
+ git (>= 1.2.5)
+ rake
+ rake (0.8.7)
+ rcov (0.9.9)
+ shoulda (2.11.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activesupport
+ bundler (~> 1.0.0)
+ i18n
+ jeweler (~> 1.5.2)
+ rcov
+ shoulda
View
22 lib/time_diff.rb
@@ -0,0 +1,22 @@
+require 'rubygems'
+require 'active_support/all'
+module TimeDiff
+ def diff(start_date, end_date)
+ from_time = start_date.to_time if start_date.respond_to?(:to_time)
+ to_time = end_date.to_time if end_date.respond_to?(:to_time)
+ distance_in_seconds = ((to_time - from_time).abs).round
+ components = []
+
+ %w(year month week day hour minute second).each do |interval|
+ delta = (distance_in_seconds / 1.send(interval)).floor
+ distance_in_seconds -= delta.send(interval)
+ components << pluralize(delta, interval)
+ end
+ return components
+ end
+
+ def pluralize(value, text)
+ result = value > 1 ? text.pluralize : text
+ return "#{value} #{result}"
+ end
+end
View
15 test/test_time_diff.rb
@@ -1,7 +1,18 @@
require 'helper'
+require 'time'
+include TimeDiff
class TestTimeDiff < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ should "return the time differnce in displayable format" do
+ assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-07'), ["0 year", "0 month", "0 week", "1 day", "0 hour", "0 minute", "0 second"])
+ assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-04-08'), ["0 year", "1 month", "0 week", "3 days", "0 hour", "0 minute", "0 second"])
+ assert_test_scenarios(Time.parse('2011-03-06 12:30:00'), Time.parse('2011-03-07 12:30:30'), ["0 year", "0 month", "0 week", "1 day", "0 hour", "0 minute", "30 seconds"])
+ assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2013-03-07'), ["2 years", "0 month", "0 week", "1 day", "12 hours", "0 minute", "0 second"])
+ assert_test_scenarios(Time.parse('2011-03-06'), Time.parse('2011-03-14'), ["0 year", "0 month", "1 week", "1 day", "0 hour", "0 minute", "0 second"])
+ end
+
+ def assert_test_scenarios(start_date, end_date, expected_result)
+ date_diff = diff(start_date, end_date)
+ assert_equal(date_diff, expected_result)
end
end

0 comments on commit 099231b

Please sign in to comment.
Something went wrong with that request. Please try again.