2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-03-05 19:30:24 +08:00
|
|
|
#include <AK/String.h>
|
|
|
|
|
#include <AK/StringBuilder.h>
|
2020-08-25 16:38:24 -04:00
|
|
|
#include <AK/Time.h>
|
2021-08-10 13:44:01 +02:00
|
|
|
#include <Kernel/API/TimePage.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
2020-03-05 19:30:24 +08:00
|
|
|
#include <stdio.h>
|
2019-12-15 21:29:26 +01:00
|
|
|
#include <string.h>
|
2019-03-27 01:31:53 +01:00
|
|
|
#include <sys/time.h>
|
2019-05-17 20:19:03 +02:00
|
|
|
#include <sys/times.h>
|
2021-02-05 12:16:30 +01:00
|
|
|
#include <syscall.h>
|
2019-06-07 11:49:03 +02:00
|
|
|
#include <time.h>
|
2021-03-29 00:35:00 +02:00
|
|
|
#include <utime.h>
|
2018-10-25 17:29:49 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
Kernel+LibC: Add adjtime(2)
Most systems (Linux, OpenBSD) adjust 0.5 ms per second, or 0.5 us per
1 ms tick. That is, the clock is sped up or slowed down by at most
0.05%. This means adjusting the clock by 1 s takes 2000 s, and the
clock an be adjusted by at most 1.8 s per hour.
FreeBSD adjusts 5 ms per second if the remaining time adjustment is
>= 1 s (0.5%) , else it adjusts by 0.5 ms as well. This allows adjusting
by (almost) 18 s per hour.
Since Serenity OS can lose more than 22 s per hour (#3429), this
picks an adjustment rate up to 1% for now. This allows us to
adjust up to 36s per hour, which should be sufficient to adjust
the clock fast enough to keep up with how much time the clock
currently loses. Once we have a fancier NTP implementation that can
adjust tick rate in addition to offset, we can think about reducing
this.
adjtime is a bit old-school and most current POSIX-y OSs instead
implement adjtimex/ntp_adjtime, but a) we have to start somewhere
b) ntp_adjtime() is a fairly gnarly API. OpenBSD's adjfreq looks
like it might provide similar functionality with a nicer API. But
before worrying about all this, it's probably a good idea to get
to a place where the kernel APIs are (barely) good enough so that
we can write an ntp service, and once we have that we should write
a way to automatically evaluate how well it keeps the time adjusted,
and only then should we add improvements ot the adjustment mechanism.
2020-11-05 16:00:51 -05:00
|
|
|
int adjtime(const struct timeval* delta, struct timeval* old_delta)
|
|
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_adjtime, delta, old_delta);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 01:31:53 +01:00
|
|
|
int gettimeofday(struct timeval* __restrict__ tv, void* __restrict__)
|
2018-10-25 17:29:49 +02:00
|
|
|
{
|
2021-08-10 12:48:37 +02:00
|
|
|
if (!tv) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct timespec ts = {};
|
|
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
TIMESPEC_TO_TIMEVAL(tv, &ts);
|
|
|
|
|
return 0;
|
2018-10-25 17:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-06 09:59:19 -04:00
|
|
|
int settimeofday(struct timeval* __restrict__ tv, void* __restrict__)
|
|
|
|
|
{
|
|
|
|
|
timespec ts;
|
|
|
|
|
TIMEVAL_TO_TIMESPEC(tv, &ts);
|
|
|
|
|
return clock_settime(CLOCK_REALTIME, &ts);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 00:35:00 +02:00
|
|
|
int utimes(const char* pathname, const struct timeval times[2])
|
|
|
|
|
{
|
|
|
|
|
if (!times) {
|
|
|
|
|
return utime(pathname, nullptr);
|
|
|
|
|
}
|
|
|
|
|
// FIXME: implement support for tv_usec in the utime (or a new) syscall
|
|
|
|
|
utimbuf buf = { times[0].tv_sec, times[1].tv_sec };
|
|
|
|
|
return utime(pathname, &buf);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 19:30:24 +08:00
|
|
|
char* ctime(const time_t* t)
|
2018-11-11 10:38:33 +01:00
|
|
|
{
|
2020-03-05 19:30:24 +08:00
|
|
|
return asctime(localtime(t));
|
2018-11-11 10:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 22:20:13 -07:00
|
|
|
char* ctime_r(const time_t* t, char* buf)
|
|
|
|
|
{
|
|
|
|
|
struct tm tm_buf;
|
|
|
|
|
return asctime_r(localtime_r(t, &tm_buf), buf);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 10:30:21 +01:00
|
|
|
static const int __seconds_per_day = 60 * 60 * 24;
|
|
|
|
|
|
2019-01-31 16:37:43 +01:00
|
|
|
static void time_to_tm(struct tm* tm, time_t t)
|
|
|
|
|
{
|
2020-08-22 22:15:00 -04:00
|
|
|
int year = 1970;
|
2020-08-25 20:17:19 -04:00
|
|
|
for (; t >= days_in_year(year) * __seconds_per_day; ++year)
|
|
|
|
|
t -= days_in_year(year) * __seconds_per_day;
|
2020-08-22 22:15:00 -04:00
|
|
|
for (; t < 0; --year)
|
2020-08-25 20:17:19 -04:00
|
|
|
t += days_in_year(year - 1) * __seconds_per_day;
|
2020-08-25 20:25:50 -04:00
|
|
|
tm->tm_year = year - 1900;
|
2020-08-22 22:15:00 -04:00
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(t >= 0);
|
2019-01-31 16:51:27 +01:00
|
|
|
int days = t / __seconds_per_day;
|
2020-08-25 20:25:50 -04:00
|
|
|
tm->tm_yday = days;
|
2019-01-31 16:51:27 +01:00
|
|
|
int remaining = t % __seconds_per_day;
|
|
|
|
|
tm->tm_sec = remaining % 60;
|
|
|
|
|
remaining /= 60;
|
|
|
|
|
tm->tm_min = remaining % 60;
|
|
|
|
|
tm->tm_hour = remaining / 60;
|
2020-08-25 20:11:12 -04:00
|
|
|
|
2019-01-31 16:51:27 +01:00
|
|
|
int month;
|
2020-08-25 20:11:12 -04:00
|
|
|
for (month = 1; month < 12 && days >= days_in_month(year, month); ++month)
|
|
|
|
|
days -= days_in_month(year, month);
|
|
|
|
|
|
|
|
|
|
tm->tm_mday = days + 1;
|
2020-08-25 20:32:32 -04:00
|
|
|
tm->tm_wday = day_of_week(year, month, tm->tm_mday);
|
|
|
|
|
tm->tm_mon = month - 1;
|
2019-01-31 16:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-21 22:17:18 -04:00
|
|
|
static time_t tm_to_time(struct tm* tm, long timezone_adjust_seconds)
|
2019-01-31 16:51:27 +01:00
|
|
|
{
|
2020-08-24 09:19:50 -04:00
|
|
|
// "The original values of the tm_wday and tm_yday components of the structure are ignored,
|
|
|
|
|
// and the original values of the other components are not restricted to the ranges described in <time.h>.
|
|
|
|
|
// [...]
|
|
|
|
|
// Upon successful completion, the values of the tm_wday and tm_yday components of the structure shall be set appropriately,
|
|
|
|
|
// and the other components are set to represent the specified time since the Epoch,
|
|
|
|
|
// but with their values forced to the ranges indicated in the <time.h> entry;
|
|
|
|
|
// the final value of tm_mday shall not be set until tm_mon and tm_year are determined."
|
|
|
|
|
|
|
|
|
|
// FIXME: Handle tm_isdst eventually.
|
|
|
|
|
|
2020-08-23 21:37:03 -04:00
|
|
|
tm->tm_year += tm->tm_mon / 12;
|
|
|
|
|
tm->tm_mon %= 12;
|
|
|
|
|
if (tm->tm_mon < 0) {
|
|
|
|
|
tm->tm_year--;
|
|
|
|
|
tm->tm_mon += 12;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-25 19:19:16 -04:00
|
|
|
tm->tm_yday = day_of_year(1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
|
2020-08-25 20:25:50 -04:00
|
|
|
time_t days_since_epoch = years_to_days_since_epoch(1900 + tm->tm_year) + tm->tm_yday;
|
|
|
|
|
auto timestamp = ((days_since_epoch * 24 + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec + timezone_adjust_seconds;
|
2020-08-24 09:19:50 -04:00
|
|
|
time_to_tm(tm, timestamp);
|
|
|
|
|
return timestamp;
|
2020-08-20 15:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time_t mktime(struct tm* tm)
|
|
|
|
|
{
|
|
|
|
|
return tm_to_time(tm, timezone);
|
2019-01-31 16:51:27 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-31 16:37:43 +01:00
|
|
|
struct tm* localtime(const time_t* t)
|
|
|
|
|
{
|
|
|
|
|
static struct tm tm_buf;
|
2020-02-11 19:36:59 +01:00
|
|
|
return localtime_r(t, &tm_buf);
|
2018-11-17 00:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 19:30:24 +08:00
|
|
|
struct tm* localtime_r(const time_t* t, struct tm* tm)
|
|
|
|
|
{
|
|
|
|
|
if (!t)
|
|
|
|
|
return nullptr;
|
|
|
|
|
time_to_tm(tm, (*t) - timezone);
|
|
|
|
|
return tm;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 15:44:10 -04:00
|
|
|
time_t timegm(struct tm* tm)
|
|
|
|
|
{
|
|
|
|
|
return tm_to_time(tm, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-23 17:24:50 +01:00
|
|
|
struct tm* gmtime(const time_t* t)
|
|
|
|
|
{
|
2020-03-05 19:30:24 +08:00
|
|
|
static struct tm tm_buf;
|
|
|
|
|
return gmtime_r(t, &tm_buf);
|
2019-02-23 17:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 23:09:37 +01:00
|
|
|
struct tm* gmtime_r(const time_t* t, struct tm* tm)
|
|
|
|
|
{
|
2020-03-05 19:30:24 +08:00
|
|
|
if (!t)
|
|
|
|
|
return nullptr;
|
|
|
|
|
time_to_tm(tm, *t);
|
|
|
|
|
return tm;
|
2020-02-19 23:09:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 19:30:24 +08:00
|
|
|
char* asctime(const struct tm* tm)
|
2019-02-24 15:19:32 +01:00
|
|
|
{
|
2020-03-08 17:00:58 +08:00
|
|
|
static char buffer[69];
|
2021-04-19 22:20:13 -07:00
|
|
|
return asctime_r(tm, buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* asctime_r(const struct tm* tm, char* buffer)
|
|
|
|
|
{
|
|
|
|
|
// Spec states buffer must be at least 26 bytes.
|
|
|
|
|
constexpr size_t assumed_len = 26;
|
2021-04-20 16:26:12 -07:00
|
|
|
size_t filled_size = strftime(buffer, assumed_len, "%a %b %e %T %Y\n", tm);
|
|
|
|
|
|
|
|
|
|
// Verify that the buffer was large enough.
|
|
|
|
|
VERIFY(filled_size != 0);
|
|
|
|
|
|
2020-03-08 17:00:58 +08:00
|
|
|
return buffer;
|
2019-02-24 15:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 19:30:24 +08:00
|
|
|
//FIXME: Some formats are not supported.
|
|
|
|
|
size_t strftime(char* destination, size_t max_size, const char* format, const struct tm* tm)
|
2019-02-23 17:24:50 +01:00
|
|
|
{
|
2020-03-08 19:50:56 +08:00
|
|
|
const char wday_short_names[7][4] = {
|
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
|
};
|
|
|
|
|
const char wday_long_names[7][10] = {
|
|
|
|
|
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
|
|
|
|
|
};
|
|
|
|
|
const char mon_short_names[12][4] = {
|
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
|
};
|
|
|
|
|
const char mon_long_names[12][10] = {
|
|
|
|
|
"January", "February", "March", "April", "May", "June",
|
2020-10-02 09:59:28 -04:00
|
|
|
"July", "August", "September", "October", "November", "December"
|
2020-03-08 19:50:56 +08:00
|
|
|
};
|
|
|
|
|
|
2020-08-23 14:43:16 +02:00
|
|
|
StringBuilder builder { max_size };
|
2020-03-05 19:30:24 +08:00
|
|
|
|
|
|
|
|
const int format_len = strlen(format);
|
|
|
|
|
for (int i = 0; i < format_len; ++i) {
|
|
|
|
|
if (format[i] != '%') {
|
|
|
|
|
builder.append(format[i]);
|
|
|
|
|
} else {
|
|
|
|
|
if (++i >= format_len)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
switch (format[i]) {
|
|
|
|
|
case 'a':
|
|
|
|
|
builder.append(wday_short_names[tm->tm_wday]);
|
|
|
|
|
break;
|
|
|
|
|
case 'A':
|
|
|
|
|
builder.append(wday_long_names[tm->tm_wday]);
|
|
|
|
|
break;
|
|
|
|
|
case 'b':
|
|
|
|
|
builder.append(mon_short_names[tm->tm_mon]);
|
|
|
|
|
break;
|
|
|
|
|
case 'B':
|
|
|
|
|
builder.append(mon_long_names[tm->tm_mon]);
|
|
|
|
|
break;
|
|
|
|
|
case 'C':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", (tm->tm_year + 1900) / 100);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'd':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_mday);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'D':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}/{:02}/{:02}", tm->tm_mon + 1, tm->tm_mday, (tm->tm_year + 1900) % 100);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'e':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:2}", tm->tm_mday);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'h':
|
|
|
|
|
builder.append(mon_short_names[tm->tm_mon]);
|
|
|
|
|
break;
|
|
|
|
|
case 'H':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_hour);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'I':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_hour % 12);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'j':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:03}", tm->tm_yday + 1);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'm':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_mon + 1);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'M':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_min);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'n':
|
|
|
|
|
builder.append('\n');
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
builder.append(tm->tm_hour < 12 ? "a.m." : "p.m.");
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}:{:02}:{:02} {}", tm->tm_hour % 12, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'R':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}:{:02}", tm->tm_hour, tm->tm_min);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'S':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", tm->tm_sec);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 't':
|
|
|
|
|
builder.append('\t');
|
|
|
|
|
break;
|
|
|
|
|
case 'T':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}:{:02}:{:02}", tm->tm_hour, tm->tm_min, tm->tm_sec);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'u':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{}", tm->tm_wday ? tm->tm_wday : 7);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'U': {
|
|
|
|
|
const int wday_of_year_beginning = (tm->tm_wday + 6 * tm->tm_yday) % 7;
|
|
|
|
|
const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'V': {
|
|
|
|
|
const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
|
|
|
|
|
int week_number = (tm->tm_yday + wday_of_year_beginning) / 7 + 1;
|
|
|
|
|
if (wday_of_year_beginning > 3) {
|
|
|
|
|
if (tm->tm_yday >= 7 - wday_of_year_beginning)
|
|
|
|
|
--week_number;
|
|
|
|
|
else {
|
2020-08-25 20:17:19 -04:00
|
|
|
const int days_of_last_year = days_in_year(tm->tm_year + 1900 - 1);
|
2020-03-05 19:30:24 +08:00
|
|
|
const int wday_of_last_year_beginning = (wday_of_year_beginning + 6 * days_of_last_year) % 7;
|
|
|
|
|
week_number = (days_of_last_year + wday_of_last_year_beginning) / 7 + 1;
|
2020-03-08 19:50:56 +08:00
|
|
|
if (wday_of_last_year_beginning > 3)
|
2020-03-05 19:30:24 +08:00
|
|
|
--week_number;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'w':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{}", tm->tm_wday);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'W': {
|
|
|
|
|
const int wday_of_year_beginning = (tm->tm_wday + 6 + 6 * tm->tm_yday) % 7;
|
|
|
|
|
const int week_number = (tm->tm_yday + wday_of_year_beginning) / 7;
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", week_number);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'y':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{:02}", (tm->tm_year + 1900) % 100);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case 'Y':
|
2021-05-07 11:54:59 +02:00
|
|
|
builder.appendff("{}", tm->tm_year + 1900);
|
2020-03-05 19:30:24 +08:00
|
|
|
break;
|
|
|
|
|
case '%':
|
|
|
|
|
builder.append('%');
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-23 14:43:16 +02:00
|
|
|
if (builder.length() + 1 > max_size)
|
2020-03-06 20:07:06 +08:00
|
|
|
return 0;
|
2020-03-05 19:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
2020-08-25 17:32:01 +03:00
|
|
|
auto str = builder.build();
|
|
|
|
|
bool fits = str.copy_characters_to_buffer(destination, max_size);
|
|
|
|
|
return fits ? str.length() : 0;
|
2019-02-23 17:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-18 18:09:30 +01:00
|
|
|
long timezone;
|
2019-02-08 02:38:21 +01:00
|
|
|
long altzone;
|
|
|
|
|
char* tzname[2];
|
|
|
|
|
int daylight;
|
2018-11-17 00:11:08 +01:00
|
|
|
|
2021-01-18 18:09:30 +01:00
|
|
|
constexpr const char* __utc = "UTC";
|
|
|
|
|
|
2018-11-17 00:11:08 +01:00
|
|
|
void tzset()
|
|
|
|
|
{
|
2021-01-18 18:09:30 +01:00
|
|
|
// FIXME: Here we pretend we are in UTC+0.
|
2020-03-05 19:30:24 +08:00
|
|
|
timezone = 0;
|
2021-01-18 18:11:44 +01:00
|
|
|
daylight = 0;
|
2021-01-18 18:09:30 +01:00
|
|
|
tzname[0] = const_cast<char*>(__utc);
|
|
|
|
|
tzname[1] = const_cast<char*>(__utc);
|
2018-11-17 00:11:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 20:19:03 +02:00
|
|
|
clock_t clock()
|
|
|
|
|
{
|
|
|
|
|
struct tms tms;
|
|
|
|
|
times(&tms);
|
|
|
|
|
return tms.tms_utime + tms.tms_stime;
|
|
|
|
|
}
|
2019-11-02 19:34:06 +01:00
|
|
|
|
2021-08-10 13:44:01 +02:00
|
|
|
static Kernel::TimePage* get_kernel_time_page()
|
|
|
|
|
{
|
|
|
|
|
static Kernel::TimePage* s_kernel_time_page;
|
|
|
|
|
// FIXME: Thread safety
|
|
|
|
|
if (!s_kernel_time_page) {
|
|
|
|
|
auto rc = syscall(SC_map_time_page);
|
|
|
|
|
if ((int)rc < 0 && (int)rc > -EMAXERRNO) {
|
|
|
|
|
errno = -(int)rc;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
s_kernel_time_page = (Kernel::TimePage*)rc;
|
|
|
|
|
}
|
|
|
|
|
return s_kernel_time_page;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 19:34:06 +01:00
|
|
|
int clock_gettime(clockid_t clock_id, struct timespec* ts)
|
|
|
|
|
{
|
2021-08-10 13:44:01 +02:00
|
|
|
if (Kernel::time_page_supports(clock_id)) {
|
|
|
|
|
if (!ts) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto* kernel_time_page = get_kernel_time_page()) {
|
|
|
|
|
u32 update_iteration;
|
|
|
|
|
do {
|
|
|
|
|
update_iteration = AK::atomic_load(&kernel_time_page->update1, AK::memory_order_acquire);
|
|
|
|
|
*ts = kernel_time_page->clocks[clock_id];
|
|
|
|
|
} while (update_iteration != AK::atomic_load(&kernel_time_page->update2, AK::memory_order_acquire));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 19:34:06 +01:00
|
|
|
int rc = syscall(SC_clock_gettime, clock_id, ts);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 01:18:21 +02:00
|
|
|
int clock_settime(clockid_t clock_id, struct timespec* ts)
|
|
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_clock_settime, clock_id, ts);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 19:34:06 +01:00
|
|
|
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep)
|
|
|
|
|
{
|
|
|
|
|
Syscall::SC_clock_nanosleep_params params { clock_id, flags, requested_sleep, remaining_sleep };
|
|
|
|
|
int rc = syscall(SC_clock_nanosleep, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 21:53:41 +02:00
|
|
|
int nanosleep(const struct timespec* requested_sleep, struct timespec* remaining_sleep)
|
|
|
|
|
{
|
|
|
|
|
return clock_nanosleep(CLOCK_REALTIME, 0, requested_sleep, remaining_sleep);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 14:08:20 +01:00
|
|
|
int clock_getres(clockid_t, struct timespec*)
|
|
|
|
|
{
|
2021-01-17 08:22:20 +01:00
|
|
|
dbgln("FIXME: Implement clock_getres()");
|
2021-01-18 18:15:31 +01:00
|
|
|
auto rc = -ENOSYS;
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-11-11 14:08:20 +01:00
|
|
|
}
|
2020-10-13 22:59:38 +11:00
|
|
|
|
|
|
|
|
double difftime(time_t t1, time_t t0)
|
|
|
|
|
{
|
|
|
|
|
return (double)(t1 - t0);
|
|
|
|
|
}
|
2018-10-25 17:29:49 +02:00
|
|
|
}
|