time: add support for time64 libcs

Treat time_t's as entirely unique and use the POSIX API's for
converting to/from strings.

Lastly, a 64-bit integer formats as 20 digits at most in base10.
Don't need to have any 100 byte buffers to hold that.

ASTERISK-29674 #close

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Change-Id: Id7b25bdca8f92e34229f6454f6c3e500f2cd6f56
This commit is contained in:
Philip Prindeville
2022-02-13 12:06:37 -07:00
committed by Kevin Harwell
parent d1900d4a4c
commit 287a1a9126
13 changed files with 94 additions and 26 deletions

View File

@@ -169,6 +169,7 @@ sched.o: _ASTCFLAGS+=$(call get_menuselect_cflags,DEBUG_SCHEDULER DUMP_SCHEDULER
tcptls.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE) -Wno-deprecated-declarations
uuid.o: _ASTCFLAGS+=$(UUID_INCLUDE)
stasis.o: _ASTCFLAGS+=$(call get_menuselect_cflags,AO2_DEBUG)
time.o: _ASTCFLAGS+=-D_XOPEN_SOURCE=700
OBJS:=$(sort $(OBJS))

View File

@@ -25,6 +25,7 @@
#include <inttypes.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include "asterisk/time.h"
@@ -143,3 +144,31 @@ struct timeval ast_time_create_by_unit_str(unsigned long val, const char *unit)
{
return ast_time_create_by_unit(val, ast_time_str_to_unit(unit));
}
/*!
* \brief Returns a string representation of a time_t as decimal seconds
* since the epoch.
*/
int ast_time_t_to_string(time_t time, char *buf, size_t length)
{
struct tm tm;
localtime_r(&time, &tm);
return (strftime(buf, length, "%s", &tm) == 0) ? -1 : 0;
}
/*!
* \brief Returns a time_t from a string containing seconds since the epoch.
*/
time_t ast_string_to_time_t(const char *str)
{
struct tm tm = { 0, };
/* handle leading spaces */
if (strptime(str, " %s", &tm) == NULL) {
return (time_t)-1;
}
tm.tm_isdst = -1;
return mktime(&tm);
}