For a ticket reporting tool, I want to compute whether certain service level times (slt) are met. That is (a bit simplified): Was the timespan between opening and closing of the ticket within a certain slt. Now there is also something called a service window (SW), like mo-fr,9:00-17:00. Now to compute whether a slt was met, times not within the SW have to be excluded. Even though not forseen, tickets can be opened or closed outside the SW, since there are other sla wich allow for 24x7 service. An engineer could work on a ticket outside the SW, since it might be easier for him to close a ticket while he is at it. Maybe he was doing work on a related ticket.

In order to determine the time a ticket took between opening and closing time, I though of two algorithms:

1) Distinguish all possible combinations of start_date and end_date beeing within or outside the SW, on different days and so on (I see 8 combinations). Then for each combination, set up a seperate formula to compute the time the ticket took.

Plus: little computational overhead

Minus: Rather complex, lots of code

2) Decide for a smallest possible unit (minutes seem to be apropriate), then for each unit between start_date and end_date, decide whether this unit is inside or outside the SW. If inside, add to the time the ticket took.

Plus: very simple

Minus: big computational overhead.

I am leaning towards 2) since compuation is cheap, simple code is to be preferred, and the rest of the programm contains lots of time consuming db queries, so a little computation will not be noticable.

However, both solutions seem not very elegant to me. Does anyone see a third way?

  • 1
    Do you actually need to know the time that the ticket took? Or just whether it is greater than a certain service level time? If the latter, could you approach it from the angle of calculating the "deadline time" from the start_date, and then comparing the end_date to that? Nov 18, 2013 at 7:21
  • I only need to know if a certain slt is met. So your approach is definatly an improvement over my ideas. But for deadline calculation, I would still resort to Doc Browns method, so I am choosing his algorithm.
    – Isaac
    Nov 18, 2013 at 7:39

1 Answer 1


Here is how I would probably solve the problem:

  • if the start date is outside the service window, change it to the next following date/time combination which is inside. Note that this change won't affect the final result.
  • same for the end date
  • Let T1 be the time span between start and end date (in an arbitrary unit)
  • Let T2 be the sum all non-service-time intervals between start and end date. Note that this calculation is now very easy due to the change in the first two steps
  • T1-T2 ist the result you are looking for
  • While this is definatly more elegant then what I thought of, it put the complexity in the T2 calculation and the shift of start and end date. Think holidays, weekends ...
    – Isaac
    Nov 18, 2013 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.