From 51596dc4579f2257e744828408f41462432b4aa3 Mon Sep 17 00:00:00 2001 From: Sebastien Lorquet Date: Thu, 25 Aug 2016 08:00:50 -0600 Subject: [PATCH] Updata difftime. Add a version of difftime for the case where the platform does not support type double --- include/time.h | 6 +++++- libc/time/Make.defs | 5 +---- libc/time/lib_difftime.c | 29 +++++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/include/time.h b/include/time.h index 649e4f9f39..f524357013 100644 --- a/include/time.h +++ b/include/time.h @@ -41,6 +41,7 @@ ********************************************************************************/ #include +#include #include #include @@ -103,6 +104,7 @@ /******************************************************************************** * Public Types ********************************************************************************/ + /* Scalar types */ typedef uint32_t time_t; /* Holds time in seconds */ @@ -214,8 +216,10 @@ FAR char *ctime_r(FAR const time_t *timep, FAR char *buf); time_t time(FAR time_t *timep); -#if defined(CONFIG_LIBC_DIFFTIME) +#ifdef CONFIG_HAVE_DOUBLE double difftime(time_t time1, time_t time0); +#else +float difftime(time_t time1, time_t time0); #endif int timer_create(clockid_t clockid, FAR struct sigevent *evp, diff --git a/libc/time/Make.defs b/libc/time/Make.defs index 0aeda13eef..68e6243687 100644 --- a/libc/time/Make.defs +++ b/libc/time/Make.defs @@ -37,6 +37,7 @@ CSRCS += lib_strftime.c lib_calendar2utc.c lib_daysbeforemonth.c CSRCS += lib_gettimeofday.c lib_isleapyear.c lib_settimeofday.c lib_time.c +CSRCS += lib_difftime.c ifdef CONFIG_LIBC_LOCALTIME CSRCS += lib_localtime.c lib_asctime.c lib_asctimer.c lib_ctime.c @@ -49,10 +50,6 @@ CSRCS += lib_ctimer.c endif endif -ifdef CONFIG_LIBC_DIFFTIME -CSRCS += lib_difftime.c -endif - # Add the time directory to the build DEPPATH += --dep-path time diff --git a/libc/time/lib_difftime.c b/libc/time/lib_difftime.c index 3487d118cd..ee9a62ebb2 100644 --- a/libc/time/lib_difftime.c +++ b/libc/time/lib_difftime.c @@ -40,10 +40,9 @@ #include #include +#include #include -#ifdef CONFIG_HAVE_DOUBLE - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,13 +52,35 @@ * * Description: * The difftime() function returns the number of seconds elapsed - * between time time1 and time time0, represented as a double. + * between time time1 and time time0, represented as a double or a float. + * Float is used if the platform does not support double. However, when + * using a float, some precision may be lost for big differences. * ****************************************************************************/ +#ifdef CONFIG_HAVE_DOUBLE double difftime(time_t time1, time_t time0) { return (double)time1 - (double)time0; } +#else +float difftime(time_t time1, time_t time0) +{ + if (time1 >= time2) + { + /* Result will be positive (even though bit 31 may be set on very large + * differences!) + */ -#endif /* CONFIG_HAVE_DOUBLE */ + return (float)((uint32_t)(time1 - time0)) + } + else + { + /* Result will be negative. REVISIT: Am I missing any case where bit 31 + * might not be set? + */ + + return (float)((int32_t)(time1 - time0)) + } +} +#endif