mktime.c中好像存在问题

这是0.11内核中的一个文件
/*
 *  linux/kernel/mktime.c
 *
 *  (C) 1991  Linus Torvalds
 */

#include

/*
 * This isn't the library routine, it is only used in the kernel.
 * as such, we don't care about years<1970 etc, but assume everything
 * is ok. Similarly, TZ etc is happily ignored. We just do everything
 * as easily as possible. Let's find something public for the library
 * routines (although I think minix times is public).
 */
/*
 * PS. I hate whoever though up the year 1970 - couldn't they have gotten
 * a leap-year instead? I also hate Gregorius, pope or no. I'm grumpy.
 */
#define MINUTE 60
#define HOUR (60*MINUTE)
#define DAY (24*HOUR)
#define YEAR (365*DAY)

/* interestingly, we assume leap-years */
static int month[12] = {
    0,
    DAY*(31),
    DAY*(31+29),
    DAY*(31+29+31),
    DAY*(31+29+31+30),
    DAY*(31+29+31+30+31),
    DAY*(31+29+31+30+31+30),
    DAY*(31+29+31+30+31+30+31),
    DAY*(31+29+31+30+31+30+31+31),
    DAY*(31+29+31+30+31+30+31+31+30),
    DAY*(31+29+31+30+31+30+31+31+30+31),
    DAY*(31+29+31+30+31+30+31+31+30+31+30)
};

long kernel_mktime(struct tm * tm)
{
    long res;
    int year;

    year = tm->tm_year - 70;
/* magic offsets (y+1) needed to get leapyears right.*/
    res = YEAR*year + DAY*((year+1)/4);
    res += month[tm->tm_mon];
/* and (y+2) here. If it wasn't a leap-year, we have to adjust */
    if (tm->tm_mon>1 && ((year+2)%4))
        res -= DAY;
    res += DAY*(tm->tm_mday-1);
    res += HOUR*tm->tm_hour;
    res += MINUTE*tm->tm_min;
    res += tm->tm_sec;
    return res;
}




看第一遍时觉得kernel_mktime()
函数写的很是巧妙,再仔细琢磨时, 个人觉得这个函数好像有点问题(也许是个人能力有 限, 不是函数有问题,而是自己没有领悟到更深的一层)。
在kernel/mktime.c 中的kernel_mktime()函数原型如下:
    41    long kernel_mktime(struct tm * tm)
    42    {
    43        long res;
    44        int year;
    45   
    46        year = tm->tm_year - 70;
    47    /* magic offsets (y+1) needed to get leapyears right.*/
    48        res = YEAR*year + DAY*((year+1)/4);
    49        res += month[tm->tm_mon];
    50    /* and (y+2) here. If it wasn't a leap-year, we have to adjust */
    51        if (tm->tm_mon>1 && ((year+2)%4))
    52            res -= DAY;
    53        res += DAY*(tm->tm_mday-1);
    54        res += HOUR*tm->tm_hour;
    55        res += MINUTE*tm->tm_min;
    56        res += tm->tm_sec;
    57        return res;
    58    }
我的问题处在第48行的“ ((year+1)/4)“上, 这 行代码功能:计算从1970年到现在经历的年数, 再将计算出来的年数乘以一年的秒数,转化为以秒为单位的时间。
((year+1)/4)是计算从1970年到现在的闰年数, 他 是按照闰年没4年出现一次的规律计算的。
而闰年是每4年出现一次吗?不是的。400年中闰年只有97个, 而不是100个,1900不是闰年,1800不是,2000是。
所以他 这样计算出来的闰年数对吗?

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2599) | 评论(2) | 转发(0) |
0

上一篇:ubuntu10.04源

下一篇:信号灯

给主人留下些什么吧!~~

sunjiangang-ok2014-01-27 22:28:47

c87276548:kernel里头计算的这个秒数应该是1970-2038年的 所有不存在所谓的世纪年不是闰年,因为这个时间段里头只有一个世纪年

谢谢

回复 | 举报

c872765482013-10-22 15:58:13

kernel里头计算的这个秒数应该是1970-2038年的 所有不存在所谓的世纪年不是闰年,因为这个时间段里头只有一个世纪年

评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值