Linux内核中的mktime()函数位于kernel/time.c内
该函数主要用于内核启动时,将CMOS中的 年-月-日 时:分:秒 信息转换为距离1970-01-01 00:00:00的秒数
具体定义如下:
unsignedlong
mktime(constunsignedintyear0,constunsignedintmon0,
constunsignedintday,constunsignedinthour,
constunsignedintmin,constunsignedintsec)
{
unsignedintmon = mon0, year = year0;
/* 1..12 -> 11,12,1..10 */
if(0 >= (int) (mon -= 2)) {
mon += 12;/* Puts Feb last since it has leap day */
year -= 1;
}
return((((unsignedlong)
(year/4 - year/100 + year/400 + 367*mon/12 + day) +
year*365 - 719499
)*24 + hour/* now have hours */
)*60 + min/* now have minutes */
)*60 + sec;/* finally seconds */
}
注意到计算的结果为相对时间
具体的计算方法也进行了两次相对运算
1、将时间轴整体后移2个月,以方便闰年的计算
原来相对1970-01-01 00:00:00,变成了相对1969-11-01 00:00:00
被计算的参数时间数值上也相对移位减小
但是这并不影响原来的相对差值
2、时间基准点为1-1-1 00:00:00(移位2个月后的)
即分别计算参数时间与基准点的秒数A
和1969-11-01 00:00:00与基准点的秒数B
然后A - B即最终结果
因为 天 时:分:秒 的相对基准固定
故算法中主要关心年份和月份到天数的转换
先考虑通用的 年-月-日 转天数的计算方法
例如:计算year-mon-day距离公元1-1-1的天数
公式可以表示为:(year - 1) * 365 + f(mon) + (day - 1) + leap_days
f(mon)表示关于mon的一个函数关系
可以使用类似如下的代码实现
intmon_passed_2days(intm)
{
intx = 0;
switch(m - 1) {
default:
break;
case11:
x += 30;
case10:
x += 31;
case9:
x += 30;
case8:
x += 31;
case7:
x += 31;
case6:
x += 30;
case5:
x += 31;
case4:
x += 30;
case3:
x += 31;
case2:
x += 28;
case1:
x += 31;
}
returnx;
}
leap_days表示对闰年天数的修正
在计算闰年所增加的天数时使用公式:(year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400
式中各个除法运算 / 后,还需向下取整,表达式中省略了符号 [ ] ,下同
这里减1是因为当前的year补闰1天需要根据月份进行单独判断处理
可以使用类似如下的代码
if(mon > 2 && is_leap_year(year)) {
days += 1;
}
当将时间轴移位2个月
将闰2月变成了1年之中的最后一个月份时
此时将闰年需要修正的一天记为该年之中的0月,这个月要么是0天,要么是1天
那么原来为当前年进行2月修正的判断便成为了
if(mon > 0 && is_leap_year(year)) {
days += 1;
}