summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2022-03-16 20:06:49 (EDT)
committer P. J. McDermott <pj@pehjota.net>2022-03-16 21:40:43 (EDT)
commit3e666656dc9ac50dd3bf36d35d70c403dd860c9f (patch)
tree1b2c2e7588a51575a90fa8bac25c48738e5b839b
parent0aeffaf5707d8133f693d450833ec8371bcd6869 (diff)
downloadatsign-3e666656dc9ac50dd3bf36d35d70c403dd860c9f.zip
atsign-3e666656dc9ac50dd3bf36d35d70c403dd860c9f.tar.gz
atsign-3e666656dc9ac50dd3bf36d35d70c403dd860c9f.tar.bz2
main: Use difftime() instead of comparing time_t
C99 ยง 7.23.1 defines time_t as an "arithmetic [type] capable of representing times" with "implementation-defined" "range and precision of times representable". It doesn't require that time1 > time0 for time1 later than time0. See also: http://computer-programming-forum.com/47-c-language/8d7dec65bd1ddafe.htm
-rw-r--r--configure.ac5
-rw-r--r--src/main.c8
2 files changed, 7 insertions, 6 deletions
diff --git a/configure.ac b/configure.ac
index 3ff9f79..7c94fbd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -38,8 +38,9 @@ test -d "${srcdir}/.git" || CFLAGS="${save_CFLAGS}"
funcs_missing=false
AC_CHECK_FUNCS(
[\
- calloc fprintf fputs free isspace localtime mktime printf \
- realloc setvbuf sleep strerror strlen strftime strptime time
+ calloc difftime fprintf fputs free isspace localtime mktime \
+ printf realloc setvbuf sleep strerror strlen strftime strptime \
+ time
],
[],
[funcs_missing=true])
diff --git a/src/main.c b/src/main.c
index 10630e8..4abf977 100644
--- a/src/main.c
+++ b/src/main.c
@@ -172,7 +172,7 @@ main(int argc, char * const argv[])
char *buf;
time_t arg;
time_t now;
- time_t dif;
+ double dif;
optind = 1;
opterr = 0;
@@ -218,14 +218,14 @@ main(int argc, char * const argv[])
free(buf);
time(&now);
- dif = arg - now;
+ dif = difftime(arg, now);
if (dif >= 1000 * 24 * 60 * 60) {
fputs("Date too far in the future\n", stderr);
return EXIT_FAILURE;
}
setvbuf(stdout, NULL, _IONBF, 0);
- while (arg > now) {
+ while (dif > 0) {
printf("\r%03d:%02d:%02d:%02d",
(int) dif / 60 / 60 / 24,
(int) dif / 60 / 60 % 24,
@@ -233,7 +233,7 @@ main(int argc, char * const argv[])
(int) dif % 60);
sleep(1);
time(&now);
- dif = arg - now;
+ dif = difftime(arg, now);
}
printf("\r000:00:00:00\n");