2012-11-11 00:06:01 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* libs/libc/time/lib_time.c
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +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
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +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.
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2015-02-16 06:38:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-11-11 00:06:01 +08:00
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: time
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the current calendar time as a time_t object. The function returns
|
|
|
|
* this value, and if the argument is not a null pointer, the value is also
|
|
|
|
* set to the object pointed by tloc.
|
|
|
|
*
|
2015-02-13 01:10:46 +08:00
|
|
|
* Note that this function is just a thin wrapper around clock_gettime()
|
|
|
|
* and is provided for compatibility. clock_gettime() is the preferred way
|
2012-11-11 00:06:01 +08:00
|
|
|
* to obtain system time.
|
|
|
|
*
|
2018-03-13 22:07:03 +08:00
|
|
|
* Input Parameters:
|
2012-11-11 00:06:01 +08:00
|
|
|
* Pointer to an object of type time_t, where the time value is stored.
|
2015-02-13 01:10:46 +08:00
|
|
|
* Alternatively, this parameter can be a null pointer, in which case the
|
2012-11-11 00:06:01 +08:00
|
|
|
* parameter is not used, but a time_t object is still returned by the
|
|
|
|
* function.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2012-11-11 00:06:01 +08:00
|
|
|
* The current calendar time as a time_t object. If the argument is not
|
|
|
|
* a null pointer, the return value is the same as the one stored in the
|
|
|
|
* location pointed by the argument.
|
|
|
|
*
|
|
|
|
* If the function could not retrieve the calendar time, it returns a -1
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
time_t time(time_t *tloc)
|
|
|
|
{
|
2015-02-13 01:10:46 +08:00
|
|
|
struct timespec ts;
|
2012-11-11 00:06:01 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the current time from the system */
|
|
|
|
|
2015-02-13 01:10:46 +08:00
|
|
|
ret = clock_gettime(CLOCK_REALTIME, &ts);
|
2012-11-11 00:06:01 +08:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Return the seconds since the epoch */
|
|
|
|
|
|
|
|
if (tloc)
|
|
|
|
{
|
2015-02-13 01:10:46 +08:00
|
|
|
*tloc = ts.tv_sec;
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 01:10:46 +08:00
|
|
|
return ts.tv_sec;
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (time_t)ERROR;
|
|
|
|
}
|