我如何计算当月的星期日?

时间:2017-08-11 17:57:35

标签: c++ arduino arduino-esp8266 sparkcore

我制作了一个LED挂钟&基于arduino的日历,我现在想要修改它以添加一个变量来自动调整DST的偏移量。我已经每天2次上网时间服务器以确保准确性,但时间服务器通常不提供DST信息。更改发生在3月的第2个星期日和11月的第1个星期日。 (https://www.nist.gov/pml/time-and-frequency-division/popular-links/daylight-saving-time-dst)基本上我需要知道它是3月的第2个星期日,并告诉它将时区偏移调整为+1,然后在11月的第1个星期日调整-1。我只是不确定如何实现这一点。

我想的是:

如果 月== 3或11 和 如果工作日== 1; 然后 星期日+ 1; 其他 sunday = 0;

如果星期日== 2; 和 月== 3; 然后 isDST = TRUE;

如果星期日== 1; 和 月== 11; 然后 isDST = FALSE;

我希望这是我想要做的足够好的例子。我讨厌使用这么多嵌套的但是这是我能想到的最好的仍然是一个相对的新手。这甚至可以工作还是有更好的方法去做?对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在这里,我为您制作了一个C ++代码。你不需要C ++ 11来运行它。

它是如何工作的,它获得当前的月份和年份。检查年份是否为闰年,然后继续设置最大日期金额。让我们说2017年8月。最长的一天将是31。

程序开始运行,如果它命中的那天是星期天,则会计算sundayCounter

通过这种方式,您可以自动获得当月的周日数量。

这是我的代码,随意使用它。它完全符合您的要求。您想要的变量是sundayCounter。玩得开心。

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    bool isLeap = false;
    int sundayCounter = 0;
    int MAX_DAY;
    int month, year;
    string monthText;

    time_t theTime = time(NULL);
    struct tm *aTime = localtime(&theTime);
    month = aTime->tm_mon;
    year = aTime->tm_year;

    if (year % 4 == 0)
        {;
            if (year % 100 == 0)
            {
                if (year % 400 == 0)
                    isLeap = true;
                else
                    isLeap = false;
            }
            else
                isLeap = true;
        }
    else {
        isLeap = false;
    }

    switch(month)
    {
    case 0:
        MAX_DAY = 31;
        break;
    case 1:
        if(isLeap)
            MAX_DAY = 29;
        else
            MAX_DAY = 28;
        break;
    case 2:
        MAX_DAY = 31;
        break;
    case 3:
        MAX_DAY = 30;
        break;
    case 4:
        MAX_DAY = 31;
        break;
    case 5:
        MAX_DAY = 30;
        break;
    case 6:
        MAX_DAY = 31;
        break;
    case 7:
        MAX_DAY = 31;
        break;
    case 8:
        MAX_DAY = 30;
        break;
    case 9:
        MAX_DAY = 31;
        break;
    case 10:
        MAX_DAY = 30;
        break;
    case 11:
        MAX_DAY = 31;
        break;
    }

    for(int day = 0; day <= MAX_DAY; day++) {
        struct tm time_in = { 0, 0, 0, // second, minute, hour
                day, month + 1, year - 1900 }; // 1-based day, 0-based month, year since 1900

        time_t time_temp = mktime( & time_in );

        // the return value from localtime is a static global - do not call
        // this function from more than one thread!
        tm const *time_out = localtime( & time_temp );

        switch(time_out->tm_wday)
        {
        case 0:
            sundayCounter++;
            break;
        }
    }

    const char* month_list[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int current_month = month+1;
    const char* current_month_name = month_list[current_month-1];
    cout << "Sunday amount in " << current_month_name << " " << (year + 1900) << ": " << sundayCounter;
    cout << endl;
    return 0;
}

输出:

Sunday amount in August 2017: 4
相关问题