Skip to content

Commit

Permalink
fix #469
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Jan 17, 2024
1 parent 9cde264 commit 6b98f49
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 67 deletions.
24 changes: 13 additions & 11 deletions src/libc/gmtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#define SECS_PER_YEAR (365UL * SECS_PER_DAY)
#define SECS_PER_LEAP (SECS_PER_YEAR + SECS_PER_DAY)

extern bool __isleap(int year);

static bool istmleap(int year)
static bool istmleap(unsigned int year)
{
year += 1900;

return __isleap(year);
if (year % 100 == 0)
{
return year % 400 == 0;
}

return year % 4 == 0;
}

struct tm *gmtime(const time_t *tp)
Expand All @@ -28,16 +31,16 @@ struct tm *gmtime(const time_t *tp)
time_t secs_this_year;
time_t t = *tp;

tm2.tm_sec = 0;
tm2.tm_min = 0;
tm2.tm_sec = 0;
tm2.tm_min = 0;
tm2.tm_hour = 0;
tm2.tm_mday = 1;
tm2.tm_mon = 0;
tm2.tm_mon = 0;
tm2.tm_year = 70;
tm2.tm_wday = (t / SECS_PER_DAY + 4 ) % 7;
tm2.tm_wday = (t / SECS_PER_DAY + 4) % 7;
tm2.tm_isdst = -1;

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

tm2.tm_yday = t / SECS_PER_DAY;

dpm[1] = 28;
if (istmleap(tm2.tm_year))
{
dpm[1] = 29;
Expand All @@ -60,8 +64,6 @@ struct tm *gmtime(const time_t *tp)
t -= dpm[tm2.tm_mon++] * SECS_PER_DAY;
}

dpm[1] = 28;

while (t >= SECS_PER_DAY)
{
t -= SECS_PER_DAY;
Expand Down
12 changes: 0 additions & 12 deletions src/libc/isleap.c

This file was deleted.

54 changes: 13 additions & 41 deletions src/libc/mktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,20 @@
#include <time.h>
#include <stdint.h>

#define SECS_PER_DAY 86400UL
#define SECS_PER_HOUR 3600UL
#define SECS_PER_MIN 60UL
#define DAYS_PER_YEAR 365UL

extern bool __isleap(int year);
#define SECS_PER_DAY 86400u
#define SECS_PER_HOUR 3600u
#define SECS_PER_MIN 60u

time_t mktime(struct tm *tp)
{
static const unsigned int dpmt[] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
unsigned int i;
time_t days;

if (tp->tm_year < (1970 - 1900))
{
return -1L;
}

days = (tp->tm_year - (1970 - 1900)) * DAYS_PER_YEAR;

for (i = 1970; i < (unsigned int)tp->tm_year + 1900; ++i)
{
if (__isleap(i))
{
days++;
}
}

days += dpmt[tp->tm_mon];
if (__isleap(tp->tm_year))
{
days++;
}

days += tp->tm_mday - 1;

days *= SECS_PER_DAY;

days += (tp->tm_hour * SECS_PER_HOUR) + (tp->tm_min * SECS_PER_MIN) + tp->tm_sec;

return days;
static const int yday[] = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333 };

return
tp->tm_sec +
tp->tm_min * SECS_PER_MIN +
tp->tm_hour * SECS_PER_HOUR +
(yday[tp->tm_mon] + tp->tm_mday) * SECS_PER_DAY +
(tp->tm_year - 70) * 31536000 + ((tp->tm_year - 69) / 4) * SECS_PER_DAY -
((tp->tm_year - 1) / 100) * SECS_PER_DAY + ((tp->tm_year + 299) / 400) * SECS_PER_DAY;
}

4 changes: 1 addition & 3 deletions src/libc/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ time_t time(time_t *timer)
boot_GetDate(&day, &month, &year);
boot_GetTime(&sec, &min, &hrs);

month--;

tm2.tm_sec = sec;
tm2.tm_min = min;
tm2.tm_hour = hrs;
tm2.tm_mday = day;
tm2.tm_mon = month;
tm2.tm_mon = month - 1;
tm2.tm_year = (unsigned int)year - 1900;

res = mktime(&tm2);
Expand Down

0 comments on commit 6b98f49

Please sign in to comment.