summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-04-13 21:13:53 -0400
committerJune McEnroe <june@causal.agency>2022-04-13 21:13:53 -0400
commita125bd84969f26e2bb66e64a8f0ee1728d6094c0 (patch)
tree1db821096ce0e456e40b4e1a9d9a955aacb802c3
parentUpdate "Care" (diff)
downloadsrc-a125bd84969f26e2bb66e64a8f0ee1728d6094c0.tar.gz
src-a125bd84969f26e2bb66e64a8f0ee1728d6094c0.zip
Swap dates so the difference is always positive
-rw-r--r--bin/when.y12
1 files changed, 10 insertions, 2 deletions
diff --git a/bin/when.y b/bin/when.y
index ac5c7f00..64859da7 100644
--- a/bin/when.y
+++ b/bin/when.y
@@ -1,4 +1,4 @@
-/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+/* Copyright (C) 2019  June McEnroe <june@causal.agency>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -103,6 +103,15 @@ static struct tm dateSub(struct tm date, struct tm scalar) {
 }
 
 static struct tm dateDiff(struct tm a, struct tm b) {
+	time_t atime = timegm(&a), btime = timegm(&b);
+	if (atime < btime) {
+		struct tm x = a;
+		a = b;
+		b = x;
+		time_t xtime = atime;
+		atime = btime;
+		btime = xtime;
+	}
 	struct tm diff = {
 		.tm_year = a.tm_year - b.tm_year,
 		.tm_mon = a.tm_mon - b.tm_mon,
@@ -117,7 +126,6 @@ static struct tm dateDiff(struct tm a, struct tm b) {
 		diff.tm_mday = 0;
 		while (dateAdd(b, diff).tm_mday != a.tm_mday) diff.tm_mday++;
 	}
-	time_t atime = timegm(&a), btime = timegm(&b);
 	diff.tm_yday = (atime - btime) / 24 / 60 / 60;
 	return diff;
 }