当前位置: 首页 > 知识库问答 >
问题:

如何计算C中x天后的日期

刘博文
2023-03-14

我正在用C语言编程一个微控制器,它有一个内部RTC,并自动增加一个日计数器(0-65536)。因此,考虑到用户调整的初始日期(DD/MM/YYYY),我需要根据该计数器更新日历。也就是说,我需要知道如何计算x天后的日期。有人知道算法吗?在网上找不到任何东西。

提前谢谢。丹尼尔

共有2个答案

耿建弼
2023-03-14

检查这个。也许它需要一些调整。有点蛮力,但是速度应该足够了,即使是微控制器。

#include <stdio.h>

static int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day, month, year;

unsigned short day_counter;

int is_leap(int y) {
    return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0);
}

next_day()
{
    day += 1; day_counter++;
    if (day > days_in_month[month]) {
        day = 1;
        month += 1;
        if (month > 12) {
            month = 1;
            year += 1;
            if (is_leap(year)) {
                days_in_month[2] = 29;
            } else {
                days_in_month[2] = 28;
            }
        }
    }
}

set_date(int d, int m, int y) 
{
    m < 1 ? m = 1 : 0;
    m > 12 ? m = 12 : 0;
    d < 1 ? d = 1 : 0;
    d > days_in_month[m] ? d = days_in_month[m] : 0;
    if (is_leap(y)){
        days_in_month[2] = 29;
    } else {
        days_in_month[2] = 28;
    }
    day = d;
    month = m;
    year = y;
}

skip_days(int x)
{
    int i;
    for (i=0;i<x;i++) next_day();
}

print_date()
{
    printf ("day: %d month: %d year: %d\n", day, month, year);
}

int main(int argc, char **argv)
{
    int i;

    set_date(5, 2, 1980);

    skip_days(10000);
    day_counter = 0;
    /* after this call next_day each day */

    print_date();

    return 0;
}
罗宪
2023-03-14

正如@moooeeep在回答同一个问题时所暗示的,

为了清晰和正确,你应该坚持现有的解决方案

#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;
}
 类似资料:
  • 本文向大家介绍python计算N天之后日期的方法,包括了python计算N天之后日期的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python计算N天之后日期的方法。分享给大家供大家参考。具体如下: python计算N天之后的日期,可以自己写成一个函数,想得到几天后的日期都行 输出结果如下: 希望本文所述对大家的Python程序设计有所帮助。

  • 问题内容: 我正在计算从“开始”到“结束”日期之间的天数。例如,如果起始日期为2010年4月13日,起始日期为2010年5月15日,则结果应为 如何使用JavaScript获得结果? 问题答案:

  • 问题内容: 我有两个输入日期来自Date Picker控件。我选择了开始日期2/2/2012和结束日期2/7/2012。我为此编写了以下代码。 我应该得到6的结果,但是我却得到5。 谁能告诉我如何获得确切的区别? 问题答案: 从Moment文档: 或包含起点: 避免手动打乱时间戳和时区。 根据您的特定用例,您可以 使用和/或强制diff在“端点”处包含或排除(如@kotpal在评论中所建议)。 设

  • 问题内容: 如果我有一对夫妇的字符串和被设置为(例如)和(意为2011年7月1至2011年7月17日)。我如何计算从开始日期到结束日期的天数?在给出的示例中,将是17天。 问题答案: 这是原始的方法

  • 问题 你需要找出两个日期间隔了几年,几个月,几天,几个小时,几分钟,几秒。 解决方案 利用 JavaScript 的日期计算函数 getTime() 。它提供了从 1970 年 1 月 1 日开始经过了多少毫秒。 DAY = 1000 * 60 * 60 * 24 d1 = new Date('02/01/2011') d2 = new Date('02/06/2011') days_pas

  • 问题内容: 我想100天前从13年4月8日开始作为日期。 如何使用pl / sql执行此操作? 问题答案: 假设情况是您所处的字符串。因此,您需要将其转换为using函数,然后简单地减去100个文字。 的SQL 2 from dual 3 / RES 29-12-2012 PL / SQL 2 l_res_date date; 3 l_in_date varchar2(11) := ‘08-APR