我正在用 C 编写一个微 Controller ,它有一个内部 RTC 并自动递增一个日计数器 (0-65536)。因此,鉴于用户调整的初始日期 (DD/MM/YYYY),我需要根据该计数器更新日历。也就是说,我需要知道如何计算 x 天后的日期。有谁知道一个算法吗?在整个网络上找不到任何内容。
提前致谢。 丹尼尔
最佳答案
正如@moooeeeeep 在 his answer 中建议的那样同样的问题,
For the sake of clarity and correctness, you should stick to the existing solutions.
#include <stdio.h>
#include <time.h>
int main()
{
/* initialize */
int y=1980, m=2, d=5;
struct tm t = { .tm_year=y-1900, /* The number of years since 1900 */
.tm_mon=m-1, /* month, range 0 to 11 */
.tm_mday=d };
/* modify */
t.tm_mday += 40;
mktime(&t);
/* show result */
printf("%s", asctime(&t)); /* prints: Sun Mar 16 00:00:00 1980 */
return 0;
}
关于c - 如何在C中计算x天后的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209097/