put repeated code for printing time into a function