Rather than manually modifying the script, create logic to figure it out:
FirstBadTime
- The first trackpoint timestamp where value is more than a day in advance of the activity id time.
LastGoodTime
- Probably the last trackpoint timestamp that occurred within 24 hours of the activity id time.