mktime - C program time_t and struct tm, same result? -


i new c program , got problem when code program using mktime function.

i declare 2 time, first 1 system time, , second 1 one day before it, here code:

struct tm *now = calloc(1,sizeof(struct tm)); struct tm *dayb4 = calloc(1,sizeof(struct tm)); time_t t1 = time(null);  = localtime(&t1); dayb4 = localtime(&t1);  dayb4->tm_day -= 1; mktime(dayb4); 

however, found the time "now" , "dayb4" same, 1 day before current time... told me part getting wrong?

thank lot !!!

the problem arise when update localtime() return value, need use localtime_r():

struct tm *localtime(const time_t *timep); 

the return value of localtime() points statically allocated struct might overwritten subsequent calls of date , time functions.

struct tm *localtime_r(const time_t *timep, struct tm *result); 

the localtime_r() stores data in user-supplied struct.

in example, should similar to:

dayb4 = localtime_r(&t1, dayb4); 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -