Replace localtime() with localtime_r() for thread-safety