Calculate a duration extracted from a period

Hello,

i want to get the duration of an kind of activity between 2 dates and i would like to know if there's an easy solution for that. Maybe a function.

I have an activity called HS and i would like to know the duration of this activity (if it exists) between 2 dates. If there're one or more activties in the period, it's easy, i can do it with a sum but if i have an activity that starts before or ends after the period, it's harder.

Example: I want to know the duration of the activity (or activities) HS between the 13 sept 00h00 and the 15sept 23h59. I have one HS activity that starts on the 11 sept 00:00 and ends on the 14 23h59.  
It's a kind of "Calculate the duration of the HS activity during the period"

 

Thanks by advanced