extract month name computation into a separate fn

This commit is contained in:
Kartik K. Agaram 2022-02-15 21:53:48 -08:00
parent d04acc0e55
commit 4d87706a3d
1 changed files with 21 additions and 26 deletions

47
ncal.c
View File

@ -60,7 +60,6 @@ __FBSDID("$FreeBSD: head/usr.bin/ncal/ncal.c 326276 2017-11-27 15:37:16Z pfg $")
typedef struct date date;
struct monthlines {
wchar_t name[MAX_WIDTH + 1];
char lines[7][MAX_WIDTH + 1];
char weeks[MAX_WIDTH + 1];
unsigned int extralen[7];
@ -655,6 +654,14 @@ printeaster(int y, int julian, int orthodox)
#define M2Y(m) ((m) / 12)
#define M2M(m) (1 + (m) % 12)
static void compute_month_name(int month_index, wchar_t *month_name, int mlen) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_mon = month_index;
wcsftime(month_name, mlen, L"%B", &tm);
month_name[0] = towupper(month_name[0]);
}
/* Print all months for the period in the range [ before .. y-m .. after ]. */
static void
monthrangeb(int y, int m, int jd_flag, int before, int after, int highlightdate)
@ -717,15 +724,18 @@ monthrangeb(int y, int m, int jd_flag, int before, int after, int highlightdate)
}
/* Month names. */
for (i = 0; i < count; i++)
for (i = 0; i < count; i++) {
wchar_t month_name[MAX_WIDTH+1];
compute_month_name(M2M(m + i) - 1, month_name, sizeof(month_name)/sizeof(month_name[0]));
if (printyearheader)
printw("%-*ls ",
mw, wcenter(ws, year[i].name, mw));
mw, wcenter(ws, month_name, mw));
else {
swprintf(ws, sizeof(ws)/sizeof(ws[0]),
L"%-ls %d", year[i].name, M2Y(m + i));
L"%-ls %d", month_name, M2Y(m + i));
printw("%-*ls ", mw, wcenter(ws1, ws, mw));
}
}
printw("\n");
/* Day of the week names. */
@ -817,12 +827,15 @@ monthranger(int y, int m, int jd_flag, int before, int after, int highlightdate)
/* Month names. */
printw(" ");
for (i = 0; i < count; i++)
for (i = 0; i < count; i++) {
wchar_t month_name[MAX_WIDTH+1];
compute_month_name(M2M(m + i) - 1, month_name, sizeof(month_name)/sizeof(month_name[0]));
if (printyearheader)
printw("%-*ls", mw, year[i].name);
printw("%-*ls", mw, month_name);
else
printw("%-ls %-*d", year[i].name,
mw - wcslen(year[i].name) - 1, M2Y(m + i));
printw("%-ls %-*d", month_name,
mw - wcslen(month_name) - 1, M2Y(m + i));
}
printw("\n");
/* And the days of the month. */
@ -855,8 +868,6 @@ static void
mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines, int highlightdate)
{
struct tm tm; /* for strftime printing local names of
* months */
date dt; /* handy date */
int dw; /* width of numbers */
int first; /* first day of month */
@ -867,13 +878,6 @@ mkmonthr(int y, int m, int jd_flag, struct monthlines *mlines, int highlightdate
char *ds; /* pointer to day strings (daystr or
* jdaystr) */
/* Set name of month. */
memset(&tm, 0, sizeof(tm));
tm.tm_mon = m;
wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
L"%B", &tm);
mlines->name[0] = towupper(mlines->name[0]);
/*
* Set first and last to the day number of the first day of this
* month and the first day of next month respectively. Set jan1 to
@ -949,8 +953,6 @@ static void
mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines, int highlightdate)
{
struct tm tm; /* for strftime printing local names of
* months */
date dt; /* handy date */
int dw; /* width of numbers */
int first; /* first day of month */
@ -970,13 +972,6 @@ mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines, int highlightdate
dw = 3;
}
/* Set name of month centered. */
memset(&tm, 0, sizeof(tm));
tm.tm_mon = m;
wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
L"%B", &tm);
mlines->name[0] = towupper(mlines->name[0]);
/*
* Set first and last to the day number of the first day of this
* month and the first day of next month respectively. Set jan1 to