time: generic and platform-specific functions

This commit is contained in:
Lephenixnoir 2021-12-04 22:00:40 +01:00
parent 02fe82d903
commit 7f27191726
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
7 changed files with 86 additions and 7 deletions

View File

@ -48,6 +48,9 @@ set(SOURCES
src/string/memarray.c
src/string/misc.c
src/string/naive.c
# time
src/time/clock.c
src/time/functions.c
# unistd
src/unistd/files.c
# fcntl
@ -70,7 +73,7 @@ target_compile_options(fxlibctest PRIVATE
-Wall -Wextra -Os -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/src/=)
target_link_options(fxlibctest PRIVATE -Wl,-Map=map -Wl,--print-memory-usage)
target_include_directories(fxlibctest PRIVATE include/)
target_link_libraries(fxlibctest JustUI::JustUI Gint::Gint)
target_link_libraries(fxlibctest JustUI::JustUI Gint::Gint -lc Gint::Gint)
if("${FXSDK_PLATFORM_LONG}" STREQUAL fx9860G)
generate_g1a(TARGET fxlibctest OUTPUT "FxLibcT.g1a"

View File

@ -49,6 +49,10 @@ extern ft_test ft_string_strlen;
extern ft_test ft_string_naive;
extern ft_test ft_string_strerror;
/* time */
extern ft_test ft_time_clock;
extern ft_test ft_time_functions;
/* unistd */
extern ft_test ft_unistd_simple_write;
extern ft_test ft_unistd_write_odd;

View File

@ -68,7 +68,11 @@ ft_list headers_libc[] = {
&ft_string_strerror,
NULL,
}},
{ _("time.h", "<time.h>"), NULL },
{ _("time.h", "<time.h>"), (ft_test*[]){
&ft_time_clock,
&ft_time_functions,
NULL,
}},
{ _("uchar.h", "<uchar.h>"), NULL },
{ _("wchar.h", "<wchar.h>"), NULL },
{ _("wctype.h", "<wctype.h>"), NULL },

31
src/time/clock.c Normal file
View File

@ -0,0 +1,31 @@
#include <ft/test.h>
#include <ft/all-tests.h>
#include <gint/rtc.h>
#include <time.h>
static void _ft_time_clock(ft_test *t)
{
rtc_time_t rtc;
rtc_get_time(&rtc);
static char const weekday_names[8][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???",
};
int wday = (rtc.week_day >= 7) ? 7 : rtc.week_day;
ft_log(t, "RTC time: %04d-%02d-%02d (%.3s) %02d:%02d:%02d (%d t)\n",
rtc.year, rtc.month+1, rtc.month_day, weekday_names[wday],
rtc.hours, rtc.minutes, rtc.seconds, rtc.ticks);
ft_log(t, "rtc_ticks() = %ld\n", rtc_ticks());
clock_t c = clock();
ft_log(t, "clock() = %llu\n", c);
time_t ti = time(NULL);
ft_log(t, "time() = %llu\n", ti);
}
ft_test ft_time_clock = {
.name = "Obtaining time",
.function = _ft_time_clock,
};

39
src/time/functions.c Normal file
View File

@ -0,0 +1,39 @@
#include <ft/test.h>
#include <ft/all-tests.h>
#include <time.h>
static void _ft_time_functions(ft_test *t)
{
time_t t1 = 1638650741;
time_t t2 = 1573929834;
time_t t3 = 1237432983;
time_t t4 = 1839824619;
/* gmtime() uses mktime() internally, this saves us the need to build
some struct tm's manually */
struct tm tm1 = *gmtime(&t1);
struct tm tm2 = *gmtime(&t2);
struct tm tm3 = *gmtime(&t3);
struct tm tm4 = *gmtime(&t4);
ft_log(t, "%llu:\n %s", t1, asctime(&tm1));
ft_assert(t, !strcmp(asctime(&tm1), "Sat Dec 4 20:45:41 2021\n"));
ft_assert(t, mktime(&tm1) == t1);
ft_log(t, "%llu:\n %s", t2, asctime(&tm2));
ft_assert(t, !strcmp(asctime(&tm2), "Sat Nov 16 18:43:54 2019\n"));
ft_assert(t, mktime(&tm2) == t2);
ft_log(t, "%llu:\n %s", t3, asctime(&tm3));
ft_assert(t, !strcmp(asctime(&tm3), "Thu Mar 19 03:23:03 2009\n"));
ft_assert(t, mktime(&tm3) == t3);
ft_log(t, "%llu:\n %s", t4, asctime(&tm4));
ft_assert(t, !strcmp(asctime(&tm4), "Thu Apr 20 06:23:39 2028\n"));
ft_assert(t, mktime(&tm4) == t4);
}
ft_test ft_time_functions = {
.name = "All time functions",
.function = _ft_time_functions,
};

View File

@ -107,6 +107,8 @@ static int block(int rx, int y, GUNUSED int h, int px, int val, int fg)
#else
static int block(int rx, int y, int h, int px, int val, int fg)
{
if(!px) return 0;
drect(rx, y, rx+px-1, y+h-1, fg);
dprint_opt(rx+px/2, y+2, C_BLACK, C_NONE, DTEXT_CENTER, DTEXT_TOP,
"%d", val);

View File

@ -111,7 +111,7 @@ static void flog_poly_render(void *l0, int x, int y)
{
#ifdef FXCG50
if(current_line >= l->top && current_line < l->top + l->visible)
dprint_opt(x+22, current_y, C_BLACK, C_NONE,
dprint_opt(x+22, current_y, C_RGB(16,16,16), C_NONE,
DTEXT_RIGHT, DTEXT_TOP, "%d", current_line+1);
#endif
@ -131,10 +131,6 @@ static void flog_poly_render(void *l0, int x, int y)
}
if(current_line >= l->top && current_line < l->top + l->visible) {
#ifdef FXCG50
int previous_y = current_y;
dline(x+26, previous_y, x+26, current_y-1, C_BLACK);
#endif
current_y += l->font->line_height + l->line_spacing;
}
current_line++;