zephyr/lib/libc/common/source/time/asctime.c
Yong Cong Sin fe94d4354a libc: common: implement multiple time functions
Implemented the following:
- `asctime_r()`
- `asctime()`
- `localtime()`
- `localtime_r()`
- `ctime()`
- `ctime_r()`

Specifically:
- the implementation of `localtime()` & `localtime_r()` simply
  wraps around the gmtime() & gmtime_r() functions, the
  results are always expressed as UTC.
- `ctime()` is equivalent to `asctime(localtime(clock))`, it
  inherits the limitation of `localtime()` as well, which only
  supports UTC results currently.

Added tests for these newly implemented functions.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2024-08-21 16:59:15 -04:00

57 lines
1.3 KiB
C

/*
* Copyright (c) 2024 Meta Platforms
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <zephyr/sys/util.h>
#define DATE_STRING_BUF_SZ 26U
#define DATE_WDAY_STRING_SZ 7U
#define DATE_MON_STRING_SZ 12U
#define DATE_TM_YEAR_BASE 1900
static char *asctime_impl(const struct tm *tp, char *buf)
{
static const char wday_str[DATE_WDAY_STRING_SZ][3] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
};
static const char mon_str[DATE_MON_STRING_SZ][3] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
if ((buf == NULL) || (tp == NULL) || ((unsigned int)tp->tm_wday >= DATE_WDAY_STRING_SZ) ||
((unsigned int)tp->tm_mon >= DATE_MON_STRING_SZ)) {
return NULL;
}
unsigned int n = (unsigned int)snprintf(
buf, DATE_STRING_BUF_SZ, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", wday_str[tp->tm_wday],
mon_str[tp->tm_mon], tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec,
DATE_TM_YEAR_BASE + tp->tm_year);
if (n >= DATE_STRING_BUF_SZ) {
return NULL;
}
return buf;
}
char *asctime(const struct tm *tp)
{
static char buf[DATE_STRING_BUF_SZ];
return asctime_impl(tp, buf);
}
#if defined(CONFIG_COMMON_LIBC_ASCTIME_R)
char *asctime_r(const struct tm *tp, char *buf)
{
return asctime_impl(tp, buf);
}
#endif /* CONFIG_COMMON_LIBC_ASCTIME_R */