summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2022-03-22 16:37:55 (EDT)
committer P. J. McDermott <pj@pehjota.net>2022-03-22 16:37:55 (EDT)
commite96ff94936991672991da93953cad4dc0318e6e0 (patch)
treec537277b4e24aa627ed79655e6f8f9957bc9b717
parentc8b8ccfb3af8c1bfd490cf22aefa7bcafaff723a (diff)
downloadatsign-e96ff94936991672991da93953cad4dc0318e6e0.zip
atsign-e96ff94936991672991da93953cad4dc0318e6e0.tar.gz
atsign-e96ff94936991672991da93953cad4dc0318e6e0.tar.bz2
datetime: Copy time-only & misc formats to buffer
-rw-r--r--src/datetime.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/datetime.c b/src/datetime.c
index 57bae20..ffc3329 100644
--- a/src/datetime.c
+++ b/src/datetime.c
@@ -115,6 +115,8 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec)
{
int date_fmt_len;
int time_fmt_len;
+ int misc_fmt_len;
+ int fmt_len;
char *fmt_buf;
size_t d;
size_t t;
@@ -125,7 +127,12 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec)
date_fmt_len = strlen(FORMATS_DATE[0]);
time_fmt_len = strlen(FORMATS_TIME[0]);
- fmt_buf = calloc(date_fmt_len + time_fmt_len + 1, sizeof(*fmt_buf));
+ misc_fmt_len = strlen(FORMATS_MISC[0]);
+ fmt_len = date_fmt_len + time_fmt_len;
+ if (misc_fmt_len > fmt_len) {
+ fmt_len = misc_fmt_len;
+ }
+ fmt_buf = calloc(fmt_len + 1, sizeof(*fmt_buf));
if (fmt_buf == NULL) {
fprintf(stderr, "Failed to allocate buffer: %s\n",
strerror(errno));
@@ -135,8 +142,9 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec)
sec = *arg_sec; /* GCC is dumb. */
got = false;
for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]) ; ++t) {
+ memcpy(fmt_buf, FORMATS_TIME[t], time_fmt_len);
_datetime_reset_tm(&arg_tm);
- end = strptime(input, FORMATS_TIME[t], &arg_tm);
+ end = strptime(input, fmt_buf, &arg_tm);
if (end != NULL && *end == '\0') {
_datetime_normalize(now_tm, &arg_tm, arg_sec);
if (datetime_diff_epoch(*arg_sec) >= 0) {
@@ -192,8 +200,9 @@ datetime_parse(struct tm *now_tm, const char *input, time_t *arg_sec)
}
}
for (d = 0; d < sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0]) ; ++d) {
+ memcpy(fmt_buf, FORMATS_MISC[d], misc_fmt_len);
_datetime_reset_tm(&arg_tm);
- end = strptime(input, FORMATS_MISC[d], &arg_tm);
+ end = strptime(input, fmt_buf, &arg_tm);
if (end != NULL && *end == '\0') {
_datetime_normalize(now_tm, &arg_tm, arg_sec);
if (datetime_diff_epoch(*arg_sec) >= 0) {