summaryrefslogtreecommitdiffstats
path: root/src/datetime.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datetime.c')
-rw-r--r--src/datetime.c319
1 files changed, 167 insertions, 152 deletions
diff --git a/src/datetime.c b/src/datetime.c
index 079266b..72f0219 100644
--- a/src/datetime.c
+++ b/src/datetime.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2021 P. J. McDermott
+ * Copyright (C) 2021, 2022 P. J. McDermott
*
* This file is part of @
*
@@ -18,6 +18,9 @@
*/
#define _XOPEN_SOURCE
+/* glibc requires these for timegm(): */
+#define _DEFAULT_SOURCE
+#define _BSD_SOURCE
#include <ctype.h>
#include <errno.h>
@@ -28,80 +31,33 @@
#include <string.h>
#include <time.h>
#include "datetime.h"
+#include "formats.h"
+#include "i18n.h"
-/* IMPORTANT: All of the format strings in each array must be padded with spaces
- * to be of equal lengths */
-static const char *DATETIME_DATE_FMTS_[] = {
- " ", /* */
- " %a ", /* Wed */
- " %Y-%m-%d ", /* 1969-12-31 */
- " %Y/%m/%d ", /* 1969/12/31 */
- " %m-%d-%Y ", /* 12-31-1969 */
- " %m-%d ", /* 12-31 */
- " %m/%d/%Y ", /* 12/31/1969 */
- " %m/%d ", /* 12/31 */
- " %e %b %Y ", /* 31 Dec 1969 */
- " %e %b ", /* 31 Dec */
- " %d-%b-%Y ", /* 31-Dec-1969 */
- " %d-%b ", /* 31-Dec */
- " %d/%b/%Y ", /* 31/Dec/1969 */
- " %d/%b ", /* 31/Dec */
- " %a %e %b %Y ", /* Wed 31 Dec 1969 */
- " %a %e %b ", /* Wed 31 Dec */
- " %a %d-%b-%Y ", /* Wed 31-Dec-1969 */
- " %a %d-%b ", /* Wed 31-Dec */
- " %a %d/%b/%Y ", /* Wed 31/Dec/1969 */
- " %a %d/%b ", /* Wed 31/Dec */
- " %a, %e %b %Y ", /* Wed, 31 Dec 1969 */
- " %a, %e %b ", /* Wed, 31 Dec */
- " %a, %d-%b-%Y ", /* Wed, 31-Dec-1969 */
- " %a, %d-%b ", /* Wed, 31-Dec */
- " %a, %d/%b/%Y ", /* Wed, 31/Dec/1969 */
- " %a, %d/%b ", /* Wed, 31/Dec */
- " %b %e, %Y ", /* Dec 31, 1969 */
- " %b %e ", /* Dec 31 */
- " %b-%d-%Y ", /* Dec-31-1969 */
- " %b-%d ", /* Dec-31 */
- " %b/%d/%Y ", /* Dec/31/1969 */
- " %b/%d ", /* Dec/31 */
- " %a %b %e, %Y ", /* Wed Dec 31, 1969 */
- " %a %b %e ", /* Wed Dec 31 */
- " %a %b-%d-%Y ", /* Wed Dec-31-1969 */
- " %a %b-%d ", /* Wed Dec-31 */
- " %a %b/%d/%Y ", /* Wed Dec/31/1969 */
- " %a %b/%d ", /* Wed Dec/31 */
- " %a, %b %e, %Y ", /* Wed, Dec 31, 1969 */
- " %a, %b %e ", /* Wed, Dec 31 */
- " %a, %b-%d-%Y ", /* Wed, Dec-31-1969 */
- " %a, %b-%d ", /* Wed, Dec-31 */
- " %a, %b/%d/%Y ", /* Wed, Dec/31/1969 */
- " %a, %b/%d ", /* Wed, Dec/31 */
-};
-static const char *DATETIME_TIME_FMTS_[] = {
- " %I:%M:%S %p ", /* 7:00:01 PM */
- " %H:%M:%S ", /* 19:00:01 */
- " %I:%M %p ", /* 7:00 PM */
- " %H:%M ", /* 19:00 */
- " %I%M%S %p ", /* 70001 PM */
- " %H%M%S ", /* 190001 */
- " %I%M %p ", /* 700 PM */
- " %H%M ", /* 1900 */
-};
-static const char *DATETIME_MISC_FMTS_[] = {
- /* ISO 8601: 1969-12-31 delimited by "T" */
- " %Y-%m-%dT%H:%M:%S ",
- " %Y-%m-%dT%H:%M ",
- /* ISO 8601: 19691231 delimited by "T" */
- " %Y%m%dT%H%M%S ",
- " %Y%m%dT%H%M ",
- /* 19691231 */
- " %Y%m%d %I%M%S %p ",
- " %Y%m%d %H%M%S ",
- " %Y%m%d %I%M %p ",
- " %Y%m%d %H%M ",
- /* Wed Dec 31 19:00:01 1969 */
- " %a %b %d %H:%M:%S %Y ",
-};
+static void
+_datetime_buf_cpy_p(char *dst, const char *src)
+{
+ for (; *src != '\0'; ++src, ++dst) {
+ if (*src == '^') {
+ *dst = ' ';
+ } else {
+ *dst = *src;
+ }
+ }
+ *dst = '\0';
+}
+
+static void
+_datetime_buf_cpy_f(char *dst, const char *src)
+{
+ for (; *src != '\0'; ++src, ++dst) {
+ if (*src == '^') {
+ ++src;
+ }
+ *dst = *src;
+ }
+ *dst = '\0';
+}
static void
_datetime_reset_tm(struct tm *tm)
@@ -117,58 +73,64 @@ _datetime_reset_tm(struct tm *tm)
}
static void
-_datetime_normalize(struct tm *arg_tm, time_t *arg_sec)
+_datetime_normalize(struct tm *now_tm, struct tm *arg_tm, time_t *arg_sec)
{
- time_t now_sec;
- struct tm *now_tm;
- int wday;
+ int days_in_mon[12] = {31, 28, 31, 30, 31, 30,
+ 31, 31, 30, 31, 30, 31};
+
+ if (now_tm->tm_year % 4 == 0 && (now_tm->tm_year % 100 != 0 ||
+ now_tm->tm_year % 400 == 0)) {
+ days_in_mon[1] = 29;
+ }
if (arg_tm->tm_sec == INT_MIN) {
arg_tm->tm_sec = 0;
}
if (arg_tm->tm_mday == INT_MIN && arg_tm->tm_wday == INT_MIN) {
/* No date specified; try today. */
- now_sec = time(NULL);
- now_tm = localtime(&now_sec);
arg_tm->tm_year = now_tm->tm_year;
arg_tm->tm_mon = now_tm->tm_mon;
arg_tm->tm_mday = now_tm->tm_mday;
arg_tm->tm_wday = now_tm->tm_wday;
*arg_sec = mktime(arg_tm);
- if (*arg_sec <= mktime(now_tm)) {
+ if (difftime(*arg_sec, mktime(now_tm)) <= 0) {
/* Specified time already happened today; use tomorrow.
- * Adding the number of seconds in a day is a shortcut
- * that ignores leap seconds. One better method would
- * be to increment tm_mday % days in tm_mon, etc. */
- *arg_sec += 60 * 60 * 24;
- now_tm = localtime(arg_sec);
- arg_tm->tm_year = now_tm->tm_year;
- arg_tm->tm_mon = now_tm->tm_mon;
- arg_tm->tm_mday = now_tm->tm_mday;
+ */
+ ++arg_tm->tm_mday;
+ arg_tm->tm_wday = (arg_tm->tm_wday + 1) % 7;
+ if (arg_tm->tm_mday > days_in_mon[arg_tm->tm_mon]) {
+ arg_tm->tm_mday = 1;
+ ++arg_tm->tm_mon;
+ if (arg_tm->tm_mon >= 12) {
+ arg_tm->tm_mon = 0;
+ ++arg_tm->tm_year;
+ }
+ }
*arg_sec = mktime(arg_tm);
}
} else if (arg_tm->tm_mday == INT_MIN) {
- /* Only a weekday specified; try tomorrow or next week. Uses
- * same shortcut as above. */
- wday = arg_tm->tm_wday;
- now_sec = time(NULL);
- now_tm = localtime(&now_sec);
+ /* Only a weekday specified; try tomorrow or next week. */
arg_tm->tm_year = now_tm->tm_year;
arg_tm->tm_mon = now_tm->tm_mon;
arg_tm->tm_mday = now_tm->tm_mday;
- arg_tm->tm_wday = now_tm->tm_wday;
- if (wday <= now_tm->tm_wday) {
- wday += 7;
+ if (arg_tm->tm_wday <= now_tm->tm_wday) {
+ arg_tm->tm_mday += 7;
+ }
+ arg_tm->tm_mday += arg_tm->tm_wday - now_tm->tm_wday;
+ if (arg_tm->tm_mday > days_in_mon[arg_tm->tm_mon]) {
+ arg_tm->tm_mday -= days_in_mon[arg_tm->tm_mon];
+ ++arg_tm->tm_mon;
+ if (arg_tm->tm_mon >= 12) {
+ arg_tm->tm_mon = 0;
+ ++arg_tm->tm_year;
+ }
}
*arg_sec = mktime(arg_tm);
- *arg_sec += (60 * 60 * 24) * (wday - now_tm->tm_wday);
} else if (arg_tm->tm_year == INT_MIN) {
/* No year specified; try this year. */
- now_sec = time(NULL);
- now_tm = localtime(&now_sec);
arg_tm->tm_year = now_tm->tm_year;
*arg_sec = mktime(arg_tm);
- if (*arg_sec <= mktime(now_tm)) {
+ if (difftime(*arg_sec, mktime(now_tm)) <= 0) {
/* Specified time already happened this year; use next
* year. */
++arg_tm->tm_year;
@@ -180,71 +142,118 @@ _datetime_normalize(struct tm *arg_tm, time_t *arg_sec)
}
int
-datetime_parse(const char *input, time_t *arg_sec)
+datetime_parse(struct tm *now_tm, const char *input,
+ struct tm *arg_tm, 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;
char *end;
- struct tm arg_tm;
+ time_t sec;
+ bool got;
- date_fmt_len = strlen(DATETIME_DATE_FMTS_[0]);
- time_fmt_len = strlen(DATETIME_TIME_FMTS_[0]);
- fmt_buf = calloc(date_fmt_len + time_fmt_len + 1, sizeof(*fmt_buf));
+ date_fmt_len = strlen(FORMATS_DATE[0]);
+ time_fmt_len = strlen(FORMATS_TIME[0]);
+ 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",
+ fprintf(stderr, _("Failed to allocate buffer: %s\n"),
strerror(errno));
return -1;
}
- for (d = 0; d < sizeof(DATETIME_DATE_FMTS_) /
- sizeof(DATETIME_DATE_FMTS_[0]); ++d) {
- memcpy(fmt_buf, DATETIME_DATE_FMTS_[d], date_fmt_len);
- for (t = 0; t < sizeof(DATETIME_TIME_FMTS_) /
- sizeof(DATETIME_TIME_FMTS_[0]); ++t) {
- memcpy(fmt_buf + date_fmt_len, DATETIME_TIME_FMTS_[t],
- time_fmt_len);
- _datetime_reset_tm(&arg_tm);
- end = strptime(input, fmt_buf, &arg_tm);
- if (end != NULL && *end == '\0') {
+ sec = *arg_sec; /* GCC is dumb. */
+ got = false;
+ for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]); ++t) {
+ _datetime_buf_cpy_p(fmt_buf, FORMATS_TIME[t]);
+ _datetime_reset_tm(arg_tm);
+ end = strptime(input, fmt_buf, arg_tm);
+ if (end != NULL && *end == '\0') {
+ _datetime_normalize(now_tm, arg_tm, arg_sec);
+ if (arg_tm->tm_year >= 0) {
free(fmt_buf);
- _datetime_normalize(&arg_tm, arg_sec);
return 0;
}
+ sec = *arg_sec;
+ got = true;
}
}
- for (t = 0; t < sizeof(DATETIME_TIME_FMTS_) /
- sizeof(DATETIME_TIME_FMTS_[0]); ++t) {
- memcpy(fmt_buf, DATETIME_TIME_FMTS_[t], time_fmt_len);
- for (d = 0; d < sizeof(DATETIME_DATE_FMTS_) /
- sizeof(DATETIME_DATE_FMTS_[0]); ++d) {
- memcpy(fmt_buf + time_fmt_len, DATETIME_DATE_FMTS_[d],
- date_fmt_len);
- _datetime_reset_tm(&arg_tm);
- end = strptime(input, fmt_buf, &arg_tm);
+ for (d = 0; d < sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0]); ++d) {
+ if (FORMATS_DATE[d][0] == '\0') {
+ break;
+ }
+ _datetime_buf_cpy_p(fmt_buf, FORMATS_DATE[d]);
+ for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]);
+ ++t) {
+ _datetime_buf_cpy_p(fmt_buf + date_fmt_len,
+ FORMATS_TIME[t]);
+ _datetime_reset_tm(arg_tm);
+ end = strptime(input, fmt_buf, arg_tm);
if (end != NULL && *end == '\0') {
- free(fmt_buf);
- _datetime_normalize(&arg_tm, arg_sec);
- return 0;
+ _datetime_normalize(now_tm, arg_tm, arg_sec);
+ if (arg_tm->tm_year >= 0) {
+ free(fmt_buf);
+ return 0;
+ }
+ sec = *arg_sec;
+ got = true;
+ }
+ }
+ }
+ for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]); ++t) {
+ _datetime_buf_cpy_p(fmt_buf, FORMATS_TIME[t]);
+ for (d = 0; d < sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0]);
+ ++d) {
+ if (FORMATS_DATE[d][0] == '\0') {
+ break;
+ }
+ _datetime_buf_cpy_p(fmt_buf + time_fmt_len,
+ FORMATS_DATE[d]);
+ _datetime_reset_tm(arg_tm);
+ end = strptime(input, fmt_buf, arg_tm);
+ if (end != NULL && *end == '\0') {
+ _datetime_normalize(now_tm, arg_tm, arg_sec);
+ if (arg_tm->tm_year >= 0) {
+ free(fmt_buf);
+ return 0;
+ }
+ sec = *arg_sec;
+ got = true;
}
}
}
- for (d = 0; d < sizeof(DATETIME_MISC_FMTS_) /
- sizeof(DATETIME_MISC_FMTS_[0]) ; ++d) {
- _datetime_reset_tm(&arg_tm);
- end = strptime(input, DATETIME_MISC_FMTS_[d], &arg_tm);
+ for (d = 0; d < sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0]); ++d) {
+ _datetime_buf_cpy_p(fmt_buf, FORMATS_MISC[d]);
+ _datetime_reset_tm(arg_tm);
+ end = strptime(input, fmt_buf, arg_tm);
if (end != NULL && *end == '\0') {
- free(fmt_buf);
- _datetime_normalize(&arg_tm, arg_sec);
- return 0;
+ _datetime_normalize(now_tm, arg_tm, arg_sec);
+ if (arg_tm->tm_year >= 0) {
+ free(fmt_buf);
+ return 0;
+ }
+ sec = *arg_sec;
+ got = true;
}
}
- free(fmt_buf);
- fputs("Unknown date format\n", stderr);
- return -1;
+ if (got == true) {
+ *arg_sec = sec;
+ free(fmt_buf);
+ return 0;
+ } else {
+ fputs(_("Unknown date format\n"), stderr);
+ free(fmt_buf);
+ return -1;
+ }
}
static void
@@ -283,30 +292,36 @@ _datetime_strftime(const char *fmts[], const struct tm *tm, char **out,
int resized;
if (*i == 0) {
- *buf_sz = strlen(fmts[0]);
+ *buf_sz = strlen(fmts[0]) + 1;
*buf = calloc(*buf_sz, sizeof(**buf));
if (*buf == NULL) {
- fprintf(stderr, "Failed to allocate buffer: %s\n",
+ fprintf(stderr, _("Failed to allocate buffer: %s\n"),
strerror(errno));
return -1;
}
*out = calloc(*buf_sz, sizeof(**out));
if (*out == NULL) {
- fprintf(stderr, "Failed to allocate buffer: %s\n",
+ fprintf(stderr, _("Failed to allocate buffer: %s\n"),
strerror(errno));
return -1;
}
}
+ if (fmts[*i][0] == '\0') {
+ ++*i;
+ }
+
+ _datetime_buf_cpy_f(*out, fmts[*i]);
+
resized = 0;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
- while (strftime(*buf, *buf_sz, fmts[*i], tm) == 0) {
+ while (strftime(*buf, *buf_sz, *out, tm) == 0) {
#pragma GCC diagnostic pop
++*buf_sz;
*buf = realloc(*buf, *buf_sz * sizeof(**buf));
if (*buf == NULL) {
- fprintf(stderr, "Failed to allocate buffer: %s\n",
+ fprintf(stderr, _("Failed to allocate buffer: %s\n"),
strerror(errno));
return -1;
}
@@ -315,7 +330,7 @@ _datetime_strftime(const char *fmts[], const struct tm *tm, char **out,
if (resized > 0) {
*out = realloc(*out, *buf_sz * sizeof(**out));
if (*out == NULL) {
- fprintf(stderr, "Failed to allocate buffer: %s\n",
+ fprintf(stderr, _("Failed to allocate buffer: %s\n"),
strerror(errno));
return -1;
}
@@ -331,7 +346,7 @@ int
datetime_strftime_date(const struct tm *tm, char **out, size_t *buf_sz,
char **buf, size_t *i)
{
- if (*i >= sizeof(DATETIME_DATE_FMTS_) / sizeof(DATETIME_DATE_FMTS_[0])){
+ if (*i >= sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0])) {
if (*out != NULL) {
free(*out);
}
@@ -341,14 +356,14 @@ datetime_strftime_date(const struct tm *tm, char **out, size_t *buf_sz,
return 0;
}
- return _datetime_strftime(DATETIME_DATE_FMTS_, tm, out, buf_sz, buf, i);
+ return _datetime_strftime(FORMATS_DATE, tm, out, buf_sz, buf, i);
}
int
datetime_strftime_time(const struct tm *tm, char **out, size_t *buf_sz,
char **buf, size_t *i)
{
- if (*i >= sizeof(DATETIME_TIME_FMTS_) / sizeof(DATETIME_TIME_FMTS_[0])){
+ if (*i >= sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0])) {
if (*out != NULL) {
free(*out);
}
@@ -358,14 +373,14 @@ datetime_strftime_time(const struct tm *tm, char **out, size_t *buf_sz,
return 0;
}
- return _datetime_strftime(DATETIME_TIME_FMTS_, tm, out, buf_sz, buf, i);
+ return _datetime_strftime(FORMATS_TIME, tm, out, buf_sz, buf, i);
}
int
datetime_strftime_misc(const struct tm *tm, char **out, size_t *buf_sz,
char **buf, size_t *i)
{
- if (*i >= sizeof(DATETIME_MISC_FMTS_) / sizeof(DATETIME_MISC_FMTS_[0])){
+ if (*i >= sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0])) {
if (*out != NULL) {
free(*out);
}
@@ -375,5 +390,5 @@ datetime_strftime_misc(const struct tm *tm, char **out, size_t *buf_sz,
return 0;
}
- return _datetime_strftime(DATETIME_MISC_FMTS_, tm, out, buf_sz, buf, i);
+ return _datetime_strftime(FORMATS_MISC, tm, out, buf_sz, buf, i);
}