LibC: Add mktime().

This commit is contained in:
Andreas Kling 2019-01-31 16:51:27 +01:00
parent e04ba0a83c
commit 27fa09aee4
3 changed files with 32 additions and 20 deletions

View file

@ -253,7 +253,7 @@ struct sigaction {
// FIXME: Support 64-bit offsets! // FIXME: Support 64-bit offsets!
typedef signed_dword off_t; typedef signed_dword off_t;
typedef unsigned int time_t; typedef dword time_t;
struct utimbuf { struct utimbuf {
time_t actime; time_t actime;

View file

@ -41,8 +41,6 @@ typedef word mode_t;
typedef dword nlink_t; typedef dword nlink_t;
typedef dword blksize_t; typedef dword blksize_t;
typedef dword blkcnt_t; typedef dword blkcnt_t;
typedef dword time_t;
typedef dword suseconds_t;
struct FarPtr { struct FarPtr {
dword offset { 0 }; dword offset { 0 };

View file

@ -27,39 +27,53 @@ char* ctime(const time_t*)
return const_cast<char*>("ctime() not implemented"); return const_cast<char*>("ctime() not implemented");
} }
inline bool is_leap_year(unsigned year) static inline bool __is_leap_year(int year)
{ {
return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0)); return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
} }
static const int __days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static const int __seconds_per_day = 60 * 60 * 24;
static void time_to_tm(struct tm* tm, time_t t) static void time_to_tm(struct tm* tm, time_t t)
{ {
static const unsigned seconds_per_day = 60 * 60 * 24; int days = t / __seconds_per_day;
static const unsigned days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int remaining = t % __seconds_per_day;
unsigned days = t / seconds_per_day; tm->tm_sec = remaining % 60;
unsigned rem = t % seconds_per_day; remaining /= 60;
tm->tm_sec = rem % 60; tm->tm_min = remaining % 60;
rem /= 60; tm->tm_hour = remaining / 60;
tm->tm_min = rem % 60;
tm->tm_hour = rem / 60;
tm->tm_wday = (4 + days) % 7; tm->tm_wday = (4 + days) % 7;
unsigned year; int year;
for (year = 1970; days >= 365 + is_leap_year(year); ++year) for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
days -= 365 + is_leap_year(year); days -= 365 + __is_leap_year(year);
tm->tm_year = year - 1900; tm->tm_year = year - 1900;
tm->tm_yday = days; tm->tm_yday = days;
tm->tm_mday = 1; tm->tm_mday = 1;
if (is_leap_year(year) && days == 59) if (__is_leap_year(year) && days == 59)
++tm->tm_mday; ++tm->tm_mday;
if (is_leap_year(year) && days >= 59) if (__is_leap_year(year) && days >= 59)
--days; --days;
unsigned month; int month;
for (month = 0; month < 11 && days >= days_per_month[month]; ++month) for (month = 0; month < 11 && days >= __days_per_month[month]; ++month)
days -= days_per_month[month]; days -= __days_per_month[month];
tm->tm_mon = month; tm->tm_mon = month;
tm->tm_mday += days; tm->tm_mday += days;
} }
time_t mktime(struct tm* tm)
{
int days = 0;
int seconds = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
for (int year = 70; year < tm->tm_year; ++year)
days += 365 + __is_leap_year(1900 + year);
tm->tm_yday = tm->tm_mday - 1;
for (int month = 0; month < tm->tm_mon; ++month)
tm->tm_yday += __days_per_month[month];
days += tm->tm_yday;
return days * __seconds_per_day + seconds;
}
struct tm* localtime(const time_t* t) struct tm* localtime(const time_t* t)
{ {
if (!t) if (!t)