Linux内核中mktime()函数算法分析


Linux内核中的mktime()函数位于kernel/time.c内

该函数主要用于内核启动时,将CMOS中的 年-月-日 时:分:秒 信息转换为距离1970-01-01 00:00:00的秒数

具体定义如下:

 
  1. unsigned long  
  2. mktime(const unsigned int year0, const unsigned int mon0,  
  3.        const unsigned int day, const unsigned int hour,  
  4.        const unsigned int min, const unsigned int sec)  
  5. {  
  6.     unsigned int mon = mon0, year = year0;  
  7.   
  8.     /* 1..12 -> 11,12,1..10 */  
  9.     if (0 >= (int) (mon -= 2)) {  
  10.         mon += 12;  /* Puts Feb last since it has leap day */  
  11.         year -= 1;  
  12.     }  
  13.   
  14.     return ((((unsigned long)  
  15.           (year/4 - year/100 + year/400 + 367*mon/12 + day) +  
  16.           year*365 - 719499  
  17.         )*24 + hour /* now have hours */  
  18.       )*60 + min /* now have minutes */  
  19.     )*60 + sec; /* finally seconds */  
  20. }  

注意到计算的结果为相对时间

具体的计算方法也进行了两次相对运算

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的一个函数关系

可以使用类似如下的代码实现

 
  1. int mon_passed_2days(int m)  
  2. {  
  3.     int x = 0;  
  4.   
  5.     switch (m - 1) {  
  6.     default:  
  7.         break;  
  8.     case 11:  
  9.         x += 30;  
  10.     case 10:  
  11.         x += 31;  
  12.     case 9:  
  13.         x += 30;  
  14.     case 8:  
  15.         x += 31;  
  16.     case 7:  
  17.         x += 31;  
  18.     case 6:  
  19.         x += 30;  
  20.     case 5:  
  21.         x += 31;  
  22.     case 4:  
  23.         x += 30;  
  24.     case 3:  
  25.         x += 31;  
  26.     case 2:  
  27.         x += 28;  
  28.     case 1:  
  29.         x += 31;  
  30.     }  
  31.     return x;  
  32. }  

leap_days表示对闰年天数的修正

在计算闰年所增加的天数时使用公式:(year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400

式中各个除法运算 /  后,还需向下取整,表达式中省略了符号 [ ] ,下同

这里减1是因为当前的year补闰1天需要根据月份进行单独判断处理

可以使用类似如下的代码

 
  1. if (mon > 2 && is_leap_year(year)) {  
  2.         days += 1;  
  3. }  

当将时间轴移位2个月

将闰2月变成了1年之中的最后一个月份时

此时将闰年需要修正的一天记为该年之中的0月,这个月要么是0天,要么是1天

那么原来为当前年进行2月修正的判断便成为了

 
  1. if (mon > 0 && is_leap_year(year)) {  
  2.         days += 1;  
  3. }  

显然mon > 0总是成立

这样对所有闰年的修正表达式便简化成为了:year  / 4 - year / 100 + year / 400

  • 1
  • 2
  • 下一页

相关内容