Skip to content

Commit 6b98f49

Browse files
fix #469
1 parent 9cde264 commit 6b98f49

File tree

4 files changed

+27
-67
lines changed

4 files changed

+27
-67
lines changed

src/libc/gmtime.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
#define SECS_PER_YEAR (365UL * SECS_PER_DAY)
1010
#define SECS_PER_LEAP (SECS_PER_YEAR + SECS_PER_DAY)
1111

12-
extern bool __isleap(int year);
13-
14-
static bool istmleap(int year)
12+
static bool istmleap(unsigned int year)
1513
{
1614
year += 1900;
1715

18-
return __isleap(year);
16+
if (year % 100 == 0)
17+
{
18+
return year % 400 == 0;
19+
}
20+
21+
return year % 4 == 0;
1922
}
2023

2124
struct tm *gmtime(const time_t *tp)
@@ -28,16 +31,16 @@ struct tm *gmtime(const time_t *tp)
2831
time_t secs_this_year;
2932
time_t t = *tp;
3033

31-
tm2.tm_sec = 0;
32-
tm2.tm_min = 0;
34+
tm2.tm_sec = 0;
35+
tm2.tm_min = 0;
3336
tm2.tm_hour = 0;
3437
tm2.tm_mday = 1;
35-
tm2.tm_mon = 0;
38+
tm2.tm_mon = 0;
3639
tm2.tm_year = 70;
37-
tm2.tm_wday = (t / SECS_PER_DAY + 4 ) % 7;
40+
tm2.tm_wday = (t / SECS_PER_DAY + 4) % 7;
3841
tm2.tm_isdst = -1;
3942

40-
while (t >= (secs_this_year = istmleap(tm2.tm_year) ? SECS_PER_LEAP : SECS_PER_YEAR ))
43+
while (t >= (secs_this_year = istmleap(tm2.tm_year) ? SECS_PER_LEAP : SECS_PER_YEAR))
4144
{
4245
t -= secs_this_year;
4346
tm2.tm_year++;
@@ -50,6 +53,7 @@ struct tm *gmtime(const time_t *tp)
5053

5154
tm2.tm_yday = t / SECS_PER_DAY;
5255

56+
dpm[1] = 28;
5357
if (istmleap(tm2.tm_year))
5458
{
5559
dpm[1] = 29;
@@ -60,8 +64,6 @@ struct tm *gmtime(const time_t *tp)
6064
t -= dpm[tm2.tm_mon++] * SECS_PER_DAY;
6165
}
6266

63-
dpm[1] = 28;
64-
6567
while (t >= SECS_PER_DAY)
6668
{
6769
t -= SECS_PER_DAY;

src/libc/isleap.c

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/libc/mktime.c

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,20 @@
22
#include <time.h>
33
#include <stdint.h>
44

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
118

129
time_t mktime(struct tm *tp)
1310
{
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;
4920
}
21+

src/libc/time.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ time_t time(time_t *timer)
1313
boot_GetDate(&day, &month, &year);
1414
boot_GetTime(&sec, &min, &hrs);
1515

16-
month--;
17-
1816
tm2.tm_sec = sec;
1917
tm2.tm_min = min;
2018
tm2.tm_hour = hrs;
2119
tm2.tm_mday = day;
22-
tm2.tm_mon = month;
20+
tm2.tm_mon = month - 1;
2321
tm2.tm_year = (unsigned int)year - 1900;
2422

2523
res = mktime(&tm2);

0 commit comments

Comments
 (0)