Time Calculations And Time Between certain times

Ok I’ve wasted all day, and I’m hoping it’s because I’m a noob, and
that it can be done…

Say I’ve got two random times, they’re stored in the database as time
objects so if in the database I have 4:15:00 to rails they come out as
January 1st 2000 4:15:00 for example…

No I have two of these, and I need to check how much of them is between
11PM on January 1st and 6AM on January second…

I tried to create time objects to hold 11PM January 1st and so on, but
I was unable to figure it out.

What I’m trying to do is figure out how many hours someone worked
between those times, to I can pay them extra. Any advice would be very
much appreciated.


Try something like this:

start_hour = 17 # start work at 5pm (24 hour time)
end_hour = 3 # finish work at 3am

default to no overtime

overtime_hours = 0

worked the whole overtime shift?

overtime_hours = 7 if start_hour<=23 && end_hour>=6

start at/before 11 and finished after midnight but before whole shift

overtime_hours = 1+end_hour if start_hour<=23 && end_hour<6

start at/before 11 and finished before midnight

overtime_hours = end_hour-23 if start_hour<=23 && end_hour>=23

start between 11 and 12 and finished after 6

overtime_hours = 6+ if start_hour>=23 && end_hour>=6

on second thoughts… thats pretty ‘kludgey’. But you get the idea.

Scratch my last reply, this is much better:

@start_hour = 22 # start at 10pm 24hour time
@end_hour = 3.5 # end at 3:30am - notice the .5 means 30 minutes

@start_overtime=23 #overtime starts at 11pm
@end_overtime=6 #overtime finishes at 6am

def maximum(a,b)
a>b ? a : b

def find_overtime

firstly, find when they started and finished, relative to the start

of overtime
s = @start_hour - @start_overtime #start work
e = @end_hour - @start_overtime #end work
e += 24 if @end_hour<@start_hour # if they worked past midnight

this is the important line

overtime = maximum(e,0) - maximum(s,0)

print find_overtime

This looks good Chirs,

Thanks… right after the hockey game I’ll give it a shot!

Thanks again!

I always convert to epoch and do the math that way… dunno… not
saying it’s what you’re needing but would work.

Yeah that gives me the difference, but I need to know how much time was
spent between two specific time periods. Subtracting gives me total
time worked, but I need to know how much of that time, was between 11PM
and 6AM.


Cant you just get the difference from the times like this?
@start, @end are DateTime

@diff = @end - @start # this will return the diff in seconds have to
divide by 3600 to get it in hours.

Hope it works for u.


Lets see if this works…

@start = (@start.hour > 23) ? @start : @start.change(:hours=>23)
@end = (@end.hour < 6) ? @end : @end.change(:hour=>6)
@overtime = @end - @start #again in ms

This works if the days are consecutive…

ok… for the sake of archival needs and in case anyone else tries to
do this… here’s what my final solution was…

this is in the model for the class that holds the “day of work” so to

#if the start time is greater than the end time, move end time to Jan

if self.finish_time < self.call_time
#add 86, 400 seconds
self.finish_time += ( 60 * 60 * 24 )

calculate the total time in seconds, convert to hours… and round to

the next full interger( .ceil ) ( 2.01 becomes 3 because they are paid
for every hour or part thereof )

hours_worked = ( ( self.finish_time - self.call_time ) / 3600 ).ceil

#-create constants to hold the point in time where 6AM and 11PM are on
Jan1st and 2nd
jan_one_eleven_pm_epoch = 946785600
jan_one_six_am_epoch = 946724400
jan_two_six_am_epoch = 946810800

##- night premiums
#- check to see if clal time or end time falls between OT period ( need
to do more to check if the employee works 10Pm to 7AM for example…
but that’s easy )
if( self.call_time.to_i >= jan_one_eleven_pm_epoch ||
self.call_time.to_i <= jan_one_six_am_epoch ||
( self.finish_time.to_i <= jan_two_six_am_epoch &&
self.finish_time.to_i >= jan_one_eleven_pm_epoch ) )

#- use the call_time or use 11PM, use finish or use 6AM etc… convert
to Float and convert to hours
night_hours = ( minimum( self.finish_time.to_i, jan_two_six_am_epoch )

                   maximum( self.call_time.to_i,

jan_one_eleven_pm_epoch ) ).to_f / 3600

##private methods from Chirs…

def maximum(a,b)
a>b ? a : b

def minimum(a,b)
a<b ? a : b

Works like a charm!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs