����8-4 linux/kernel/mktime.c����
1 /*
2 * linux/kernel/mktime.c
3 *
4 * (C) 1991 Linus Torvalds
5 */
6
7 #include <time.h> // ʱ��ͷ�ļ��������˱�ʱ�����ݽṹtm��һЩ����ʱ�亯��ԭ�͡�
8
9 /*
10 * This isn't the library routine, it is only used in the kernel.
11 * as such, we don't care about years<1970 etc, but assume everything
12 * is ok. Similarly, TZ etc is happily ignored. We just do everything
13 * as easily as possible. Let's find something public for the library
14 * routines (although I think minix times is public).
15 */
16 /*
17 * PS. I hate whoever though up the year 1970 - couldn't they have gotten
18 * a leap-year instead? I also hate Gregorius, pope or no. I'm grumpy.
19 */
/*
* �ⲻ�ǿ⺯�����������ں�ʹ�á�������Dz�����С��1970�����ݵȣ����ٶ�һ�о���������
* ͬ����ʱ������TZ����Ҳ�Ⱥ��ԡ�����ֻ�Ǿ����ܼش������⡣������ҵ�һЩ�����Ŀ⺯��
* ����������Ϊminix��ʱ�亯���ǹ����ģ���
* ���⣬�Һ��Ǹ�����1970�꿪ʼ���� - �ѵ����ǾͲ���ѡ���һ�����꿪ʼ���Һ����������
* �����̻ʡ����̣���ʲô�����ں������Ǹ�Ƣ��������ˡ�
*/
20 #define MINUTE 60 // 1���ӵ�������
21 #define HOUR (60*MINUTE) // 1Сʱ��������
22 #define DAY (24*HOUR) // 1���������
23 #define YEAR (365*DAY) // 1���������
24
25 /* interestingly, we assume leap-years */
/* ��Ȥ�������ǿ��ǽ������� */
// ��������Ϊ���ޣ�������ÿ���¿�ʼʱ������ʱ�䡣
26 static int month[12] = {
27 0,
28 DAY*(31),
29 DAY*(31+29),
30 DAY*(31+29+31),
31 DAY*(31+29+31+30),
32 DAY*(31+29+31+30+31),
33 DAY*(31+29+31+30+31+30),
34 DAY*(31+29+31+30+31+30+31),
35 DAY*(31+29+31+30+31+30+31+31),
36 DAY*(31+29+31+30+31+30+31+31+30),
37 DAY*(31+29+31+30+31+30+31+31+30+31),
38 DAY*(31+29+31+30+31+30+31+31+30+31+30)
39 };
40
// �ú��������1970��1��1��0ʱ�������վ�������������Ϊ����ʱ�䡣
// ����tm�и��ֶ��Ѿ���init/main.c�б���ֵ����Ϣȡ��CMOS��
41 long kernel_mktime(struct tm * tm)
42 {
43 long res;
44 int year;
45
// ���ȼ���70�굽���ھ�������������Ϊ��2λ��ʾ��ʽ�����Ի���2000�����⡣���ǿ���
// ������ǰ������һ����������������⣺if (tm->tm_year<70) tm->tm_year += 100;
// ����UNIX�����y�Ǵ�1970�����𡣵�1972�����һ�����꣬��˹�3�꣨71��72��73��
// ���ǵ�1�����꣬������1970�꿪ʼ�����������㷽����Ӧ����Ϊ1 + (y - 3)/4����Ϊ
// (y + 1)/4��res = ��Щ�꾭��������ʱ�� + ÿ������ʱ��1�������ʱ�� + ���굽����ʱ
// �����������⣬month[]�������Ѿ���2�·ݵ������а�����������ʱ����������2�·�����
// ������ 1�졣��ˣ������겻�����겢�ҵ�ǰ�·ݴ��� 2�·ݵĻ������Ǿ�Ҫ��ȥ���졣��
// Ϊ��70�꿪ʼ�������Ե�����������жϷ����� (y + 2) �ܱ�4�����������ܳ���������
// �����Ͳ������ꡣ
46 year = tm->tm_year - 70;
47 /* magic offsets (y+1) needed to get leapyears right.*/
/* Ϊ�˻����ȷ����������������Ҫ����һ��ħ��ֵ(y+1) */
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 */
/* �Լ�(y+2)�����(y+2)�������꣬��ô���Ǿͱ�����е���(��ȥһ�������ʱ��)��*/
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; // �ټ���1Сʱ�ڹ�ȥ�ķ�����������ʱ�䡣
56 res += tm->tm_sec; // �ټ���1�������ѹ���������
57 return res; // �����ڴ�1970����������������ʱ�䡣
58 }
59