You can implement a simple function to calculate the overlap between two time ranges similar to how it's done here: Efficient date range overlap calculation?
Then the number of hours worked in each shift is just the overlap between the employee's working time with that specific shift working hours.