From 2395f93c8ca0ba6ff04c0099d5d428c50f66d643 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 7 Aug 2018 10:52:33 +0200 Subject: [PATCH] cal: fix --span for large numbers of months The need to calculate with whole years when go back for --span. Addresses: https://github.com/karelzak/util-linux/issues/677 Signed-off-by: Karel Zak --- misc-utils/cal.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/misc-utils/cal.c b/misc-utils/cal.c index acbcf5005e8..4dff491fb63 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -840,17 +840,20 @@ static void cal_output_months(struct cal_month *month, const struct cal_control static void monthly(const struct cal_control *ctl) { struct cal_month m1,m2,m3, *m; - int i, rows, new_month, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month; + int i, rows, month = ctl->req.start_month ? ctl->req.start_month : ctl->req.month; int32_t year = ctl->req.year; /* cal -3, cal -Y --span, etc. */ if (ctl->span_months) { - new_month = month - ctl->num_months / 2; + int new_month = month - ctl->num_months / 2; if (new_month < 1) { - month = new_month + MONTHS_IN_YEAR; - year--; - } - else + new_month *= -1; + year -= (new_month / MONTHS_IN_YEAR) + 1; + + if (new_month > MONTHS_IN_YEAR) + new_month %= MONTHS_IN_YEAR; + month = MONTHS_IN_YEAR - new_month; + } else month = new_month; }