Data wouldn't persist between two calls to localtime
or asctime
. You have to copy data somewhere. Here is corrected example (still have little issue with strncpy):
#include <stdio.h>
#include <time.h>
#include <string.h>
int main() {
time_t time1, time2;
struct tm timeinfo1, timeinfo2, *ti;
char time1str[256], time2str[256], *tstr;
time1 = 3600;
time2 = 3720;
ti = localtime(&time1);
memcpy(&timeinfo1, ti, sizeof(*ti));
ti = localtime(&time2);
memcpy(&timeinfo2, ti, sizeof(*ti));
tstr = asctime(&timeinfo1);
strncpy(time1str, tstr, sizeof(time1str) - 1);
tstr = asctime(&timeinfo2);
strncpy(time2str, tstr, sizeof(time1str) - 1);
puts(time1str);
puts(time2str);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…