Skip site navigation (1)Skip section navigation (2)
Date:      Sat,  2 Jun 2007 22:59:31 +1000 (EST)
From:      Edwin Groothuis <edwin@mavetju.org>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   bin/113250: [patch] fix calendar -A and -B options
Message-ID:  <20070602125931.78E7A2E@k7.mavetju>
Resent-Message-ID: <200706021300.l52D0BUw095155@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         113250
>Category:       bin
>Synopsis:       [patch] fix calendar -A and -B options
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jun 02 13:00:10 GMT 2007
>Closed-Date:
>Last-Modified:
>Originator:     Edwin Groothuis
>Release:        FreeBSD 6.2-RELEASE-p4 i386
>Organization:
-
>Environment:
System: FreeBSD k7.mavetju 6.2-RELEASE-p4 FreeBSD 6.2-RELEASE-p4 #0: Thu Apr 26 17:55:55 UTC 2007 root@i386-builder.daemonology.net:/usr/obj/usr/src/sys/SMP i386

>Description:

calendar has the following two options:

    -A num  Print lines from today and the next num days (forward, future).
    -B num  Print lines from today and the previous num days (backward, past).

Currently, while using the attached calibration file, calendar says:

[/usr/src/usr.bin/calendar] root@k7>calendar -B 3 -f c.calibrate -t 04.01.2006
Jan  1  jan 1
Jan  2  jan 2
Jan  3  jan 3
Jan  4  jan 4
Jan  5  jan 5

That is one day too much

[/usr/src/usr.bin/calendar] root@k7>calendar -B 3 -f c.calibrate -t 01.01.2006
Jan  1  jan 1
Jan  2  jan 2
Jan  3  jan 3

That is not three days before 01.01.2006

[/usr/src/usr.bin/calendar] root@k7>calendar -A 3 -f c.calibrate -t 27.12.2006
Dec 27  dec 27
Dec 28  dec 28
Dec 29  dec 29
Dec 30  dec 30
Dec 31  dec 31

That is one day too much

[/usr/src/usr.bin/calendar] root@k7>calendar -A 3 -f c.calibrate -t 31.12.2006
Jan  1  jan 1
Jan  2  jan 2
Jan  3  jan 3
Jan  4  jan 4
Dec 31  dec 31

That is in the wrong order

>How-To-Repeat:

Use this calendar file:

calendar.calibration file:
01/01	jan 1
01/02	jan 2
01/03	jan 3
01/04	jan 4
01/05	jan 5
01/06	jan 6
01/07	jan 7
01/08	jan 8
01/09	jan 9
12/22	dec 22
12/23	dec 23
12/24	dec 24
12/25	dec 25
12/26	dec 26
12/27	dec 27
12/28	dec 28
12/29	dec 29
12/30	dec 30
12/31	dec 31


Fix:

Instead of doing the matches of before, after, offset etc in one
function, split it out properly. First do checks if the standard
dates have to be given, then check if the -A or -B option is given.

--- day.c.orig	Sat Jun  2 14:48:00 2007
+++ day.c	Sat Jun  2 22:46:57 2007
@@ -381,17 +381,45 @@
 #ifdef DEBUG
 	fprintf(stderr, "day2: day %d(%d-%d) yday %d\n", *dayp, day, cumdays[month], tp->tm_yday);
 #endif
-	/* if today or today + offset days */
+
+	/* when only today and tomorrow (or today and the next three days if
+	   it is friday) is needed */
+	if (f_dayBefore == 0 &&
+	    f_dayAfter == 0 ) {
+		/* no year rollover */
+		if (day >= tp->tm_yday &&
+		    day <= tp->tm_yday + offset)
+			return (1);
+		/* year rollover */
+		if (tp->tm_yday + offset >= yrdays) {
+			int end = tp->tm_yday + offset - yrdays;
+			if (day <= end)
+				return (1);
+		}
+
+		return (0);
+	}
+
+	/* When days before or days after is specified */
+	/* no year rollover */
 	if (day >= tp->tm_yday - f_dayBefore &&
-	    day <= tp->tm_yday + offset + f_dayAfter)
+	    day <= tp->tm_yday + f_dayAfter)
 		return (1);
 
-	/* if number of days left in this year + days to event in next year */
-	if (yrdays - tp->tm_yday + day <= offset + f_dayAfter ||
-	    /* a year backward, eg. 6 Jan and 10 days before -> 27. Dec */
-	    tp->tm_yday + day - f_dayBefore < 0
-	    )
-		return (1);
+	/* next year */
+	if (tp->tm_yday + f_dayAfter >= yrdays) {
+		int end = tp->tm_yday + f_dayAfter - yrdays;
+		if (day <= end)
+			return (1);
+	}
+
+	/* previous year */
+	if (tp->tm_yday - f_dayBefore < 0) {
+		int before = yrdays + (tp->tm_yday - f_dayBefore );
+		if (day >= before)
+			return (1);
+	}
+
 	return (0);
 }
 
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070602125931.78E7A2E>