[mktime()在C中覆盖time_t

问题描述 投票:0回答:1

我在c中的mktime有问题:

time_t t_fim;
time(&t_fim);

struct tm* p_fim;
struct tm* c_fim;
p_fim = localtime(&t_fim);
c_fim = localtime(&t_fim);

......

p_fim->tm_year = ano-1900;
p_fim->tm_mon = mes-1;
p_fim->tm_mday = dia;
//here the p_fim date is good

c_fim->tm_mday -=47;
mktime(c_fim);

printf("Pascoa %d %d %d\n", p_fim->tm_mday, p_fim->tm_mon + 1, p_fim->tm_year + 1900);
printf("Carnaval %d %d %d\n", c_fim->tm_mday, c_fim->tm_mon+1, c_fim->tm_year + 1900);

//here the c_fim is good with -47 days but the `p_fim` is now equal with `c_fim`

mktime正在更改p_fim的值,如何解决?

c mktime
1个回答
0
投票

尝试摆脱指针

struct tm p_fim;
struct tm c_fim;
p_fim = *localtime(&t_fim);
c_fim = *localtime(&t_fim);

或者,如果您使用的是POSIX系统,请尝试localtime_r()代替

© www.soinside.com 2019 - 2024. All rights reserved.