首页 > 代码库 > 输入一个日期和时间,输出下一秒的日期和时间

输入一个日期和时间,输出下一秒的日期和时间

 1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string> 4 #include <string.h> 5 #include <iostream> 6 #include <vector> 7 #include <stack> 8 using namespace std; 9 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl10 11 int isleapyear(int y);12 void ns(int y,int m,int d,int h,int mi,int s);13 int main(void) {14     freopen("../tmp.txt","r",stdin);15     int y,m,d;16     int h,mi,s;17     while(scanf("%d%d%d%d%d%d", &y,&m,&d,&h,&mi,&s)!=EOF){18         ns(y,m,d,h,mi,s);19     }20     return 0;21 }22 int isleapyear(int y){23     if(y%4==0&&y%100!=0 || y%400==0)24         return 1;25     return 0;26 }27 28 void ns(int y,int m,int d,int h,int mi,int s){29     int mth[13]={0,31,28,31,30,31, 30,31,31,30,31, 30,31};30     if(isleapyear(y)){31         mth[2] += 1;32     }33     if(m<1 || m > 12 || d>mth[m] || d<1 || h<0 || h>23 || mi<0 || mi >59 || s<0 || s>59){34         printf("the data(%d-%d-%d %d:%d:%d) is illegal!\n",y,m,d,h,mi,s);35         return;36     }37     if(s==59){38         s=0;39         if(mi==59){40             mi=0;41             if(h==23){42                 h=0;43                 if(d==mth[m]){44                     d=1;45                     if(m==12){46                         m=1;47                         y+=1;48                     }else {49                         m+=1;50                     }51                 }else {52                     d+=1;53                 }54             }else {55                 h+=1;56             }57         }else {58             mi+=1;59         }60 61     }else {//s < 5962         s+=1;63     }64 65     printf("%d-%d-%d %d:%d:%d\n",y,m,d,h,mi,s);66 }

 

技术分享
second += 1;           if(second == 60)                                    // 增加一秒之后,完成日期和时间的更新      {          second = 0;          minute += 1;          if(minute == 60)          {              minute = 0;              hour += 1;              if(hour == 24)              {                  hour = 0;                  day += 1;                  if(day > DayOfMonth[month-1])                  {                      day = 1;                      month += 1;                      if(month == 13)                      {                          month = 1;                          year += 1;                      }                  }              }          }      }  
View Code

 

输入一个日期和时间,输出下一秒的日期和时间