HelenOS sources
This source file includes following definitions.
- is_leap_year
- days_in_month
- day_of_year
- floor_div
- floor_mod
- days_since_epoch
- secs_since_epoch
- day_of_week
- normalize_tm_ts
- normalize_tm_time
- wbyear_offset
- wbyear
- sun_week_number
- iso_week_number
- mon_week_number
- ts_normalize
- ts_add_diff
- ts_add
- ts_sub_diff
- ts_sub
- ts_gt
- ts_gteq
- getrealtime
- getuptime
- udelay
- mktime
- strftime
- time_utc2tm
- time_utc2str
- time_tm2str
- time_ts2tm
- time_local2tm
- time_local2str
- difftime
#include <time.h>
#include <stdbool.h>
#include <barrier.h>
#include <macros.h>
#include <errno.h>
#include <sysinfo.h>
#include <as.h>
#include <ddi.h>
#include <libc.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <loc.h>
#include <device/clock_dev.h>
#include <stats.h>
#define ASCTIME_BUF_LEN 27
#define HOURS_PER_DAY 24
#define MINS_PER_HOUR 60
#define SECS_PER_MIN 60
#define NSECS_PER_SEC 1000000000ll
#define MINS_PER_DAY (MINS_PER_HOUR * HOURS_PER_DAY)
#define SECS_PER_HOUR (SECS_PER_MIN * MINS_PER_HOUR)
#define SECS_PER_DAY (SECS_PER_HOUR * HOURS_PER_DAY)
struct {
volatile sysarg_t seconds1;
volatile sysarg_t useconds;
volatile sysarg_t seconds2;
} *ktime = NULL;
static async_sess_t *clock_conn = NULL;
clock_t clock(void)
{
static_assert(CLOCKS_PER_SEC == 1000000, "");
size_t count;
stats_cpu_t *cpu_stats = stats_get_cpus(&count);
if (!cpu_stats)
return (clock_t) -1;
if (!cpu_stats->frequency_mhz) {
free(cpu_stats);
return (clock_t) -1;
}
clock_t total_usecs = -1;
if (cpu_stats) {
stats_task_t *task_stats = stats_get_task(task_get_id());
if (task_stats) {
total_usecs = (clock_t) (task_stats->kcycles +
task_stats->ucycles) / cpu_stats->frequency_mhz;
free(task_stats);
}
free(cpu_stats);
}
return total_usecs;
}
static bool is_leap_year(time_t year)
{
year += 1900;
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
static int days_in_month(time_t year, time_t mon)
{
assert(mon >= 0);
assert(mon <= 11);
static int month_days[] = {
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
if (mon == 1) {
year += 1900;
return is_leap_year(year) ? 29 : 28;
}
return month_days[mon];
}
static int day_of_year(time_t year, time_t mon, time_t mday)
{
static int mdays[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
static int leap_mdays[] = {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335
};
return (is_leap_year(year) ? leap_mdays[mon] : mdays[mon]) + mday - 1;
}
static time_t floor_div(time_t op1, time_t op2)
{
if ((op1 >= 0) || (op1 % op2 == 0))
return op1 / op2;
return op1 / op2 - 1;
}
static time_t floor_mod(time_t op1, time_t op2)
{
time_t div = floor_div(op1, op2);
time_t result = op1 - div * op2;
assert(result >= 0);
assert(result < op2);
assert(div * op2 + result == op1);
return result;
}
static time_t days_since_epoch(time_t year, time_t mon, time_t mday)
{
return (year - 70) * 365 + floor_div(year - 69, 4) -
floor_div(year - 1, 100) + floor_div(year + 299, 400) +
day_of_year(year, mon, mday);
}
static time_t secs_since_epoch(const struct tm *tm)
{
return days_since_epoch(tm->tm_year, tm->tm_mon, tm->tm_mday) *
SECS_PER_DAY + tm->tm_hour * SECS_PER_HOUR +
tm->tm_min * SECS_PER_MIN + tm->tm_sec;
}
static time_t day_of_week(time_t year, time_t mon, time_t mday)
{
return floor_mod(days_since_epoch(year, mon, mday) + 4, 7);
}
static int normalize_tm_ts(struct tm *tm, const struct timespec *ts)
{
time_t nsec = tm->tm_nsec + ts->tv_nsec;
time_t sec = tm->tm_sec + ts->tv_sec;
time_t min = tm->tm_min;
time_t hour = tm->tm_hour;
time_t day = tm->tm_mday - 1;
time_t mon = tm->tm_mon;
time_t year = tm->tm_year;
sec += floor_div(nsec, NSECS_PER_SEC);
nsec = floor_mod(nsec, NSECS_PER_SEC);
min += floor_div(sec, SECS_PER_MIN);
sec = floor_mod(sec, SECS_PER_MIN);
hour += floor_div(min, MINS_PER_HOUR);
min = floor_mod(min, MINS_PER_HOUR);
day += floor_div(hour, HOURS_PER_DAY);
hour = floor_mod(hour, HOURS_PER_DAY);
year += floor_div(mon, 12);
mon = floor_mod(mon, 12);
year += floor_div(day, 146097) * 400;
day = floor_mod(day, 146097);
if (mon <= 1) {
while (day > 365) {
day -= is_leap_year(year) ? 366 : 365;
year++;
}
} else {
while (day > 365) {
day -= is_leap_year(year + 1) ? 366 : 365;
year++;
}
}
while (day >= days_in_month(year, mon)) {
day -= days_in_month(year, mon);
mon++;
if (mon >= 12) {
mon -= 12;
year++;
}
}
tm->tm_yday = day_of_year(year, mon, day + 1);
tm->tm_wday = day_of_week(year, mon, day + 1);
tm->tm_nsec = (int) nsec;
tm->tm_sec = (int) sec;
tm->tm_min = (int) min;
tm->tm_hour = (int) hour;
tm->tm_mday = (int) day + 1;
tm->tm_mon = (int) mon;
if (year > ((int) INT_MAX) || year < ((int) INT_MIN)) {
tm->tm_year = (year < 0) ? ((int) INT_MIN) : ((int) INT_MAX);
return -1;
}
tm->tm_year = (int) year;
return 0;
}
static int normalize_tm_time(struct tm *tm, time_t time)
{
struct timespec ts = {
.tv_sec = time,
.tv_nsec = 0
};
return normalize_tm_ts(tm, &ts);
}
static int wbyear_offset(int year)
{
int start_wday = day_of_week(year, 0, 1);
return floor_mod(4 - start_wday, 7) - 3;
}
static int wbyear(const struct tm *tm)
{
int day = tm->tm_yday - wbyear_offset(tm->tm_year);
if (day < 0) {
return tm->tm_year - 1;
}
if (day > 364 + is_leap_year(tm->tm_year)) {
return tm->tm_year + 1;
}
return tm->tm_year;
}
static int sun_week_number(const struct tm *tm)
{
int first_day = (7 - day_of_week(tm->tm_year, 0, 1)) % 7;
return (tm->tm_yday - first_day + 7) / 7;
}
static int iso_week_number(const struct tm *tm)
{
int day = tm->tm_yday - wbyear_offset(tm->tm_year);
if (day < 0) {
return 53;
}
if (day > 364 + is_leap_year(tm->tm_year)) {
return 1;
}
return (day / 7 + 1);
}
static int mon_week_number(const struct tm *tm)
{
int first_day = (1 - day_of_week(tm->tm_year, 0, 1)) % 7;
return (tm->tm_yday - first_day + 7) / 7;
}
static void ts_normalize(struct timespec *ts)
{
while (ts->tv_nsec >= NSECS_PER_SEC) {
ts->tv_sec++;
ts->tv_nsec -= NSECS_PER_SEC;
}
while (ts->tv_nsec < 0) {
ts->tv_sec--;
ts->tv_nsec += NSECS_PER_SEC;
}
}
void ts_add_diff(struct timespec *ts, nsec_t nsecs)
{
ts->tv_sec += nsecs / NSECS_PER_SEC;
ts->tv_nsec += nsecs % NSECS_PER_SEC;
ts_normalize(ts);
}
void ts_add(struct timespec *ts1, const struct timespec *ts2)
{
ts1->tv_sec += ts2->tv_sec;
ts1->tv_nsec += ts2->tv_nsec;
ts_normalize(ts1);
}
nsec_t ts_sub_diff(const struct timespec *ts1, const struct timespec *ts2)
{
return (nsec_t) (ts1->tv_nsec - ts2->tv_nsec) +
SEC2NSEC((ts1->tv_sec - ts2->tv_sec));
}
void ts_sub(struct timespec *ts1, const struct timespec *ts2)
{
ts1->tv_sec -= ts2->tv_sec;
ts1->tv_nsec -= ts2->tv_nsec;
ts_normalize(ts1);
}
bool ts_gt(const struct timespec *ts1, const struct timespec *ts2)
{
if (ts1->tv_sec > ts2->tv_sec)
return true;
if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec > ts2->tv_nsec))
return true;
return false;
}
bool ts_gteq(const struct timespec *ts1, const struct timespec *ts2)
{
if (ts1->tv_sec > ts2->tv_sec)
return true;
if ((ts1->tv_sec == ts2->tv_sec) && (ts1->tv_nsec >= ts2->tv_nsec))
return true;
return false;
}
void getrealtime(struct timespec *ts)
{
if (clock_conn == NULL) {
category_id_t cat_id;
errno_t rc = loc_category_get_id("clock", &cat_id, IPC_FLAG_BLOCKING);
if (rc != EOK)
goto fallback;
service_id_t *svc_ids;
size_t svc_cnt;
rc = loc_category_get_svcs(cat_id, &svc_ids, &svc_cnt);
if (rc != EOK)
goto fallback;
if (svc_cnt == 0)
goto fallback;
char *svc_name;
rc = loc_service_get_name(svc_ids[0], &svc_name);
free(svc_ids);
if (rc != EOK)
goto fallback;
service_id_t svc_id;
rc = loc_service_get_id(svc_name, &svc_id, 0);
free(svc_name);
if (rc != EOK)
goto fallback;
clock_conn = loc_service_connect(svc_id, INTERFACE_DDF,
IPC_FLAG_BLOCKING);
if (!clock_conn)
goto fallback;
}
struct tm time;
errno_t rc = clock_dev_time_get(clock_conn, &time);
if (rc != EOK)
goto fallback;
ts->tv_nsec = time.tm_nsec;
ts->tv_sec = mktime(&time);
return;
fallback:
getuptime(ts);
}
void getuptime(struct timespec *ts)
{
if (ktime == NULL) {
uintptr_t faddr;
errno_t rc = sysinfo_get_value("clock.faddr", &faddr);
if (rc != EOK) {
errno = rc;
goto fallback;
}
void *addr = AS_AREA_ANY;
rc = physmem_map(faddr, 1, AS_AREA_READ | AS_AREA_CACHEABLE,
&addr);
if (rc != EOK) {
as_area_destroy(addr);
errno = rc;
goto fallback;
}
ktime = addr;
}
sysarg_t s2 = ktime->seconds2;
read_barrier();
ts->tv_nsec = USEC2NSEC(ktime->useconds);
read_barrier();
sysarg_t s1 = ktime->seconds1;
if (s1 != s2) {
ts->tv_sec = max(s1, s2);
ts->tv_nsec = 0;
} else
ts->tv_sec = s1;
return;
fallback:
ts->tv_sec = 0;
ts->tv_nsec = 0;
}
time_t time(time_t *tloc)
{
struct timespec ts;
getrealtime(&ts);
if (tloc)
*tloc = ts.tv_sec;
return ts.tv_sec;
}
void udelay(sysarg_t time)
{
(void) __SYSCALL1(SYS_THREAD_UDELAY, (sysarg_t) time);
}
time_t mktime(struct tm *tm)
{
normalize_tm_time(tm, 0);
return secs_since_epoch(tm);
}
#define APPEND(...) \
{ \
consumed = snprintf(ptr, remaining, __VA_ARGS__); \
if (consumed >= remaining) \
return 0; \
\
ptr += consumed; \
remaining -= consumed; \
}
#define RECURSE(fmt) \
{ \
consumed = strftime(ptr, remaining, fmt, tm); \
if (consumed == 0) \
return 0; \
\
ptr += consumed; \
remaining -= consumed; \
}
#define TO_12H(hour) \
(((hour) > 12) ? ((hour) - 12) : \
(((hour) == 0) ? 12 : (hour)))
size_t strftime(char *restrict s, size_t maxsize,
const char *restrict format, const struct tm *restrict tm)
{
assert(s != NULL);
assert(format != NULL);
assert(tm != NULL);
static const char *wday_abbr[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
static const char *mon_abbr[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const char *mon[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
if (maxsize < 1)
return 0;
char *ptr = s;
size_t consumed;
size_t remaining = maxsize;
while (*format != '\0') {
if (*format != '%') {
APPEND("%c", *format);
format++;
continue;
}
format++;
if ((*format == '0') || (*format == '+')) {
format++;
}
while (isdigit(*format)) {
format++;
}
if ((*format == 'O') || (*format == 'E')) {
format++;
}
switch (*format) {
case 'a':
APPEND("%s", wday_abbr[tm->tm_wday]);
break;
case 'A':
APPEND("%s", wday[tm->tm_wday]);
break;
case 'b':
APPEND("%s", mon_abbr[tm->tm_mon]);
break;
case 'B':
APPEND("%s", mon[tm->tm_mon]);
break;
case 'c':
RECURSE("%Y-%m-%d %H:%M:%S");
break;
case 'C':
APPEND("%02d", (1900 + tm->tm_year) / 100);
break;
case 'd':
APPEND("%02d", tm->tm_mday);
break;
case 'D':
RECURSE("%m/%d/%y");
break;
case 'e':
APPEND("%2d", tm->tm_mday);
break;
case 'F':
RECURSE("%+4Y-%m-%d");
break;
case 'g':
APPEND("%02d", wbyear(tm) % 100);
break;
case 'G':
APPEND("%d", wbyear(tm));
break;
case 'h':
RECURSE("%b");
break;
case 'H':
APPEND("%02d", tm->tm_hour);
break;
case 'I':
APPEND("%02d", TO_12H(tm->tm_hour));
break;
case 'j':
APPEND("%03d", tm->tm_yday);
break;
case 'k':
APPEND("%2d", tm->tm_hour);
break;
case 'l':
APPEND("%2d", TO_12H(tm->tm_hour));
break;
case 'm':
APPEND("%02d", tm->tm_mon);
break;
case 'M':
APPEND("%02d", tm->tm_min);
break;
case 'n':
APPEND("\n");
break;
case 'p':
APPEND("%s", tm->tm_hour < 12 ? "AM" : "PM");
break;
case 'P':
APPEND("%s", tm->tm_hour < 12 ? "am" : "PM");
break;
case 'r':
RECURSE("%I:%M:%S %p");
break;
case 'R':
RECURSE("%H:%M");
break;
case 's':
APPEND("%lld", secs_since_epoch(tm));
break;
case 'S':
APPEND("%02d", tm->tm_sec);
break;
case 't':
APPEND("\t");
break;
case 'T':
RECURSE("%H:%M:%S");
break;
case 'u':
APPEND("%d", (tm->tm_wday == 0) ? 7 : tm->tm_wday);
break;
case 'U':
APPEND("%02d", sun_week_number(tm));
break;
case 'V':
APPEND("%02d", iso_week_number(tm));
break;
case 'w':
APPEND("%d", tm->tm_wday);
break;
case 'W':
APPEND("%02d", mon_week_number(tm));
break;
case 'x':
RECURSE("%Y-%m-%d");
break;
case 'X':
RECURSE("%H:%M:%S");
break;
case 'y':
APPEND("%02d", tm->tm_year % 100);
break;
case 'Y':
APPEND("%d", 1900 + tm->tm_year);
break;
case 'z':
break;
case 'Z':
break;
case '%':
APPEND("%%");
break;
default:
while (*format != '%')
format--;
APPEND("%%");
break;
}
format++;
}
return maxsize - remaining;
}
errno_t time_utc2tm(const time_t time, struct tm *restrict result)
{
assert(result != NULL);
result->tm_nsec = 0;
result->tm_sec = 0;
result->tm_min = 0;
result->tm_hour = 0;
result->tm_mday = 1;
result->tm_mon = 0;
result->tm_year = 70;
if (normalize_tm_time(result, time) == -1)
return EOVERFLOW;
return EOK;
}
errno_t time_utc2str(const time_t time, char *restrict buf)
{
struct tm tm;
errno_t ret = time_utc2tm(time, &tm);
if (ret != EOK)
return ret;
time_tm2str(&tm, buf);
return EOK;
}
void time_tm2str(const struct tm *restrict timeptr, char *restrict buf)
{
assert(timeptr != NULL);
assert(buf != NULL);
static const char *wday[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char *mon[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
snprintf(buf, ASCTIME_BUF_LEN, "%s %s %2d %02d:%02d:%02d %d\n",
wday[timeptr->tm_wday],
mon[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 + timeptr->tm_year);
}
errno_t time_ts2tm(const struct timespec *ts, struct tm *restrict result)
{
result->tm_nsec = 0;
result->tm_sec = 0;
result->tm_min = 0;
result->tm_hour = 0;
result->tm_mday = 1;
result->tm_mon = 0;
result->tm_year = 70;
if (normalize_tm_ts(result, ts) == -1)
return EOVERFLOW;
return EOK;
}
errno_t time_local2tm(const time_t time, struct tm *restrict result)
{
struct timespec ts = {
.tv_sec = time,
.tv_nsec = 0
};
return time_ts2tm(&ts, result);
}
errno_t time_local2str(const time_t time, char *buf)
{
struct tm loctime;
errno_t ret = time_local2tm(time, &loctime);
if (ret != EOK)
return ret;
time_tm2str(&loctime, buf);
return EOK;
}
double difftime(time_t time1, time_t time0)
{
return (double) (time1 - time0);
}
HelenOS homepage, sources at GitHub