The problem arise when you update localtime()
return value, you need to use localtime_r()
:
struct tm *localtime(const time_t *timep);
The return value of localtime() points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.
struct tm *localtime_r(const time_t *timep, struct tm *result);
The localtime_r()
stores the data in a user-supplied struct.
In your example, it should be something similar to:
dayb4 = localtime_r(&t1, dayb4);