From 952af7e7f9a0cc7fd511ae84440a99b6add3ec9f Mon Sep 17 00:00:00 2001 From: Max Wash Date: Tue, 10 May 2022 18:25:11 +0100 Subject: [PATCH] Added time() and clock() --- CMakeLists.txt | 2 +- photon/libc/include/time.h | 24 ++++++++++++++++++++++++ photon/libc/sys/horizon/__time.h | 9 +++++++++ photon/libc/sys/horizon/sys/_time.h | 6 ++++++ photon/libc/sys/horizon/time.c | 19 +++++++++++++++++++ photon/libc/time/clock.c | 7 +++++++ photon/libc/time/gmtime.c | 0 photon/libc/time/localtime.c | 0 photon/libc/time/mktime.c | 0 photon/libc/time/strftime.c | 0 photon/libc/time/time.c | 12 ++++++++++++ 11 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 photon/libc/sys/horizon/__time.h create mode 100644 photon/libc/sys/horizon/sys/_time.h create mode 100644 photon/libc/sys/horizon/time.c create mode 100644 photon/libc/time/clock.c create mode 100644 photon/libc/time/gmtime.c create mode 100644 photon/libc/time/localtime.c create mode 100644 photon/libc/time/mktime.c create mode 100644 photon/libc/time/strftime.c create mode 100644 photon/libc/time/time.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f639a5..6eacaaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ set(CMAKE_EXE_LINKER_FLAGS set(malloc_impl dlmalloc) 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}") diff --git a/photon/libc/include/time.h b/photon/libc/include/time.h index c0aa0b3..69241f4 100644 --- a/photon/libc/include/time.h +++ b/photon/libc/include/time.h @@ -1,11 +1,35 @@ #ifndef TIME_H_ #define TIME_H_ +#include #include +#include + +#define CLOCKS_PER_SEC __SYS_CLOCKS_PER_SEC struct timespec { time_t tv_sec; 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 diff --git a/photon/libc/sys/horizon/__time.h b/photon/libc/sys/horizon/__time.h new file mode 100644 index 0000000..48fe832 --- /dev/null +++ b/photon/libc/sys/horizon/__time.h @@ -0,0 +1,9 @@ +#ifndef SYS_HORIZON___TIME_H_ +#define SYS_HORIZON___TIME_H_ + +#include + +extern clock_t __sys_clock(void); +extern time_t __sys_time(void); + +#endif diff --git a/photon/libc/sys/horizon/sys/_time.h b/photon/libc/sys/horizon/sys/_time.h new file mode 100644 index 0000000..293b20a --- /dev/null +++ b/photon/libc/sys/horizon/sys/_time.h @@ -0,0 +1,6 @@ +#ifndef SYS_HORIZON___TIME_H_ +#define SYS_HORIZON___TIME_H_ + +#define __SYS_CLOCKS_PER_SEC 1000000000 + +#endif diff --git a/photon/libc/sys/horizon/time.c b/photon/libc/sys/horizon/time.c new file mode 100644 index 0000000..984b367 --- /dev/null +++ b/photon/libc/sys/horizon/time.c @@ -0,0 +1,19 @@ +#include +#include +#include + +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; +} diff --git a/photon/libc/time/clock.c b/photon/libc/time/clock.c new file mode 100644 index 0000000..2e95bb6 --- /dev/null +++ b/photon/libc/time/clock.c @@ -0,0 +1,7 @@ +#include +#include <__time.h> + +clock_t clock(void) +{ + return __sys_clock(); +} diff --git a/photon/libc/time/gmtime.c b/photon/libc/time/gmtime.c new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/time/localtime.c b/photon/libc/time/localtime.c new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/time/mktime.c b/photon/libc/time/mktime.c new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/time/strftime.c b/photon/libc/time/strftime.c new file mode 100644 index 0000000..e69de29 diff --git a/photon/libc/time/time.c b/photon/libc/time/time.c new file mode 100644 index 0000000..4a38ab2 --- /dev/null +++ b/photon/libc/time/time.c @@ -0,0 +1,12 @@ +#include +#include <__time.h> + +time_t time(time_t *p) +{ + time_t out = __sys_time(); + if (p) { + *p = out; + } + + return out; +}