diff options
Diffstat (limited to 'src/kernel/mktime.c')
-rw-r--r-- | src/kernel/mktime.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/kernel/mktime.c b/src/kernel/mktime.c new file mode 100644 index 0000000..d125ded --- /dev/null +++ b/src/kernel/mktime.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * linux/kernel/mktime.c | ||
3 | * | ||
4 | * (C) 1991 Linus Torvalds | ||
5 | */ | ||
6 | |||
7 | #include <time.h> | ||
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 | */ | ||
20 | #define MINUTE 60 | ||
21 | #define HOUR (60*MINUTE) | ||
22 | #define DAY (24*HOUR) | ||
23 | #define YEAR (365*DAY) | ||
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 | |||
41 | long kernel_mktime(struct tm * tm) | ||
42 | { | ||
43 | long res; | ||
44 | int year; | ||
45 | if (tm->tm_year >= 70) | ||
46 | year = tm->tm_year - 70; | ||
47 | else | ||
48 | year = tm->tm_year + 100 -70; /* Y2K bug fix by hellotigercn 20110803 */ | ||
49 | /* magic offsets (y+1) needed to get leapyears right.*/ | ||
50 | res = YEAR*year + DAY*((year+1)/4); | ||
51 | res += month[tm->tm_mon]; | ||
52 | /* and (y+2) here. If it wasn't a leap-year, we have to adjust */ | ||
53 | if (tm->tm_mon>1 && ((year+2)%4)) | ||
54 | res -= DAY; | ||
55 | res += DAY*(tm->tm_mday-1); | ||
56 | res += HOUR*tm->tm_hour; | ||
57 | res += MINUTE*tm->tm_min; | ||
58 | res += tm->tm_sec; | ||
59 | return res; | ||
60 | } | ||