Because the time and localtime functions are a bit fussy about their arguments, you may find it wise to understand what they do once, and then to hide them in a function.
time
localtime