Browse Source

Improved TimeCalculation, added unit tests.

Martin Edenhofer 11 years ago
parent
commit
dfcfc7ab28
2 changed files with 47 additions and 2 deletions
  1. 9 2
      lib/time_calculation.rb
  2. 38 0
      test/unit/working_time_test.rb

+ 9 - 2
lib/time_calculation.rb

@@ -236,6 +236,8 @@ put working hours matrix and timezone in function, returns UTC working hours mat
       start_time = Time.parse( start_time.to_s + ' UTC' )
     end
 
+    return start_time if diff_in_min == 0
+
     # if no config is given, just return calculation directly
     if !config
       return start_time + (diff_in_min * 60)
@@ -288,8 +290,12 @@ put working hours matrix and timezone in function, returns UTC working hours mat
       # fillup to first full hour
       if first_loop
 
-        # get rest of this hour
-        diff = 3600 - (start_time - start_time.beginning_of_hour)
+        # get rest of this hour if diff_in_min in lower the one hour
+        diff_to_count = 3600
+        if diff_to_count > (diff_in_min * 60)
+          diff_to_count = diff_in_min * 60
+        end
+        diff = diff_to_count - (start_time - start_time.beginning_of_hour)
         start_time += diff
 
         # check if it's countable hour
@@ -318,6 +324,7 @@ put working hours matrix and timezone in function, returns UTC working hours mat
 
         # check if it's business hour and count
         if working_hours[ week_day_map[week_day] ][ next_hour ]
+
           # check if count is within this hour
           if count > 59 * 60
             diff = 3600

+ 38 - 0
test/unit/working_time_test.rb

@@ -509,6 +509,44 @@ class WorkingTimeTest < ActiveSupport::TestCase
         :diff      => 119,
       },
 
+      # test 18
+      {
+        :start     => '2013-10-21 04:01:00',
+        :dest_time => '2013-10-21 04:01:00',
+        :diff      => 0,
+      },
+
+      # test 19
+      {
+        :start     => '2013-04-12 21:20:15',
+        :dest_time => '2013-04-12 21:20:15',
+        :diff      => 0,
+        :config    => {
+          'Mon'                  => true,
+          'Tue'                  => true,
+          'Wed'                  => true,
+          'Thu'                  => true,
+          'Fri'                  => true,
+          'beginning_of_workday' => '8:00 am',
+          'end_of_workday'       => '6:00 pm',
+        },
+      },
+
+      # test 20
+      {
+        :start     => '2013-04-12 11:20:15',
+        :dest_time => '2013-04-12 11:21:15',
+        :diff      => 1,
+        :config    => {
+          'Mon'                  => true,
+          'Tue'                  => true,
+          'Wed'                  => true,
+          'Thu'                  => true,
+          'Fri'                  => true,
+          'beginning_of_workday' => '8:00 am',
+          'end_of_workday'       => '6:00 pm',
+        },
+      },
     ]
     tests.each { |test|
       dest_time = TimeCalculation.dest_time( test[:start] + ' UTC', test[:diff], test[:config], test[:timezone] )