mktime() 在 C 中覆盖 time_t
mktime() overwrite time_t in C
我对 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
的值,如何解决?
尝试摆脱指针
struct tm p_fim;
struct tm c_fim;
p_fim = *localtime(&t_fim);
c_fim = *localtime(&t_fim);
或者,如果您使用的是 POSIX 系统,请尝试 localtime_r()
而不是
我对 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
的值,如何解决?
尝试摆脱指针
struct tm p_fim;
struct tm c_fim;
p_fim = *localtime(&t_fim);
c_fim = *localtime(&t_fim);
或者,如果您使用的是 POSIX 系统,请尝试 localtime_r()
而不是