NEXTDAY

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "com_time.h"
#include "com_func.h"
/*
 * Definition of leap year:

Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.
For example:  2004, 2008, 1012 are leap year.
*/
int isleapyear(int year)
{
    if ((year%400 == 0) || ((year%4 == 0) && (year%100 != 0))) {
        printf("The year:%d is a leap year!\n",year);
        return 1;
    }
    return 0;
}

int istimelegal(int year, int month, int day){
    if(year <= 0 || year >=3000)
    {
        printf("the year is out of range([0-3000]!\n");
        exit(EXIT_FAILURE);
    }else {
        if(isleapyear(year))
        {
            if(month < 1 || month >12)
            {
                printf("The month of the leap year is out of range\n!");
            }else {
                if(month == 2)
                {
                    if(day > 29)
                    {
                        printf("day:%d is out of range",day);
                        exit(EXIT_FAILURE);
                    }
                }
                else{
                    if((day <= 0) || (day > 31))
                    {
                        printf("day:%d is out of range",day);
                        exit(EXIT_FAILURE);
                    }
                }

            }
        }else {//not leap year
            if(month == 2)
            {
                if(day > 28)
                {
                    printf("The month:%d of the year is out of range\n!",month);
                    exit(EXIT_FAILURE);
                }
            }else {
                if((day <= 0) || (day > 31))
                {
                    printf("day:%d is out of range",day);
                    exit(EXIT_FAILURE);
                }

            }

        }
    }
    return 1;

}

//2016-05-02
char *nextday(char *date)
{
    int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char str_year[5]={0};
    char str_month[3]={0};
    char str_day[3]={0};
    int year=0;
    int month=0;
    int day=0;
    //strncpy();
    sscanf(date,"%4s-%2s-%2s",str_year,str_month,str_day);
    /*printlog(str_year);
    printlog(str_month);
    printlog(str_day);
    printf("%s-%s,%s%s,%s,%s\n",str_year,str_month,str_day);*/
    year = atoi(str_year);
    month = atoi(str_month);
    day = atoi(str_day);
    //printf("day:%d-%d-%d\n",year, month, day);
    if(isleapyear(year))
    {
        days[2] = 29;
    }
    else {
        days[2] = 28;
    }

    //judge time illegal or not
    istimelegal(year,month,day);
    int i = 0;
    int tmp = 0;
    tmp = month;
    for(i = 1;i < 13;i++)
    {
        if(tmp != i)
        {
            continue;
        }else {
            if(day < days[i])
            {
                printlog("&&&&&&&&&&");
                day++;
            }else if(day == days[i]){
                printlog("0000000");
                month++;
                if(month > 12)
                {
                    printlog("121212122");
                    year++;
                    month %= 12;
                }
                printlog("********");
                day = 1;

            }else {
                printlog("@@@@@@@@@@");
                break;
            }
            /*printlog("&&&&&&&&&&");
            day++;
            printlog("@@@@@@@@@@");*/
        }
    }

    //printf("day:%d-%d-%d\n",year, month, day);
    sprintf(date, "%4d-%0d-%0d", year, month, day);
    //printlog(date);
    //printf("%s\n",date);

    return date;
}
---------------------------------------------------------------------------------------
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int main(int argc, char *argv[])
{
    //char date[]="2016-03-31";
    //char date[]="2016-02-29";
    //char date[]="2016-03-09";
    //char date[]="2016-12-31";

    //char date[]="2015-03-31";
    char date[]="2015-02-29";
    //char date[]="2015-03-09";
    //char date[]="2015-12-31";
    //printf("%d\n",isleapyear(2015));
    
    nextday(date);
    
    printlog(date);
    return 0;
}
上一篇:rabbitmq集群安装与配置(故障恢复)


下一篇:用WebStorm进行Angularjs 2的开发