Skip to content

Commit

Permalink
cal: fix --span for large numbers of months
Browse files Browse the repository at this point in the history
The need to calculate with whole years when go back for --span.

Addresses: util-linux#677
Signed-off-by: Karel Zak <[email protected]>
  • Loading branch information
karelzak committed Aug 7, 2018
1 parent 100a140 commit 2395f93
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions misc-utils/cal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 2395f93

Please sign in to comment.