1.得到的时间秒数+UTC偏移量
如:北京时间UTC+08:00
sec = sec + BEIJINGTIME*60*60; //小时要换算成秒
2.通过秒计算出总天数
day= sec/DAY; //DAY=24*60*60
3.通过计算到的总天数减出初始日期天数
day=day-YEARSTART; //默认初始年份1970年YEARSTART=0, 设定初始年份2000年YEARSTART=360*(2000-1970)+7,7为修正天数
4.由于存在闰年要分别计算年份
#define YEAR400 (365*4*100 + (4*(100/4 - 1) + 1))
#define YEAR100 (365*100 + (100/4 - 1))
#define YEAR004 (365*4 + 1)
#define YEAR001 365
y400 = day/YEAR400;
y100 = (day - y400*YEAR400)/YEAR100;
y004 = (day - y400*YEAR400 - y100*YEAR100)/YEAR004;
y001 = (day - y400*YEAR400 - y100*YEAR100 - y004*YEAR004)/YEAR001;
year = y400*4*100 + y100*100 + y004*4 + y001*1 + YEARSTART;
5.计算当年剩下的天数
date = (day - y400*4*100 + y100*100 + y004*4)%YEAR001;
6.计算月、日
month_day[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (!(year%1000))
{
if (!(year/1000%4))
{
month_day[2] = 29;
}
}
else
{
if (!(year%4))
{
month_day[2] = 29;
}
}
for (n=1; n<=12; n++)
{
if (date - month_day[n] < 0)
{
break;
}
else
{
date = date - month_day[n];
}
}
date = date + 1;
month = n;
7.计算时、分、秒
hour = sec/(24*60*60);
min = sec/60 - sec/(60*60)*60;
sec = sec - sec/60*60;