• 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!
    ",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]!
    ");
            exit(EXIT_FAILURE);
        }else {
            if(isleapyear(year))
            {
                if(month < 1 || month >12)
                {
                    printf("The month of the leap year is out of range
    !");
                }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
    !",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
    ",str_year,str_month,str_day);*/
        year = atoi(str_year);
        month = atoi(str_month);
        day = atoi(str_day);
        //printf("day:%d-%d-%d
    ",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
    ",year, month, day);
        sprintf(date, "%4d-%0d-%0d", year, month, day);
        //printlog(date);
        //printf("%s
    ",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
    ",isleapyear(2015));
        
        nextday(date);
        
        printlog(date);
        return 0;
    }
  • 相关阅读:
    Python字符编码补充
    shell脚本自动部署及监控
    【Linux】应用程序内存段布局
    【Linux】Core dump故障分析
    【Linux】GDB程序调试
    【Linux】小应用 大智慧
    【嵌入式】安装Linux系统到开发板
    【读书笔记】高效演讲
    【Linux】GCC编译
    【Linux】Linux 找回Root用户密码
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/5452676.html
Copyright © 2020-2023  润新知