diff --git a/Kernel/UnixTypes.h b/Kernel/UnixTypes.h index b469d57f59..53457a3813 100644 --- a/Kernel/UnixTypes.h +++ b/Kernel/UnixTypes.h @@ -253,7 +253,7 @@ struct sigaction { // FIXME: Support 64-bit offsets! typedef signed_dword off_t; -typedef unsigned int time_t; +typedef dword time_t; struct utimbuf { time_t actime; diff --git a/Kernel/types.h b/Kernel/types.h index 2f20a750e2..f7ce3abab1 100644 --- a/Kernel/types.h +++ b/Kernel/types.h @@ -41,8 +41,6 @@ typedef word mode_t; typedef dword nlink_t; typedef dword blksize_t; typedef dword blkcnt_t; -typedef dword time_t; -typedef dword suseconds_t; struct FarPtr { dword offset { 0 }; diff --git a/LibC/time.cpp b/LibC/time.cpp index 6552c51118..cecc161f44 100644 --- a/LibC/time.cpp +++ b/LibC/time.cpp @@ -27,39 +27,53 @@ char* ctime(const time_t*) return const_cast("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)); } +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 const unsigned seconds_per_day = 60 * 60 * 24; - static const unsigned days_per_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; - unsigned days = t / seconds_per_day; - unsigned rem = t % seconds_per_day; - tm->tm_sec = rem % 60; - rem /= 60; - tm->tm_min = rem % 60; - tm->tm_hour = rem / 60; + int days = t / __seconds_per_day; + int remaining = t % __seconds_per_day; + tm->tm_sec = remaining % 60; + remaining /= 60; + tm->tm_min = remaining % 60; + tm->tm_hour = remaining / 60; tm->tm_wday = (4 + days) % 7; - unsigned year; - for (year = 1970; days >= 365 + is_leap_year(year); ++year) - days -= 365 + is_leap_year(year); + int year; + for (year = 1970; days >= 365 + __is_leap_year(year); ++year) + days -= 365 + __is_leap_year(year); tm->tm_year = year - 1900; tm->tm_yday = days; tm->tm_mday = 1; - if (is_leap_year(year) && days == 59) + if (__is_leap_year(year) && days == 59) ++tm->tm_mday; - if (is_leap_year(year) && days >= 59) + if (__is_leap_year(year) && days >= 59) --days; - unsigned month; - for (month = 0; month < 11 && days >= days_per_month[month]; ++month) - days -= days_per_month[month]; + int month; + for (month = 0; month < 11 && days >= __days_per_month[month]; ++month) + days -= __days_per_month[month]; tm->tm_mon = month; 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) { if (!t)