C语言的day 2代码打卡 1.获取当前时间(注释部分仅为自己理解,语言组织有些不清楚)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main(){
unsigned long totalsec=0;
unsigned int year=0,month=0,day=0,hour=0,min=0,sec=0,leap=0;
unsigned int daysperyear=0,dayspermonth=0;
totalsec=time(NULL);
totalsec+=8*60*60;
sec=totalsec%60;
min=totalsec/60%60;
hour=totalsec/60/60%24;
day=totalsec/60/60/24+1;
for(year=1970;;year++){
leap=( (year%100!=0) && (year%4==0)) || (year%400==0);
if(leap==1)
daysperyear=366;
else
daysperyear=365;
if(day<=daysperyear)
break;
else
day=day-daysperyear;
}
for(month=1;;month++){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:dayspermonth=31;break;
case 4:
case 6:
case 9:
case 11:dayspermonth=30;break;
case 2:
if(leap==1)
dayspermonth=29;
else
dayspermonth=28;
break;
}
if(day<=dayspermonth)
break;
else
day=day-dayspermonth;
}
printf("the current time is %d-%02d-%02d ",year,month,day);
printf("%02d:%02d:%02d\n",hour,min,sec);
}
运行结果如下:
|