call localtime as little time as possible. Thanks to ammdispose for the patch.