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 YEAR001365
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;
推荐阅读
- c/c++|有感 Visual Studio 2015 RTM 简介 - 八年后回归 Dot Net,终于迎来了 Mvc 时代,盼走了 Web 窗体时代...
- C/C++|C/C++ basis 02
- Qt实战|Qt+OpenCV联合开发(二十一)--图像翻转与旋转
- Qt实战|Qt+OpenCV联合开发(十四)--图像感兴趣区域(ROI)的提取
- Qt实战|Qt+OpenCV联合开发(十三)--通道分离与合并
- opencv|Qt+OpenCV联合开发(十六)--图像几何形状绘制
- Qt实战|Qt+OpenCV联合开发(十七)--随机数与随机颜色
- SNAT的MASQUERADE地址选择与端口选择
- IPTABLES的连接跟踪与NAT分析
- IPVS分析