Last active
August 29, 2015 14:03
-
-
Save mrdougwright/6965829d1a3e3fae63c8 to your computer and use it in GitHub Desktop.
login_driver?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'time' | |
SHIFTS = [ | |
{id: 1, shiftStart: "2014-07-08 09:00:42 -0700", shiftEnd: "2014-07-08 18:00:42 -0700"}, | |
{id: 1, shiftStart: "2014-07-08 06:57:42 -0700", shiftEnd: "2014-07-08 08:57:42 -0700"}, | |
{id: 1, shiftStart: "2014-07-08 01:57:42 -0700", shiftEnd: "2014-07-08 05:57:42 -0700"}, | |
{id: 1, shiftStart: "2014-07-07 06:57:42 -0700", shiftEnd: "2014-07-07 12:57:42 -0700"}, | |
{id: 1, shiftStart: "2014-07-06 05:57:42 -0700", shiftEnd: "2014-07-06 11:57:42 -0700"} | |
] | |
def login_driver? | |
active_shifts = active_shifts(SHIFTS) | |
if twelve_hours?(active_shifts) | |
puts "can't login driver...driving too long" | |
return false | |
else | |
puts "login driver...not more than 12 hours" | |
return true | |
end | |
end | |
def twelve_hours?(shifts) | |
total_time = 0 | |
shifts.each do |shift| | |
total_time += (Time.parse(shift[:shiftEnd]) - Time.parse(shift[:shiftStart])) | |
end | |
puts "current total time: #{total_time / 60 / 60} hours" | |
if total_time >= 43_200 # 12 hours | |
return true | |
else | |
return false | |
end | |
end | |
def active_shifts(shifts) | |
last_active_shift = nil | |
shifts.each_with_index do |shift, index| | |
last_shift_end = Time.parse(shifts[index + 1][:shiftEnd]) | |
if (Time.parse(shift[:shiftStart]) - last_shift_end) >= 28_800 # 8 hours | |
last_active_shift = index | |
break | |
end | |
end | |
shifts[0..last_active_shift] | |
end | |
login_driver? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment