《linux 内核完全剖析》 mktime.c
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了《linux 内核完全剖析》 mktime.c,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1935字,纯文字阅读大概需要3分钟。
内容图文
![《linux 内核完全剖析》 mktime.c](/upload/InfoBanner/zyjiaocheng/1103/72e4298686554ffc820ba3ea7eb9e226.jpg)
tm结构体的定义在time.h里面
struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; };
/*
* linux/kernel/mktime.c
*
* (C) 1991 Linus Torvalds
*/
#include <time.h>
/*
* 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 //一分钟60秒,这里60是以秒为单位计数
#define HOUR (60*MINUTE) //一小时60分钟
#define DAY (24*HOUR) //一天24小时
#define YEAR (365*DAY) //一年365天
/* 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;//从1970年开始计时,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))
//因为是从1970年算起的,这里year+2就能耦合上闰年。。。换而言之,1972年是闰年,但是年差是2,这里补上2就对齐了。。。。
res -= DAY;//year不是闰年,则减一天
res += DAY*(tm->tm_mday-1);
res += HOUR*tm->tm_hour;
res += MINUTE*tm->tm_min;
res += tm->tm_sec;//时间精确到秒
return res;
}
原文:http://blog.csdn.net/cinmyheart/article/details/25118715
内容总结
以上是互联网集市为您收集整理的《linux 内核完全剖析》 mktime.c全部内容,希望文章能够帮你解决《linux 内核完全剖析》 mktime.c所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。