From e52265294d05dd5d8de07b78889c9b89f27601ef Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 13 Apr 2022 21:13:53 -0400 Subject: Swap dates so the difference is always positive --- bin/when.y | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/when.y b/bin/when.y index a9f6d1ed..64859da7 100644 --- a/bin/when.y +++ b/bin/when.y @@ -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; } -- cgit 1.4.1