-
Notifications
You must be signed in to change notification settings - Fork 3
/
time.c
116 lines (95 loc) · 1.74 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <time.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <stdlib.h>
time_t time(time_t *tp) {
time_t t = _time();
if (tp) {
*tp = t;
}
return t;
}
time_t mktime(struct tm *tp) {
time_t t = _time();
struct tm tm = *tp;
if (tm.tm_year < 70) {
tm.tm_year += 100;
}
if (tm.tm_mon < 0) {
tm.tm_mon = 0;
} else if (tm.tm_mon > 11) {
tm.tm_mon = 11;
}
if (tm.tm_mday < 1) {
tm.tm_mday = 1;
} else if (tm.tm_mday > 31) {
tm.tm_mday = 31;
}
if (tm.tm_hour < 0) {
tm.tm_hour = 0;
} else if (tm.tm_hour > 23) {
tm.tm_hour = 23;
}
if (tm.tm_min < 0) {
tm.tm_min = 0;
} else if (tm.tm_min > 59) {
tm.tm_min = 59;
}
if (tm.tm_sec < 0) {
tm.tm_sec = 0;
} else if (tm.tm_sec > 59) {
tm.tm_sec = 59;
}
return t;
}
struct tm *gmtime(const time_t *tp) {
struct tm *tm = malloc(sizeof(struct tm));
time_t t = *tp;
tm->tm_sec = t % 60;
t /= 60;
tm->tm_min = t % 60;
t /= 60;
tm->tm_hour = t % 24;
t /= 24;
tm->tm_wday = (t + 4) % 7;
t /= 7;
tm->tm_mday = t + 1;
t = t / 4;
tm->tm_mon = t % 12;
t /= 12;
tm->tm_year = t - 69;
tm->tm_isdst = 0;
return tm;
}
struct tm *localtime(const time_t *tp) {
struct tm *tm = malloc(sizeof(struct tm));
time_t t = *tp;
tm->tm_sec = t % 60;
t /= 60;
tm->tm_min = t % 60;
t /= 60;
tm->tm_hour = t % 24;
t /= 24;
tm->tm_wday = (t + 4) % 7;
t /= 7;
tm->tm_mday = t + 1;
t = t / 4;
tm->tm_mon = t % 12;
t /= 12;
tm->tm_year = t - 69;
tm->tm_isdst = 0;
return tm;
}
clock_t clock() {
return (clock_t) _clock();
}
double difftime(time_t time1, time_t time2) {
if (time1 < time2) {
return (double) (time2 - time1);
} else {
return (double) (time1 - time2);
}
}
clock_t __libc_ticks_per_second() {
return (clock_t) _clock_ticks_per_second();
}