博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《linux 内核全然剖析》 mktime.c
阅读量:6969 次
发布时间:2019-06-27

本文共 1808 字,大约阅读时间需要 6 分钟。

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 
/* * 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; }

你可能感兴趣的文章
redis设置密码
查看>>
基于flash cs3 actionscript3.0的mp3网络播放器
查看>>
webJSP倒计时
查看>>
.[转] 心情不好的时候,看这10部励志短片…… 2012-5-29 15:49阅读(12)转...
查看>>
RHEL7.0 防火墙入门
查看>>
WordPress 伪静态规则(Apache/Nginx)
查看>>
修改从自动补全文本下拉列表获取的内容
查看>>
虚拟化——使用模板和克隆虚拟机
查看>>
lsof有一把利器
查看>>
Redis消息通知系统的实现
查看>>
XEN虚拟机复制
查看>>
redis未授权访问导致的安全问题
查看>>
salt Syndic 的实现
查看>>
JSP/Java 获取路径问题
查看>>
我的友情链接
查看>>
微信小程序授权获取手机号,提示获取失败,该appId没有权限
查看>>
UITableView, UIPickerView为什么要使用delegate模式
查看>>
linux(centos)搭建SVN服务器
查看>>
如何让NComputing桌面终端用户做到独享指定CPU、内存和磁盘大小
查看>>
VTK面绘制(SR)与体绘制(VR)
查看>>