2018-11-17 00:11:08 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <assert.h>
|
2018-10-25 17:29:49 +02:00
|
|
|
#include <Kernel/Syscall.h>
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
time_t time(time_t* tloc)
|
|
|
|
|
{
|
2018-11-05 16:40:48 +01:00
|
|
|
struct timeval tv;
|
|
|
|
|
struct timezone tz;
|
|
|
|
|
if (gettimeofday(&tv, &tz) < 0)
|
2018-10-25 17:29:49 +02:00
|
|
|
return (time_t)-1;
|
2018-11-09 10:09:46 +01:00
|
|
|
if (tloc)
|
|
|
|
|
*tloc = tv.tv_sec;
|
2018-10-25 17:29:49 +02:00
|
|
|
return tv.tv_sec;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 16:40:48 +01:00
|
|
|
int gettimeofday(struct timeval* tv, struct timezone*)
|
2018-10-25 17:29:49 +02:00
|
|
|
{
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_gettimeofday, tv);
|
2018-10-25 17:29:49 +02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 10:38:33 +01:00
|
|
|
char* ctime(const time_t*)
|
|
|
|
|
{
|
|
|
|
|
return const_cast<char*>("ctime() not implemented");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 16:37:43 +01:00
|
|
|
inline bool is_leap_year(unsigned year)
|
2018-11-17 00:11:08 +01:00
|
|
|
{
|
2019-01-31 16:37:43 +01:00
|
|
|
return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400) == 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
tm->tm_wday = (4 + days) % 7;
|
|
|
|
|
unsigned 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)
|
|
|
|
|
++tm->tm_mday;
|
|
|
|
|
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];
|
|
|
|
|
tm->tm_mon = month;
|
|
|
|
|
tm->tm_mday += days;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tm* localtime(const time_t* t)
|
|
|
|
|
{
|
|
|
|
|
if (!t)
|
|
|
|
|
return nullptr;
|
|
|
|
|
static struct tm tm_buf;
|
|
|
|
|
time_to_tm(&tm_buf, *t);
|
|
|
|
|
return &tm_buf;
|
2018-11-17 00:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long timezone = 0;
|
|
|
|
|
|
|
|
|
|
void tzset()
|
|
|
|
|
{
|
|
|
|
|
assert(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 17:29:49 +02:00
|
|
|
}
|