Skip to content

Commit 79d353b

Browse files
committed
libc: minimal: implement time() API
Implement time() API by using clock_gettime(CLOCK_REALTIME, ...). Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 90d0eb7 commit 79d353b

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib/libc/minimal/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ zephyr_library_sources(
2020
source/stdout/fprintf.c
2121
source/time/gmtime.c
2222
)
23+
24+
zephyr_library_sources_ifdef(CONFIG_POSIX_CLOCK source/time/time.c)

lib/libc/minimal/include/time.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ struct tm *gmtime(const time_t *timep);
5252
struct tm *gmtime_r(const time_t *_MLIBC_RESTRICT timep,
5353
struct tm *_MLIBC_RESTRICT result);
5454

55+
time_t time(time_t *tloc);
56+
5557
#ifdef __cplusplus
5658
}
5759
#endif

lib/libc/minimal/source/time/time.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2021 Golioth, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <time.h>
8+
9+
/* clock_gettime() prototype */
10+
#include <posix/time.h>
11+
12+
time_t time(time_t *tloc)
13+
{
14+
struct timespec ts;
15+
int ret;
16+
17+
ret = clock_gettime(CLOCK_REALTIME, &ts);
18+
if (ret < 0) {
19+
/* errno is already set by clock_gettime */
20+
return (time_t) -1;
21+
}
22+
23+
if (tloc) {
24+
*tloc = ts.tv_sec;
25+
}
26+
27+
return ts.tv_sec;
28+
}

samples/net/civetweb/common/src/libc_extensions.c

-5
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ long long strtoll(const char *str, char **endptr, int base)
159159
return (long long)strtol(str, endptr, base);
160160
}
161161

162-
time_t time(time_t *t)
163-
{
164-
return 0;
165-
}
166-
167162
/*
168163
* Most of the wrappers below are copies of the wrappers in net/sockets.h,
169164
* but they are available only if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled

0 commit comments

Comments
 (0)