Added time() and clock()

This commit is contained in:
2022-05-10 18:25:11 +01:00
parent 668ca0ca5f
commit 952af7e7f9
11 changed files with 78 additions and 1 deletions

View File

@@ -27,7 +27,7 @@ set(CMAKE_EXE_LINKER_FLAGS
set(malloc_impl dlmalloc) set(malloc_impl dlmalloc)
set(photon_libc_sources "") set(photon_libc_sources "")
set(generic_dirs string stdio stdlib errno internal ctype wchar) set(generic_dirs string stdio stdlib errno internal ctype wchar time)
message(STATUS "Memory allocator: ${malloc_impl}") message(STATUS "Memory allocator: ${malloc_impl}")

View File

@@ -1,11 +1,35 @@
#ifndef TIME_H_ #ifndef TIME_H_
#define TIME_H_ #define TIME_H_
#include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/_time.h>
#define CLOCKS_PER_SEC __SYS_CLOCKS_PER_SEC
struct timespec { struct timespec {
time_t tv_sec; time_t tv_sec;
long tv_nsec; long tv_nsec;
}; };
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
extern clock_t clock(void);
extern time_t time(time_t *p);
extern struct tm *gmtime(const time_t *timer);
extern struct tm *localtime(const time_t *timer);
extern time_t mktime(struct tm *timeptr);
extern size_t strftime(char *str, size_t max, const char *format, const struct tm *timeptr);
#endif #endif

View File

@@ -0,0 +1,9 @@
#ifndef SYS_HORIZON___TIME_H_
#define SYS_HORIZON___TIME_H_
#include <time.h>
extern clock_t __sys_clock(void);
extern time_t __sys_time(void);
#endif

View File

@@ -0,0 +1,6 @@
#ifndef SYS_HORIZON___TIME_H_
#define SYS_HORIZON___TIME_H_
#define __SYS_CLOCKS_PER_SEC 1000000000
#endif

View File

@@ -0,0 +1,19 @@
#include <magenta/types.h>
#include <magenta/clock.h>
#include <time.h>
clock_t __sys_clock(void)
{
mx_time_val_t v;
mx_clock_get_time(MX_CLOCK_MONOTONIC, &v);
return (v.sec * CLOCKS_PER_SEC) + v.nsec;
}
time_t __sys_time(void)
{
mx_time_val_t v;
mx_clock_get_time(MX_CLOCK_REALTIME, &v);
return v.sec;
}

7
photon/libc/time/clock.c Normal file
View File

@@ -0,0 +1,7 @@
#include <time.h>
#include <__time.h>
clock_t clock(void)
{
return __sys_clock();
}

View File

View File

View File

View File

12
photon/libc/time/time.c Normal file
View File

@@ -0,0 +1,12 @@
#include <time.h>
#include <__time.h>
time_t time(time_t *p)
{
time_t out = __sys_time();
if (p) {
*p = out;
}
return out;
}