Computing duration using gateway tag value change script

This topic might give you some ideas: