summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2022-03-18 21:10:04 (EDT)
committer P. J. McDermott <pj@pehjota.net>2022-03-19 11:23:08 (EDT)
commita08a556f8f7da64696e3f735dcc008e2176741df (patch)
tree3d2af280d5f6a8294dedafa3ffb7b5acf29c6dd7
parenteb8dbc0d4cddd1161e2b02d7973985a36e2efc95 (diff)
downloadatsign-a08a556f8f7da64696e3f735dcc008e2176741df.zip
atsign-a08a556f8f7da64696e3f735dcc008e2176741df.tar.gz
atsign-a08a556f8f7da64696e3f735dcc008e2176741df.tar.bz2
main: Allow faking current time
Drop use of faketime in tests.
-rw-r--r--src/main.c76
-rwxr-xr-xtests/parse.sh6
2 files changed, 62 insertions, 20 deletions
diff --git a/src/main.c b/src/main.c
index c8e90f4..636fede 100644
--- a/src/main.c
+++ b/src/main.c
@@ -91,18 +91,46 @@ _print_help(const char *program_name)
#endif
}
-static void
-_list_formats(void)
+static bool
+_set_now(struct tm *tm, char buf[])
{
- time_t tim;
- struct tm *tm;
- char *out;
- size_t buf_sz;
- char *buf;
- size_t i;
+ char *endptr;
+
+ if (strlen(buf) != 19) {
+ return false;
+ }
+ if (buf[4] != '-' || buf[7] != '-' || buf[10] != ' ' ||
+ buf[13] != ':' || buf[16] != ':') {
+ return false;
+ }
+ buf[4] = '\0', buf[7] = '\0', buf[10] = '\0';
+ buf[13] = '\0', buf[16] = '\0';
+ tm->tm_sec = strtol(buf + 17, &endptr, 10);
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_min = strtol(buf + 14, &endptr, 10);
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_hour = strtol(buf + 11, &endptr, 10);
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_mday = strtol(buf + 8, &endptr, 10);
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_mon = strtol(buf + 5, &endptr, 10) - 1;
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_year = strtol(buf + 0, &endptr, 10) - 1900;
+ if (endptr[0] != '\0') { return false; }
+ tm->tm_wday = -1;
+ tm->tm_yday = -1;
+ tm->tm_isdst = -1;
- tim = time(NULL);
- tm = localtime(&tim);
+ return true;
+}
+
+static void
+_list_formats(const struct tm *tm)
+{
+ char *out;
+ size_t buf_sz;
+ char *buf;
+ size_t i;
puts("Time formats:");
i = 0;
@@ -182,6 +210,8 @@ int
main(int argc, char * const argv[])
{
bool dbg;
+ bool fmt;
+ struct tm now_tm;
int opt;
char *buf;
time_t now;
@@ -190,17 +220,18 @@ main(int argc, char * const argv[])
double dif;
dbg = false;
+ fmt = false;
optind = 1;
opterr = 0;
#if defined(HAVE_GETOPT_LONG) && HAVE_GETOPT_LONG
- while ((opt = getopt_long(argc, argv, "FhVd", LONGOPTS_, NULL)) > 0) {
+ while ((opt = getopt_long(argc, argv, "FhVd:", LONGOPTS_, NULL)) > 0) {
#else
- while ((opt = getopt(argc, argv, "FhVd")) > 0) {
+ while ((opt = getopt(argc, argv, "FhVd:")) > 0) {
#endif
switch (opt) {
case 'F':
- _list_formats();
- return EXIT_SUCCESS;
+ fmt = true;
+ break;
case 'h':
_print_help(argv[0]);
return EXIT_SUCCESS;
@@ -209,6 +240,10 @@ main(int argc, char * const argv[])
return EXIT_SUCCESS;
case 'd':
dbg = true;
+ if (_set_now(&now_tm, optarg) == false) {
+ return EXIT_FAILURE;
+ }
+ now_tm_p = &now_tm;
break;
default:
_print_usage(stderr, argv[0]);
@@ -225,14 +260,21 @@ main(int argc, char * const argv[])
argc -= optind;
argv += optind;
+ if (dbg == false) {
+ now = time(NULL);
+ now_tm_p = localtime(&now);
+ }
+
+ if (fmt == true) {
+ _list_formats(now_tm_p);
+ return EXIT_SUCCESS;
+ }
+
buf = _concat_args(argc, argv);
if (buf == NULL) {
return EXIT_FAILURE;
}
- now = time(NULL);
- now_tm_p = localtime(&now);
-
if (datetime_parse(now_tm_p, buf, &arg) < 0) {
free(buf);
return EXIT_FAILURE;
diff --git a/tests/parse.sh b/tests/parse.sh
index 95a8a93..08a8dea 100755
--- a/tests/parse.sh
+++ b/tests/parse.sh
@@ -21,7 +21,7 @@ set -eu
. "${TOP_SRCDIR}/tests/aux/tap-functions.sh"
-fmts="$(TZ=UTC0 faketime -f '1970-01-01 00:00:00' "${TOP_BUILDDIR}/@" -F)"
+fmts="$(TZ=UTC0 "${TOP_BUILDDIR}/@" -d '1970-01-01 00:00:00' -F)"
time_fmts="$(printf '%s\n' "${fmts}" | \
sed -n '/^Time formats:$/,/^Date formats:$/s/^ \* //p')"
date_fmts="$(printf '%s\n' "${fmts}" | \
@@ -42,8 +42,8 @@ run_test()
datetime="${1}"
shift 1
- got="$(TZ=UTC0 faketime -f '1969-12-31 23:59:30' \
- "${TOP_BUILDDIR}/@" -d "${datetime}" 2>&1)" || :
+ got="$(TZ=UTC0 "${TOP_BUILDDIR}/@" -d '1969-12-31 23:59:30' \
+ "${datetime}" 2>&1)" || :
if [ x"${got}" = x'0' ]; then
ok_ -- "${datetime}"
else