Skip to content

Commit

Permalink
libc: common: simple implementation for localtime() & localtime_r()
Browse files Browse the repository at this point in the history
This implementation simply wraps around the gmtime() &
gmtime_r() functions, the results are always expressed as UTC.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
  • Loading branch information
ycsin committed Jun 21, 2024
1 parent a7eaa13 commit b6f768f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ _POSIX_THREAD_SAFE_FUNCTIONS
getpwnam_r(),
getpwuid_r(),
gmtime_r(), yes
localtime_r(),
localtime_r(), yes (UTC timezone only)
putc_unlocked(),
putchar_unlocked(),
rand_r(), yes
Expand Down
1 change: 1 addition & 0 deletions lib/libc/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ zephyr_library_property(ALLOW_EMPTY TRUE)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_ABORT source/stdlib/abort.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_ASCTIME source/time/asctime.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_GMTIME_R source/time/gmtime_r.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_LOCALTIME_R_UTC source/time/localtime_r_utc.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_TIME source/time/time.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_MALLOC source/stdlib/malloc.c)
zephyr_library_sources_ifdef(CONFIG_COMMON_LIBC_STRNLEN source/string/strnlen.c)
Expand Down
8 changes: 8 additions & 0 deletions lib/libc/common/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ config COMMON_LIBC_GMTIME_R
help
common implementation of gmtime_r().

config COMMON_LIBC_LOCALTIME_R_UTC
bool
select COMMON_LIBC_GMTIME_R
help
Simple implementation of localtime() & localtime_r().
This option just wraps around the gmtime(), the result is always expressed as
Coordinated Universal Time (UTC).

config COMMON_LIBC_TIME
bool
help
Expand Down
17 changes: 17 additions & 0 deletions lib/libc/common/source/time/localtime_r_utc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <time.h>

struct tm *localtime_r(const time_t *timer, struct tm *result)
{
return gmtime_r(timer, result);
}

struct tm *localtime(const time_t *timer)
{
return gmtime(timer);
}
1 change: 1 addition & 0 deletions lib/libc/minimal/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ config MINIMAL_LIBC_TIME
select COMMON_LIBC_TIME if POSIX_TIMERS
select COMMON_LIBC_GMTIME_R
select COMMON_LIBC_ASCTIME
select COMMON_LIBC_LOCALTIME_R_UTC
default y
help
Enable time() and gmtime_r() for the minimal libc.
Expand Down
2 changes: 2 additions & 0 deletions lib/libc/minimal/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *ZRESTRICT timep,
struct tm *ZRESTRICT result);
char *asctime(const struct tm *timeptr);
struct tm *localtime(const time_t *timer);
struct tm *localtime_r(const time_t *ZRESTRICT timer, struct tm *ZRESTRICT result);

#if defined(CONFIG_COMMON_LIBC_ASCTIME_R) || defined(__DOXYGEN__)
char *asctime_r(const struct tm *ZRESTRICT tp, char *ZRESTRICT buf);
Expand Down
2 changes: 2 additions & 0 deletions lib/posix/options/Kconfig.pthread
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ config POSIX_THREAD_PRIO_PROTECT
config POSIX_THREAD_SAFE_FUNCTIONS
bool "POSIX thread-safe functions"
select COMMON_LIBC_ASCTIME_R
select COMMON_LIBC_GMTIME_R
select COMMON_LIBC_LOCALTIME_R_UTC
help
Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(),
flockfile(), ftrylockfile(), funlockfile(), getc_unlocked(), getchar_unlocked(),
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/c_lib/common/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,28 @@ ZTEST(libc_common, test_time_asctime)
}
}

/**
* @brief Test time function
*
* @see localtime(), localtime_r().
*/
ZTEST(libc_common, test_time_localtime)
{
time_t tests1 = 0;
time_t tests2 = -5;
time_t tests3 = (time_t) -214748364800;
time_t tests4 = 951868800;

struct tm tp;

zassert_not_null(localtime(&tests1), "localtime failed");
zassert_not_null(localtime(&tests2), "localtime failed");

tp.tm_wday = -5;
zassert_not_null(localtime_r(&tests3, &tp), "localtime_r failed");
zassert_not_null(localtime_r(&tests4, &tp), "localtime_r failed");
}

/**
*
* @brief Test rand function
Expand Down

0 comments on commit b6f768f

Please sign in to comment.