Variety of minor cleanups

Refactor parsing a query from request args to make it a bit easier to scan.

Move time utilities into a timeutils module and get rid of the duplication.