某个月的最后一天

时间:2022-10-16 09:27:32

I've got an issue which I can't find a solution. Or at least a "good" one. I want to find the last day of the month given a month and a year in C.

我遇到了一个无法找到解决方案的问题。或者至少是一个“好”的。我想在C中找到一个月和一年的最后一天。

For example :

例如 :

last_day(10, 2017) > 31
last_day(02, 2017) > 28
last_day(02, 2016) > 29
last_day(01, 2017) > 31
last_day(12, 2010) > 31

last_day(X, Y) > X is the month, Y the year

last_day(X,Y)> X是月份,Y是年份

Here is my idea: Get the day on the month X + 1, of year Y. Remove 1 day from this date.

这是我的想法:获取Y年第X + 1个月的日期。从此日期开始删除1天。

I would like to know if there is a better solution than that, since that will a make "lot" of operation for a "simple" thing.

我想知道是否有比这更好的解决方案,因为这将为“简单”的事情做很多操作。

Thanks.

Edit : https://ideone.com/sIISO1

编辑:https://ideone.com/sIISO1

#include <stdio.h>
#include <time.h>
#include <string.h>

int main(void) {
    struct tm tm;
    char out[256];

    memset(&tm, 0, sizeof(struct tm));

    tm.tm_mon = 1;
    tm.tm_mday = 0;

    strftime(out, 256, "%d-%m-%Y", &tm);

    printf("%s", out);

    return 0;
}

I've tested by using struct tm, and day = 0, in order to get the previous day but did not work

我已经使用struct tm和day = 0进行了测试,以便获得前一天但没有工作

1 个解决方案

#1


0  

Ask point out in the commentary, I've complexify the problem way to much.

在评论中指出,我已经将问题的方式复杂化了很多。

I have been inspired by what @Agnishom Chattopadhyay said in comment, which is get the date from a lookup table.

我对@Agnishom Chattopadhyay在评论中所说的内容有所启发,这是从查询表中获取日期。

But I did make a function which did that

但我确实做了一个功能

#include <stdio.h>

int days_in_month(int month, int year) {
    if ( year < 1582 ) return 0; /* Before this year the Gregorian Calendar was not define */  
    if ( month > 12 || month < 1 ) return 0;

    if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
    else if (month == 2) return (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28);
    return 31;
}

int main() {
    printf("%d\n", days_in_month(10, 2017));    
    printf("%d\n", days_in_month(2, 2000));
    printf("%d\n", days_in_month(2, 1300)); // Does not work !
    printf("%d\n", days_in_month(2, 2018));
    printf("%d\n", days_in_month(2, 2016));
}

https://ideone.com/5OZ3pZ

#1


0  

Ask point out in the commentary, I've complexify the problem way to much.

在评论中指出,我已经将问题的方式复杂化了很多。

I have been inspired by what @Agnishom Chattopadhyay said in comment, which is get the date from a lookup table.

我对@Agnishom Chattopadhyay在评论中所说的内容有所启发,这是从查询表中获取日期。

But I did make a function which did that

但我确实做了一个功能

#include <stdio.h>

int days_in_month(int month, int year) {
    if ( year < 1582 ) return 0; /* Before this year the Gregorian Calendar was not define */  
    if ( month > 12 || month < 1 ) return 0;

    if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
    else if (month == 2) return (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28);
    return 31;
}

int main() {
    printf("%d\n", days_in_month(10, 2017));    
    printf("%d\n", days_in_month(2, 2000));
    printf("%d\n", days_in_month(2, 1300)); // Does not work !
    printf("%d\n", days_in_month(2, 2018));
    printf("%d\n", days_in_month(2, 2016));
}

https://ideone.com/5OZ3pZ