UNIX时间戳转东八区北京日期时间格式
#include <stdio.h>
#include <time.h>
const int ciday_seconds = 3600 *24;
const int cileapyear_seconds = 3600 *24 *365;
int isLeapYear(int year)
{
return (year%400==0 || (year%100 && year%4==0 ))
?1:0;
}
const int DaysMonth[2][12] =
{
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int getDaysFromYearAndMonth(int y,int m)
{
return m<13 && m>0?DaysMonth[isLeapYear(y)][m]:0;
}
int main(void)
{
int timestamp = 0;
scanf("%d",×tamp);
clock_t begin = clock();
timestamp += 3600 *8;
int y,m,d,h=0,min=0,sec=0;
int temp =timestamp;
for(y = 1970; ; y++)
{
temp -= isLeapYear(y)*ciday_seconds + cileapyear_seconds;
if(temp >0)
timestamp = temp;
else
break;
}
temp = timestamp;
for(m=1;m<13; ++m)
{
temp -= getDaysFromYearAndMonth(y,m) * ciday_seconds;
if(temp >0)
timestamp = temp;
else
break;
}
temp = timestamp;
for(d=1;d<=getDaysFromYearAndMonth(y,m);++d)
{
temp -= ciday_seconds;
if(temp >0)
timestamp = temp;
else
break;
}
sec = timestamp %60;
min = timestamp /60 ;
h = min / 60;
min %= 60;
begin = clock() - begin;
printf("%4d/%02d/%02d %02d:%02d:%02d\n",y,m,d,h,min,sec);
printf("It takes me %f seconds\n",((float)begin/CLOCKS_PER_SEC));
return 0;
}