2016-08-25 05:43:57 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* libs/libc/time/lib_difftime.c
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
2021-03-20 18:04:25 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
2021-03-20 18:04:25 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
2021-03-20 18:04:25 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
2016-08-25 22:00:50 +08:00
|
|
|
#include <stdint.h>
|
2016-08-25 05:43:57 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: difftime
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The difftime() function returns the number of seconds elapsed
|
2016-08-25 22:00:50 +08:00
|
|
|
* 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.
|
2016-08-25 05:43:57 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-08-25 22:00:50 +08:00
|
|
|
#ifdef CONFIG_HAVE_DOUBLE
|
2016-08-25 05:43:57 +08:00
|
|
|
double difftime(time_t time1, time_t time0)
|
|
|
|
{
|
|
|
|
return (double)time1 - (double)time0;
|
|
|
|
}
|
2016-08-25 22:00:50 +08:00
|
|
|
#else
|
|
|
|
float difftime(time_t time1, time_t time0)
|
|
|
|
{
|
2017-10-01 21:43:59 +08:00
|
|
|
if (time1 >= time0)
|
2016-08-25 22:00:50 +08:00
|
|
|
{
|
|
|
|
/* Result will be positive (even though bit 31 may be set on very large
|
|
|
|
* differences!)
|
|
|
|
*/
|
|
|
|
|
2017-10-01 05:28:04 +08:00
|
|
|
return (float)((uint32_t)(time1 - time0));
|
2016-08-25 22:00:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-21 00:41:23 +08:00
|
|
|
/* Result will be negative.
|
|
|
|
* REVISIT: Am I missing any case where bit 31
|
2016-08-25 22:00:50 +08:00
|
|
|
* might not be set?
|
|
|
|
*/
|
2016-08-25 05:43:57 +08:00
|
|
|
|
2017-10-01 05:28:04 +08:00
|
|
|
return (float)((int32_t)(time1 - time0));
|
2016-08-25 22:00:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|