On Sat, Apr 26, 2025 at 12:45:20PM +0200, Willy Tarreau wrote:
> On Wed, Apr 23, 2025 at 05:01:41PM +0200, Thomas Weißschuh wrote:
> > This is used in various selftests and will be handy when integrating
> > those with nolibc.
> >
> > Signed-off-by: Thomas Weißschuh <[email protected]>
> > ---
> > tools/include/nolibc/time.h | 7 +++++++
> > tools/testing/selftests/nolibc/nolibc-test.c | 1 +
> > 2 files changed, 8 insertions(+)
> >
> > diff --git a/tools/include/nolibc/time.h b/tools/include/nolibc/time.h
> > index
> > 28a1549adb14e2087fa8fbdb7e9c35e1c3f22c2a..760133c574ece97165e3bba5616a387deaf07aff
> > 100644
> > --- a/tools/include/nolibc/time.h
> > +++ b/tools/include/nolibc/time.h
> > @@ -105,6 +105,13 @@ int clock_settime(clockid_t clockid, struct timespec
> > *tp)
> > }
> >
> >
> > +static __inline__
> > +double difftime(time_t time1, time_t time2)
> > +{
> > + return time1 - time2;
> > +}
>
> Thanks for making me discover difftime(). I've never heard about it, and
> seeing it return a double looks totally weird, but that's how it is indeed.
>
> In case time_t would be unsigned, this would return a large unsigned
> value. I think it could be more robust to explicitly cast the two
> inputs to long:
>
> return (long)time1 - (long)time2;
time_t is explicitly defined as "signed long" in nolibc's std.h.
If we change it at some time in the future we will have to audit any usage site
of time_t in any case.
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c
> > b/tools/testing/selftests/nolibc/nolibc-test.c
> > index
> > aed71de4b4f3dd1f183c7fc25e5a5cee466600ed..fd8bab42e75157967658690005bc9142360fc135
> > 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -1423,6 +1423,7 @@ int run_stdlib(int min, int max)
> > CASE_TEST(toupper_noop); EXPECT_EQ(1, toupper('A'),
> > 'A'); break;
> > CASE_TEST(abs); EXPECT_EQ(1, abs(-10), 10);
> > break;
> > CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10);
> > break;
> > + CASE_TEST(difftime); EXPECT_EQ(1, difftime(200.,
> > 100.), 100.); break;
>
> Then here maybe test it in reverse to make sure the types don't cause trouble:
Ack.
> CASE_TEST(difftime); EXPECT_EQ(1, difftime(100.,
> 200.), -100.); break;