📄 71.html
字号:
<html>
<head>
<title>kernel/mktime.c</title>
<meta name='robots' content='noindex,nofollow'>
<meta name='generator' content='GLOBAL-5.4.1'>
</head>
<body text='#191970' bgcolor='#f5f5dc' vlink='gray'>
<a name='TOP'><h2><a href='../mains.html'>root</a>/<a href='../files/104.html'>kernel</a>/mktime.c</h2>
<i><font color='green'>/* [<][>]<a href='#L52'>[^]</a>[v][top]<a href='#BOTTOM'>[bottom]</a><a href='../mains.html'>[index]</a><a href='../help.html'>[help]</a> */</font></i>
<hr>
<h2>DEFINITIONS</h2>
This source file includes following definitions.
<ol>
<li><a href='#L52' title='Defined at 52.'>kernel_mktime</a>
</ol>
<hr>
<pre>
<a name='L1'><i><font color='green'>/*</font></i>
<a name='L2'><i><font color='green'>* linux/kernel/mktime.c</font></i>
<a name='L3'><i><font color='green'>*</font></i>
<a name='L4'><i><font color='green'>* (C) 1991 Linus Torvalds</font></i>
<a name='L5'><i><font color='green'>*/</font></i>
<a name='L6'>
<a name='L7'><font color='darkred'>#include</font> <<a href='49.html'>time.h</a>> <i><font color='green'>// 时间头文件,定义了标准时间数据结构tm 和一些处理时间函数原型。</font></i>
<a name='L8'>
<a name='L9'><i><font color='green'>/*</font></i>
<a name='L10'><i><font color='green'>* This isn't the library routine, it is only used in the kernel.</font></i>
<a name='L11'><i><font color='green'>* as such, we don't care about years<1970 etc, but assume everything</font></i>
<a name='L12'><i><font color='green'>* is ok. Similarly, TZ etc is happily ignored. We just do everything</font></i>
<a name='L13'><i><font color='green'>* as easily as possible. Let's find something public for the library</font></i>
<a name='L14'><i><font color='green'>* routines (although I think minix times is public).</font></i>
<a name='L15'><i><font color='green'>*/</font></i>
<a name='L16'><i><font color='green'>/*</font></i>
<a name='L17'><i><font color='green'>* PS. I hate whoever though up the year 1970 - couldn't they have gotten</font></i>
<a name='L18'><i><font color='green'>* a leap-year instead? I also hate Gregorius, pope or no. I'm grumpy.</font></i>
<a name='L19'><i><font color='green'> */</font></i>
<a name='L20'><i><font color='green'>/*</font></i>
<a name='L21'><i><font color='green'> * 这不是库函数,它仅供内核使用。因此我们不关心小于1970 年的年份等,但假定一切均很正常。</font></i>
<a name='L22'><i><font color='green'> * 同样,时间区域TZ 问题也先忽略。我们只是尽可能简单地处理问题。最好能找到一些公开的库函数</font></i>
<a name='L23'><i><font color='green'> * (尽管我认为minix 的时间函数是公开的)。</font></i>
<a name='L24'><i><font color='green'> * 另外,我恨那个设置1970 年开始的人 - 难道他们就不能选择从一个闰年开始?我恨格里高利历、</font></i>
<a name='L25'><i><font color='green'> * 罗马教皇、主教,我什么都不在乎。我是个脾气暴躁的人。</font></i>
<a name='L26'><i><font color='green'> */</font></i>
<a name='L27'><font color='darkred'>#define</font> <a href='../R/132.html' title='Multiple refered from 2 places.'>MINUTE</a> 60 <i><font color='green'>// 1 分钟的秒数。</font></i>
<a name='L28'><font color='darkred'>#define</font> <a href='../R/86.html' title='Multiple refered from 2 places.'>HOUR</a> (60*<a href='../S/71.html#L27' title='Defined at 27 in kernel/mktime.c.'>MINUTE</a>) <i><font color='green'>// 1 小时的秒数。</font></i>
<a name='L29'><font color='darkred'>#define</font> <a href='../R/22.html' title='Multiple refered from 15 places.'>DAY</a> (24*<a href='../S/71.html#L28' title='Defined at 28 in kernel/mktime.c.'>HOUR</a>) <i><font color='green'>// 1 天的秒数。</font></i>
<a name='L30'><font color='darkred'>#define</font> <a href='../S/71.html#L61' title='Refered from 61 in kernel/mktime.c.'>YEAR</a> (365*<a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a>) <i><font color='green'>// 1 年的秒数。</font></i>
<a name='L31'>
<a name='L32'><i><font color='green'>/* interestingly, we assume leap-years */</font></i>
<a name='L33'><i><font color='green'>/* 有趣的是我们考虑进了闰年 */</font></i>
<a name='L34'><i><font color='green'>// 下面以年为界限,定义了每个月开始时的秒数时间数组。</font></i>
<a name='L35'><b>static</b> <b>int</b> month[12] = <font color='red'>{</font>
<a name='L36'> 0,
<a name='L37'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31),
<a name='L38'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29),
<a name='L39'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31),
<a name='L40'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30),
<a name='L41'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31),
<a name='L42'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30),
<a name='L43'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30 + 31),
<a name='L44'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31),
<a name='L45'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30),
<a name='L46'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31),
<a name='L47'> <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30)
<a name='L48'><font color='red'>}</font>;
<a name='L49'>
<a name='L50'><i><font color='green'>// 该函数计算从1970 年1 月1 日0 时起到开机当日经过的秒数,作为开机时间。</font></i>
<a name='L51'><b>long</b>
<a name='L52'><a href='../S/52.html#L124' title='Refered from 124 in init/main.c.'>kernel_mktime</a> (<b>struct</b> tm *tm)
<a name='L53'><font color='red'>{</font>
<a name='L54'> <b>long</b> res;
<a name='L55'> <b>int</b> year;
<a name='L56'>
<a name='L57'> year = tm->tm_year - 70; <i><font color='green'>// 从70 年到现在经过的年数(2 位表示方式),</font></i>
<a name='L58'> <i><font color='green'>// 因此会有2000 年问题。</font></i>
<a name='L59'> <i><font color='green'>/* magic offsets (y+1) needed to get leapyears right. */</font></i>
<a name='L60'> <i><font color='green'>/* 为了获得正确的闰年数,这里需要这样一个魔幻偏值(y+1) */</font></i>
<a name='L61'> res = <a href='../S/71.html#L30' title='Defined at 30 in kernel/mktime.c.'>YEAR</a> * year + <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * ((year + 1) / 4); <i><font color='green'>// 这些年经过的秒数时间 + 每个闰年时多1 天</font></i>
<a name='L62'> res += month[tm->tm_mon]; <i><font color='green'>// 的秒数时间,在加上当年到当月时的秒数。</font></i>
<a name='L63'> <i><font color='green'>/* and (y+2) here. If it wasn't a leap-year, we have to adjust */</font></i>
<a name='L64'> <i><font color='green'>/* 以及(y+2)。如果(y+2)不是闰年,那么我们就必须进行调整(减去一天的秒数时间)。 */</font></i>
<a name='L65'> <b>if</b> (tm->tm_mon > 1 && ((year + 2) % 4))
<a name='L66'> res -= <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a>;
<a name='L67'> res += <a href='../S/71.html#L29' title='Defined at 29 in kernel/mktime.c.'>DAY</a> * (tm->tm_mday - 1); <i><font color='green'>// 再加上本月过去的天数的秒数时间。</font></i>
<a name='L68'> res += <a href='../S/71.html#L28' title='Defined at 28 in kernel/mktime.c.'>HOUR</a> * tm->tm_hour; <i><font color='green'>// 再加上当天过去的小时数的秒数时间。</font></i>
<a name='L69'> res += <a href='../S/71.html#L27' title='Defined at 27 in kernel/mktime.c.'>MINUTE</a> * tm->tm_min; <i><font color='green'>// 再加上1 小时内过去的分钟数的秒数时间。</font></i>
<a name='L70'> res += tm->tm_sec; <i><font color='green'>// 再加上1 分钟内已过的秒数。</font></i>
<a name='L71'> <b>return</b> res; <i><font color='green'>// 即等于从1970 年以来经过的秒数时间。</font></i>
<a name='L72'><font color='red'>}</font>
</pre>
<hr>
<a name='BOTTOM'>
<i><font color='green'>/* [<][>][^]<a href='#L52'>[v]</a><a href='#TOP'>[top]</a>[bottom]<a href='../mains.html'>[index]</a><a href='../help.html'>[help]</a> */</font></i>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -