isTimeRangeValid function