Your struct tm * ts_delay
gets overwritten by the 2. call to localtime()
You have this code:
ts_delay = localtime(&now);
... change ts_delay;
ts = localtime(&now);
Now, localtime()
returns a pointer. So some storage have to be allocated so the return value of localtime can point somwhere valid.
The runtime will allocate that storage in a single static buffer (perhaps per thread) for the result
of localtime(). So the 2. call to localtime() will overwrite the same buffer that ts_delay
points to.
Instead copy the result of localtime() into storage that you manage yourself, instead of using a struct tm
pointer into the single available buffer.
struct tm ts; //not a pointer anymore
struct tm ts_delay;
ts_delay = *localtime(&now); //copy the returned struct
... change ts_delay;
ts = *localtime(&now);
Or use getlocaltime_r
if your platform provides it, e.g.
localtime_r(&now, &ts);