?? mktime.c
字號:
/* passed* linux/kernel/mktime.c** (C) 1991 Linus Torvalds*/#include <set_seg.h>
#include <time.h> // 時(shí)間頭文件,定義了標(biāo)準(zhǔn)時(shí)間數(shù)據(jù)結(jié)構(gòu)tm 和一些處理時(shí)間函數(shù)原型。/** 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. *//* * 這不是庫函數(shù),它僅供內(nèi)核使用。因此我們不關(guān)心小于1970 年的年份等,但假定一切均很正常。 * 同樣,時(shí)間區(qū)域TZ 問題也先忽略。我們只是盡可能簡單地處理問題。最好能找到一些公開的庫函數(shù) * (盡管我認(rèn)為minix 的時(shí)間函數(shù)是公開的)。 * 另外,我恨那個(gè)設(shè)置1970 年開始的人 - 難道他們就不能選擇從一個(gè)閏年開始?我恨格里高利歷、 * 羅馬教皇、主教,我什么都不在乎。我是個(gè)脾氣暴躁的人。 */#define MINUTE 60 // 1 分鐘的秒數(shù)。#define HOUR (60*MINUTE) // 1 小時(shí)的秒數(shù)。#define DAY (24*HOUR) // 1 天的秒數(shù)。#define YEAR (365*DAY) // 1 年的秒數(shù)。/* interestingly, we assume leap-years *//* 有趣的是我們考慮進(jìn)了閏年 */// 下面以年為界限,定義了每個(gè)月開始時(shí)的秒數(shù)時(shí)間數(shù)組。 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)};// 該函數(shù)計(jì)算從1970 年1 月1 日0 時(shí)起到開機(jī)當(dāng)日經(jīng)過的秒數(shù),作為開機(jī)時(shí)間。longkernel_mktime (struct tm *tm){ long res; int year; year = tm->tm_year - 70; // 從70 年到現(xiàn)在經(jīng)過的年數(shù)(2 位表示方式), // 因此會有2000 年問題。 /* magic offsets (y+1) needed to get leapyears right. */ /* 為了獲得正確的閏年數(shù),這里需要這樣一個(gè)魔幻偏值(y+1) */ res = YEAR * year + DAY * ((year + 1) / 4); // 這些年經(jīng)過的秒數(shù)時(shí)間 + 每個(gè)閏年時(shí)多1 天 res += month[tm->tm_mon]; // 的秒數(shù)時(shí)間,在加上當(dāng)年到當(dāng)月時(shí)的秒數(shù)。 /* and (y+2) here. If it wasn't a leap-year, we have to adjust */ /* 以及(y+2)。如果(y+2)不是閏年,那么我們就必須進(jìn)行調(diào)整(減去一天的秒數(shù)時(shí)間)。 */ if (tm->tm_mon > 1 && ((year + 2) % 4)) res -= DAY; res += DAY * (tm->tm_mday - 1); // 再加上本月過去的天數(shù)的秒數(shù)時(shí)間。 res += HOUR * tm->tm_hour; // 再加上當(dāng)天過去的小時(shí)數(shù)的秒數(shù)時(shí)間。 res += MINUTE * tm->tm_min; // 再加上1 小時(shí)內(nèi)過去的分鐘數(shù)的秒數(shù)時(shí)間。 res += tm->tm_sec; // 再加上1 分鐘內(nèi)已過的秒數(shù)。 return res; // 即等于從1970 年以來經(jīng)過的秒數(shù)時(shí)間。}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -