1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 08:00:47 +00:00

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!
typedef signed_dword off_t;
typedef unsigned int time_t;
typedef dword time_t;
struct utimbuf {
time_t actime;

View File

@ -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 };

View File

@ -27,39 +27,53 @@ char* ctime(const time_t*)
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));
}
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)