2009-07-12 01:39:33 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/time/lib_gettimeofday.c
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +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
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
2021-03-02 15:54:21 +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.
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-07-12 01:39:33 +02:00
|
|
|
|
2012-07-14 21:30:31 +02:00
|
|
|
#include <sys/time.h>
|
2009-07-12 01:39:33 +02:00
|
|
|
#include <errno.h>
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2015-02-12 18:10:46 +01:00
|
|
|
#include <nuttx/clock.h>
|
2009-07-12 01:39:33 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: gettimeofday
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-02-12 18:10:46 +01:00
|
|
|
* Get the current time
|
|
|
|
*
|
|
|
|
* Conforming to SVr4, 4.3BSD. POSIX.1-2001 describes gettimeofday().
|
|
|
|
* POSIX.1-2008 marks gettimeofday() as obsolete, recommending the use of
|
|
|
|
* clock_gettime(2) instead.
|
|
|
|
*
|
|
|
|
* NuttX implements gettimeofday() as a thin layer around clock_gettime();
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* tv - The location to return the current time
|
|
|
|
* tz - Ignored
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-02-12 18:10:46 +01:00
|
|
|
* Zero (OK) on success; -1 is returned on failure with the errno variable
|
|
|
|
* set appropriately.
|
2009-07-12 01:39:33 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-12 18:10:46 +01:00
|
|
|
int gettimeofday(FAR struct timeval *tv, FAR struct timezone *tz)
|
2009-07-12 01:39:33 +02:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-02-12 18:10:46 +01:00
|
|
|
if (!tv)
|
2009-07-12 01:39:33 +02:00
|
|
|
{
|
2014-08-29 01:00:24 +02:00
|
|
|
set_errno(EINVAL);
|
2009-07-12 01:39:33 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Let clock_gettime do most of the work */
|
|
|
|
|
|
|
|
ret = clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2021-03-02 14:33:27 +01:00
|
|
|
/* Convert the struct timespec to a struct timeval */
|
2009-07-12 01:39:33 +02:00
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
tv->tv_sec = ts.tv_sec;
|
|
|
|
tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
|
2009-07-12 01:39:33 +02:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2009-07-12 01:39:33 +02:00
|
|
|
return ret;
|
|
|
|
}
|