#include<stdio.h> #include<time.h> #define u8 unsigned char u8 isRunNian(int year); int getYear(int *day); int getMonth(int *day,int year); int getMonthOrDay(int year,int month); int getHour(long second); int getMinute(long second); int getSecond(long second); int main() { int second = time(NULL); int day = (second/60/60+8)/24+1; int year= getYear(&day); printf("%4d年%2d月%2d日 %2d:%2d:%2d", year,getMonth(&day,year),day, getHour(second),getMinute(second),getSecond(second)); return 0; } int getMonth(int *day,int year) { int month=1; while(*day>getMonthOrDay(year,month)) { switch(month) { case 2: *day-=28; if(isRunNian(year)) { *day-=1; } break; case 4: case 6: case 9: case 11:*day-=30;break; default:*day-=31;break; } month++; } return month; } int getMonthOrDay(int year,int month) { int day; switch(month) { case 2: day=28; if(isRunNian(year)) { day=29; } break; case 4: case 6: case 9: case 11:day=30;break; default:day=31;break; } return day; } int getYear(int *day) { int year=1970; while(*day>365)//2000 366 { *day-=365; if(isRunNian(year)) { *day-=1; } year++; } if(*day==0) { year--; *day = 1; } return year; } u8 isRunNian(int year) { if(year%400==0 || (year%4==0&&year%100!=0)) { return 1;//代表是闰年 } return 0;//代表不是闰年 } int getHour(long second) { //当前系统时间是北京时间,但是获取的是世界协调时 ,所以我们需要加上8, return second/60/60%24+8; } int getMinute(long second) { return second/60%60; } int getSecond(long second) { return second%60; }