|
2 | 2 | #include <time.h> |
3 | 3 | #include <stdint.h> |
4 | 4 |
|
5 | | -#define SECS_PER_DAY 86400UL |
6 | | -#define SECS_PER_HOUR 3600UL |
7 | | -#define SECS_PER_MIN 60UL |
8 | | -#define DAYS_PER_YEAR 365UL |
9 | | - |
10 | | -extern bool __isleap(int year); |
| 5 | +#define SECS_PER_DAY 86400u |
| 6 | +#define SECS_PER_HOUR 3600u |
| 7 | +#define SECS_PER_MIN 60u |
11 | 8 |
|
12 | 9 | time_t mktime(struct tm *tp) |
13 | 10 | { |
14 | | - static const unsigned int dpmt[] = |
15 | | - { |
16 | | - 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
17 | | - }; |
18 | | - unsigned int i; |
19 | | - time_t days; |
20 | | - |
21 | | - if (tp->tm_year < (1970 - 1900)) |
22 | | - { |
23 | | - return -1L; |
24 | | - } |
25 | | - |
26 | | - days = (tp->tm_year - (1970 - 1900)) * DAYS_PER_YEAR; |
27 | | - |
28 | | - for (i = 1970; i < (unsigned int)tp->tm_year + 1900; ++i) |
29 | | - { |
30 | | - if (__isleap(i)) |
31 | | - { |
32 | | - days++; |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - days += dpmt[tp->tm_mon]; |
37 | | - if (__isleap(tp->tm_year)) |
38 | | - { |
39 | | - days++; |
40 | | - } |
41 | | - |
42 | | - days += tp->tm_mday - 1; |
43 | | - |
44 | | - days *= SECS_PER_DAY; |
45 | | - |
46 | | - days += (tp->tm_hour * SECS_PER_HOUR) + (tp->tm_min * SECS_PER_MIN) + tp->tm_sec; |
47 | | - |
48 | | - return days; |
| 11 | + static const int yday[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333 }; |
| 12 | + |
| 13 | + return |
| 14 | + tp->tm_sec + |
| 15 | + tp->tm_min * SECS_PER_MIN + |
| 16 | + tp->tm_hour * SECS_PER_HOUR + |
| 17 | + (yday[tp->tm_mon] + tp->tm_mday) * SECS_PER_DAY + |
| 18 | + (tp->tm_year - 70) * 31536000 + ((tp->tm_year - 69) / 4) * SECS_PER_DAY - |
| 19 | + ((tp->tm_year - 1) / 100) * SECS_PER_DAY + ((tp->tm_year + 299) / 400) * SECS_PER_DAY; |
49 | 20 | } |
| 21 | + |
0 commit comments