Updata difftime. Add a version of difftime for the case where the platform does not support type double

This commit is contained in:
Sebastien Lorquet 2016-08-25 08:00:50 -06:00 committed by Gregory Nutt
parent c51b642f81
commit 51596dc457
3 changed files with 31 additions and 9 deletions

View File

@ -41,6 +41,7 @@
********************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <sys/types.h>
#include <stdint.h>
@ -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,

View File

@ -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

View File

@ -40,10 +40,9 @@
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <stdint.h>
#include <time.h>
#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