2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2021-11-04 12:18:38 +01:00
|
|
|
* libs/libc/sched/clock_timespec_subtract.c
|
2015-07-26 23:03:47 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
2015-07-26 23:03:47 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-07-26 23:03:47 +02:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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.
|
2015-07-26 23:03:47 +02:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-26 23:03:47 +02:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-26 23:03:47 +02:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-26 23:03:47 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2021-11-04 12:18:38 +01:00
|
|
|
#include <nuttx/clock.h>
|
2015-07-26 23:03:47 +02:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2015-07-26 23:03:47 +02:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2015-07-26 23:03:47 +02:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2015-07-26 23:03:47 +02:00
|
|
|
* Name: clock_timespec_subtract
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Subtract timespec ts2 from to1 and return the result in ts3.
|
|
|
|
* Zero is returned if the time difference is negative.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-07-26 23:03:47 +02:00
|
|
|
* ts1 and ts2: The two timespecs to be subtracted (ts1 - ts2)
|
2020-01-20 13:32:36 +01:00
|
|
|
* ts3: The location to return the result (may be ts1 or ts2)
|
2015-07-26 23:03:47 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-07-26 23:03:47 +02:00
|
|
|
* None
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2015-07-26 23:03:47 +02:00
|
|
|
|
|
|
|
void clock_timespec_subtract(FAR const struct timespec *ts1,
|
|
|
|
FAR const struct timespec *ts2,
|
|
|
|
FAR struct timespec *ts3)
|
|
|
|
{
|
|
|
|
time_t sec;
|
|
|
|
long nsec;
|
|
|
|
|
|
|
|
if (ts1->tv_sec < ts2->tv_sec)
|
|
|
|
{
|
|
|
|
sec = 0;
|
|
|
|
nsec = 0;
|
|
|
|
}
|
|
|
|
else if (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec <= ts2->tv_nsec)
|
|
|
|
{
|
|
|
|
sec = 0;
|
|
|
|
nsec = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-03 14:28:46 +01:00
|
|
|
sec = ts1->tv_sec - ts2->tv_sec;
|
2015-07-26 23:03:47 +02:00
|
|
|
if (ts1->tv_nsec < ts2->tv_nsec)
|
|
|
|
{
|
|
|
|
nsec = (ts1->tv_nsec + NSEC_PER_SEC) - ts2->tv_nsec;
|
|
|
|
sec--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsec = ts1->tv_nsec - ts2->tv_nsec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ts3->tv_sec = sec;
|
|
|
|
ts3->tv_nsec = nsec;
|
|
|
|
}
|