2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 /* 1999-02-01 Jean-Francois Bignolles: added option '-m' to display
38 * monday as the first day of the week.
39 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
40 * - added Native Language Support
42 * 2000-09-01 Michael Charles Pruznick <dummy@netwiz.net>
43 * Added "-3" option to print prev/next month with current.
44 * Added over-ridable default NUM_MONTHS and "-1" option to
45 * get traditional output when -3 is the default. I hope that
46 * enough people will like -3 as the default that one day the
47 * product can be shipped that way.
49 * 2001-05-07 Pablo Saratxaga <pablo@mandrakesoft.com>
50 * Fixed the bugs with multi-byte charset (zg: cjk, utf-8)
51 * displaying. made the 'month year' ("%s %d") header translatable
52 * so it can be adapted to conventions used by different languages
53 * added support to read "first_weekday" locale information
54 * still to do: support for 'cal_direction' (will require a major
55 * rewrite of the displaying) and proper handling of RTL scripts
58 #include <sys/types.h>
70 #include "closestream.h"
76 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
77 # ifdef HAVE_NCURSES_H
79 # elif defined(HAVE_NCURSES_NCURSES_H)
80 # include <ncurses/ncurses.h>
84 static void my_setupterm(const char *term
, int fildes
, int *errret
)
86 setupterm((char *)term
, fildes
, errret
);
89 static void my_putstring(char *s
)
94 static const char *my_tgetstr(char *s
__attribute__((__unused__
)), char *ss
)
96 const char *ret
= tigetstr(ss
);
97 if (!ret
|| ret
== (char *)-1)
103 #elif defined(HAVE_LIBTERMCAP)
104 # include <termcap.h>
106 static char termbuffer
[4096];
107 static char tcbuffer
[4096];
108 static char *strbuf
= termbuffer
;
110 static void my_setupterm(const char *term
, int fildes
__attribute__((__unused__
)), int *errret
)
112 *errret
= tgetent(tcbuffer
, term
);
115 static void my_putstring(char *s
)
117 tputs(s
, 1, putchar
);
120 static const char *my_tgetstr(char *s
, char *ss
__attribute__((__unused__
)))
122 const char *ret
= tgetstr(s
, &strbuf
);
129 #else /* ! (HAVE_LIBTERMCAP || HAVE_LIBNCURSES || HAVE_LIBNCURSESW) */
131 static void my_putstring(char *s
)
136 #endif /* end of LIBTERMCAP / NCURSES */
138 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
139 static const char *term
="";
140 static int Slen
; /* strlen of Senter+Sexit */
143 static const char *Senter
="", *Sexit
="";/* enter and exit standout mode */
145 #include "widechar.h"
147 /* allow compile-time define to over-ride default */
149 # define NUM_MONTHS 1
152 #if ( NUM_MONTHS != 1 && NUM_MONTHS !=3 )
153 # error NUM_MONTHS must be 1 or 3
168 #define FIRST_WEEKDAY SATURDAY /* Jan 1st, 1 was a Saturday */
169 #define REFORMATION_YEAR 1752 /* Signed-off-by: Lord Chesterfield */
170 #define REFORMATION_MONTH 9 /* September */
171 #define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
172 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
173 #define YDAY_AFTER_MISSING 258 /* 14th in Sep 1752 */
175 #define DAYS_IN_YEAR 365 /* the common case, leap years are calculated */
176 #define MONTHS_IN_YEAR 12
177 #define DAYS_IN_MONTH 31
178 #define MAXDAYS 42 /* slots in a month array */
179 #define SPACE -1 /* used in day array */
181 #define SMALLEST_YEAR 1
183 #define DAY_LEN 3 /* 3 spaces per day */
184 #define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN)
186 #define MONTH_COLS 3 /* month columns in year view */
189 #define TODAY_FLAG 0x400 /* flag day for highlighting */
191 #define FMT_ST_LINES 9
192 #define FMT_ST_CHARS 300 /* 90 suffices in most locales */
195 char s
[FMT_ST_LINES
][FMT_ST_CHARS
];
198 static const int days_in_month
[2][13] = {
199 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
200 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
203 /* September 1752 is special, and has static assignments for both date
204 * and Julian representations. */
205 static const int d_sep1752
[MAXDAYS
/ 2] = {
206 SPACE
, SPACE
, 1, 2, 14, 15, 16,
207 17, 18, 19, 20, 21, 22, 23,
208 24, 25, 26, 27, 28, 29, 30
209 }, j_sep1752
[MAXDAYS
/ 2] = {
210 SPACE
, SPACE
, 245, 246, 258, 259, 260,
211 261, 262, 263, 264, 265, 266, 267,
212 268, 269, 270, 271, 272, 273, 274
213 }, empty
[MAXDAYS
] = {
214 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
215 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
216 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
217 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
218 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
,
219 SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
, SPACE
223 WEEK_NUM_DISABLED
= 0,
229 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
230 static char day_headings
[(WEEK_LEN
+ 1) * 6 + 1];
240 const char *full_month
[MONTHS_IN_YEAR
]; /* month names */
241 int colormode
; /* day and week number highlight */
242 int num_months
; /* number of months horizontally in print out */
243 int weekstart
; /* day the week starts, often Sun or Mon */
244 int weektype
; /* WEEK_TYPE_{NONE,ISO,US} */
245 size_t day_width
; /* day width in characters in printout */
246 size_t week_width
; /* 7 * day_width + possible week num */
247 int gutter_width
; /* spaces in between horizontal month outputs */
248 struct cal_request req
; /* the times user is interested */
249 unsigned int julian
:1, /* julian output */
250 yflag
:1, /* print whole year */
251 header_hint
:1; /* does month name + year need two lines to fit */
255 int days
[MAXDAYS
]; /* the day numbers, or SPACE */
256 int weeks
[MAXDAYS
/ DAYS_IN_WEEK
];
259 struct cal_month
*next
;
262 /* function prototypes */
263 static int leap_year(int32_t year
);
264 static void headers_init(struct cal_control
*ctl
);
265 static void set_consecutive_months(struct cal_month
*month
, int m
, int32_t y
);
266 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
);
267 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
);
268 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
);
269 static void monthly(const struct cal_control
*ctl
);
270 static void monthly3(const struct cal_control
*ctl
);
271 static void yearly(const struct cal_control
*ctl
);
272 static int day_in_year(int day
, int month
, int32_t year
);
273 static int day_in_week(int day
, int month
, int32_t year
);
274 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
);
275 static int week_to_day(const struct cal_control
*ctl
);
276 static int center_str(const char *src
, char *dest
, size_t dest_size
, size_t width
);
277 static void center(const char *str
, size_t len
, int separate
);
278 static void __attribute__((__noreturn__
)) usage(FILE *out
);
280 int main(int argc
, char **argv
)
282 struct tm
*local_time
;
285 static struct cal_control ctl
= {
287 .num_months
= NUM_MONTHS
,
288 .colormode
= UL_COLORMODE_AUTO
,
289 .weektype
= WEEK_NUM_DISABLED
,
290 .day_width
= DAY_LEN
,
297 OPT_COLOR
= CHAR_MAX
+ 1
300 static const struct option longopts
[] = {
301 {"one", no_argument
, NULL
, '1'},
302 {"three", no_argument
, NULL
, '3'},
303 {"sunday", no_argument
, NULL
, 's'},
304 {"monday", no_argument
, NULL
, 'm'},
305 {"julian", no_argument
, NULL
, 'j'},
306 {"year", no_argument
, NULL
, 'y'},
307 {"week", optional_argument
, NULL
, 'w'},
308 {"color", optional_argument
, NULL
, OPT_COLOR
},
309 {"version", no_argument
, NULL
, 'V'},
310 {"help", no_argument
, NULL
, 'h'},
314 setlocale(LC_ALL
, "");
315 bindtextdomain(PACKAGE
, LOCALEDIR
);
317 atexit(close_stdout
);
319 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
320 if ((term
= getenv("TERM"))) {
322 my_setupterm(term
, STDOUT_FILENO
, &ret
);
324 Senter
= my_tgetstr("so","smso");
325 Sexit
= my_tgetstr("se","rmso");
326 Slen
= strlen(Senter
) + strlen(Sexit
);
332 * The traditional Unix cal utility starts the week at Sunday,
333 * while ISO 8601 starts at Monday. We read the start day from
334 * the locale database, which can be overridden with the
335 * -s (Sunday) or -m (Monday) options.
337 #if HAVE_DECL__NL_TIME_WEEK_1STDAY
339 * You need to use 2 locale variables to get the first day of the week.
340 * This is needed to support first_weekday=2 and first_workday=1 for
341 * the rare case where working days span across 2 weeks.
342 * This shell script shows the combinations and calculations involved:
344 * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
345 * printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
346 * date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
349 * en_US: 19971130 + 1 -1 = 0 #0 = sunday
350 * ru_RU: 19971130 + 2 -1 = 1
351 * fr_FR: 19971201 + 1 -1 = 1
352 * csb_PL: 19971201 + 2 -1 = 2
353 * POSIX: 19971201 + 7 -1 = 0
357 union { unsigned int word
; char *string
; } val
;
358 val
.string
= nl_langinfo(_NL_TIME_WEEK_1STDAY
);
361 wfd
= day_in_week(wfd
% 100, (wfd
/ 100) % 100, wfd
/ (100 * 100));
362 ctl
.weekstart
= (wfd
+ *nl_langinfo(_NL_TIME_FIRST_WEEKDAY
) - 1) % DAYS_IN_WEEK
;
366 while ((ch
= getopt_long(argc
, argv
, "13mjsywVh", longopts
, NULL
)) != -1)
369 ctl
.num_months
= 1; /* default */
375 ctl
.weekstart
= SUNDAY
; /* default */
378 ctl
.weekstart
= MONDAY
;
382 ctl
.day_width
= DAY_LEN
+ 1;
389 ctl
.req
.week
= strtos32_or_err(optarg
,
390 _("invalid week argument"));
391 if (ctl
.req
.week
< 1 || 53 < ctl
.req
.week
)
392 errx(EXIT_FAILURE
,_("illegal week value: use 1-53"));
394 ctl
.weektype
= WEEK_NUM_US
; /* default per weekstart */
398 ctl
.colormode
= colormode_or_err(optarg
,
399 _("unsupported color mode"));
402 printf(UTIL_LINUX_VERSION
);
414 ctl
.weektype
= ctl
.req
.week
& WEEK_NUM_MASK
;
415 ctl
.weektype
|= (ctl
.weekstart
== MONDAY
? WEEK_NUM_ISO
: WEEK_NUM_US
);
416 ctl
.week_width
= (ctl
.day_width
* DAYS_IN_WEEK
) + WNUM_LEN
;
418 ctl
.week_width
= ctl
.day_width
* DAYS_IN_WEEK
;
421 local_time
= localtime(&now
);
425 ctl
.req
.day
= strtos32_or_err(*argv
++, _("illegal day value"));
426 if (ctl
.req
.day
< 1 || DAYS_IN_MONTH
< ctl
.req
.day
)
427 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), DAYS_IN_MONTH
);
430 ctl
.req
.month
= strtos32_or_err(*argv
++, _("illegal month value: use 1-12"));
431 if (ctl
.req
.month
< 1 || MONTHS_IN_YEAR
< ctl
.req
.month
)
432 errx(EXIT_FAILURE
, _("illegal month value: use 1-12"));
435 ctl
.req
.year
= strtos32_or_err(*argv
++, _("illegal year value"));
436 if (ctl
.req
.year
< SMALLEST_YEAR
)
437 errx(EXIT_FAILURE
, _("illegal year value: use positive integer"));
438 if (ctl
.req
.year
== INT32_MAX
)
439 errx(EXIT_FAILURE
, _("illegal year value"));
441 int dm
= days_in_month
[leap_year(ctl
.req
.year
)][ctl
.req
.month
];
442 if (ctl
.req
.day
> dm
)
443 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), dm
);
444 ctl
.req
.day
= day_in_year(ctl
.req
.day
, ctl
.req
.month
, ctl
.req
.year
);
445 } else if ((int32_t) (local_time
->tm_year
+ 1900) == ctl
.req
.year
) {
446 ctl
.req
.day
= local_time
->tm_yday
+ 1;
448 if (!ctl
.req
.month
&& !ctl
.req
.week
) {
449 ctl
.req
.month
= local_time
->tm_mon
+ 1;
454 ctl
.req
.day
= local_time
->tm_yday
+ 1;
455 ctl
.req
.year
= local_time
->tm_year
+ 1900;
456 ctl
.req
.month
= local_time
->tm_mon
+ 1;
462 if (0 < ctl
.req
.week
) {
463 int yday
= week_to_day(&ctl
);
464 int leap
= leap_year(ctl
.req
.year
);
468 errx(EXIT_FAILURE
, _("illegal week value: year %d "
469 "doesn't have week %d"),
470 ctl
.req
.year
, ctl
.req
.week
);
471 while (m
<= 12 && yday
> days_in_month
[leap
][m
])
472 yday
-= days_in_month
[leap
][m
++];
474 /* In some years (e.g. 2010 in ISO mode) it's possible
475 * to have a remnant of week 53 starting the year yet
476 * the year in question ends during 52, in this case
477 * we're assuming that early remnant is being referred
478 * to if 53 is given as argument. */
479 if (ctl
.req
.week
!= week_number(31, 12, ctl
.req
.year
- 1, &ctl
))
481 _("illegal week value: year %d "
482 "doesn't have week %d"),
483 ctl
.req
.year
, ctl
.req
.week
);
486 ctl
.req
.month
= 12 < m
? 1 : m
;
491 if (!colors_init(ctl
.colormode
)) {
493 ctl
.weektype
&= ~WEEK_NUM_MASK
;
498 ctl
.num_months
= MONTH_COLS
- 1;
500 ctl
.num_months
= MONTH_COLS
;
501 ctl
.gutter_width
= 3;
503 } else if (ctl
.num_months
== 1)
505 else if (ctl
.num_months
== 3)
511 /* leap year -- account for gregorian reformation in 1752 */
512 static int leap_year(int32_t year
)
514 if (year
<= REFORMATION_YEAR
)
517 return ( !(year
% 4) && (year
% 100) ) || !(year
% 400);
520 static void headers_init(struct cal_control
*ctl
)
523 char *cur_dh
= day_headings
;
524 char tmp
[FMT_ST_CHARS
];
527 year_len
= snprintf(tmp
, sizeof(tmp
), "%d", ctl
->req
.year
);
529 for (i
= 0; i
< DAYS_IN_WEEK
; i
++) {
531 wd
= (i
+ ctl
->weekstart
) % DAYS_IN_WEEK
;
534 strcat(cur_dh
++, " ");
535 space_left
= sizeof(day_headings
) - (cur_dh
- day_headings
);
537 if (space_left
<= (ctl
->day_width
- 1))
539 cur_dh
+= center_str(nl_langinfo(ABDAY_1
+ wd
), cur_dh
,
540 space_left
, ctl
->day_width
- 1);
543 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++) {
544 ctl
->full_month
[i
] = nl_langinfo(MON_1
+ i
);
545 /* The +1 after year_len is space in between month and year. */
546 if (ctl
->week_width
< strlen(ctl
->full_month
[i
]) + year_len
+ 1)
547 ctl
->header_hint
= 1;
551 static void set_consecutive_months(struct cal_month
*month
, int m
, int32_t y
)
554 for (i
= month
; i
; i
= i
->next
) {
557 if (MONTHS_IN_YEAR
< m
) {
564 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
)
566 int first_week_day
= day_in_week(1, month
->month
, month
->year
);
568 int i
, j
, weeklines
= 0;
571 j
= day_in_year(1, month
->month
, month
->year
);
574 month_days
= j
+ days_in_month
[leap_year(month
->year
)][month
->month
];
576 /* True when Sunday is not first day in the output week. */
577 if (ctl
->weekstart
) {
578 first_week_day
-= ctl
->weekstart
;
579 if (first_week_day
< 0)
580 first_week_day
= DAYS_IN_WEEK
- ctl
->weekstart
;
581 month_days
+= ctl
->weekstart
- 1;
584 /* Fill day array. */
585 for (i
= 0; i
< MAXDAYS
; i
++) {
586 if (0 < first_week_day
) {
587 month
->days
[i
] = SPACE
;
591 if (j
< month_days
) {
592 if (month
->year
== 1752 && month
->month
== 9 && (j
== 3 || j
== 247))
593 j
+= NUMBER_MISSING_DAYS
;
598 month
->days
[i
] = SPACE
;
602 /* Add week numbers */
604 int weeknum
= week_number(1, month
->month
, month
->year
, ctl
);
605 weeklines
= MAXDAYS
/ DAYS_IN_WEEK
- weeklines
/ DAYS_IN_WEEK
;
606 for (i
= 0; i
< MAXDAYS
/ DAYS_IN_WEEK
; i
++) {
608 month
->weeks
[i
] = weeknum
++;
610 month
->weeks
[i
] = SPACE
;
612 if (52 < weeknum
&& i
== 0)
613 weeknum
= week_number(month
->days
[DAYS_IN_WEEK
* (i
+ 1)], 1, month
->year
, ctl
);
614 else if (52 < weeknum
)
615 weeknum
= week_number(31, 12, month
->year
, ctl
);
620 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
)
622 char out
[FMT_ST_CHARS
];
625 if (ctl
->header_hint
|| ctl
->yflag
) {
626 for (i
= month
; i
; i
= i
->next
) {
627 sprintf(out
, _("%s"), ctl
->full_month
[i
->month
- 1]);
628 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
632 for (i
= month
; i
; i
= i
->next
) {
633 sprintf(out
, _("%d"), i
->year
);
634 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
638 for (i
= month
; i
; i
= i
->next
) {
639 sprintf(out
, _("%s %d"), ctl
->full_month
[i
->month
- 1], i
->year
);
640 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
644 for (i
= month
; i
; i
= i
->next
) {
647 printf("%*s%s", (int)ctl
->day_width
- 1, "", day_headings
);
649 printf("%*s%s", (int)ctl
->day_width
, "", day_headings
);
651 fputs(day_headings
, stdout
);
653 printf("%*s", ctl
->gutter_width
, "");
658 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
)
660 int reqday
, week_line
, d
;
664 for (week_line
= 0; week_line
< MAXDAYS
/ DAYS_IN_WEEK
; week_line
++) {
665 for (i
= month
; i
; i
= i
->next
) {
666 /* Determine the day that should be highlighted. */
668 if (i
->month
== ctl
->req
.month
&& i
->year
== ctl
->req
.year
) {
670 reqday
= ctl
->req
.day
;
673 ctl
->req
.day
+ 1 - day_in_year(1, i
->month
,
678 if (0 < i
->weeks
[week_line
]) {
679 if ((ctl
->weektype
& WEEK_NUM_MASK
) ==
681 printf("%s%2d%s", Senter
, i
->weeks
[week_line
],
684 printf("%2d", i
->weeks
[week_line
]);
687 skip
= ctl
->day_width
;
689 /* First day of the week is one char narrower than the other days,
690 * unless week number is printed. */
691 skip
= ctl
->day_width
- 1;
693 for (d
= DAYS_IN_WEEK
* week_line
;
694 d
< DAYS_IN_WEEK
* week_line
+ DAYS_IN_WEEK
; d
++) {
695 if (0 < i
->days
[d
]) {
696 if (reqday
== i
->days
[d
])
697 printf("%*s%s%*d%s", skip
- (ctl
->julian
? 3 : 2),
698 "", Senter
, (ctl
->julian
? 3 : 2),
701 printf("%*d", skip
, i
->days
[d
]);
703 printf("%*s", skip
, "");
704 if (skip
< (int)ctl
->day_width
)
708 printf("%*s", ctl
->gutter_width
, "");
711 printf("%*s\n", ctl
->gutter_width
- (ctl
->yflag
? 0 : 1), "");
715 static void monthly(const struct cal_control
*ctl
)
717 struct cal_month month
;
719 month
.month
= ctl
->req
.month
;
720 month
.year
= ctl
->req
.year
;
723 cal_fill_month(&month
, ctl
);
725 cal_output_header(&month
, ctl
);
726 cal_output_months(&month
, ctl
);
729 static void monthly3(const struct cal_control
*ctl
)
731 struct cal_month m1
, m2
, m3
, *i
;
739 if (ctl
->req
.month
== 1) {
740 first_month
= MONTHS_IN_YEAR
;
741 first_year
= ctl
->req
.year
- 1;
743 first_month
= ctl
->req
.month
- 1;
744 first_year
= ctl
->req
.year
;
747 set_consecutive_months(&m1
, first_month
, first_year
);
748 for (i
= &m1
; i
; i
= i
->next
)
749 cal_fill_month(i
, ctl
);
750 cal_output_header(&m1
, ctl
);
751 cal_output_months(&m1
, ctl
);
754 static void yearly(const struct cal_control
*ctl
)
756 struct cal_month m1
, m2
, m3
, *i
;
758 char out
[FMT_ST_CHARS
];
770 for (i
= &m1
; i
; i
= i
->next
)
771 year_width
+= ctl
->week_width
+ 1;
774 sprintf(out
, "%d", ctl
->req
.year
);
775 center(out
, year_width
, 0);
776 fputs("\n\n", stdout
);
778 for (month
= 1; month
< MONTHS_IN_YEAR
; month
+= ctl
->julian
? 2 : 3) {
779 set_consecutive_months(&m1
, month
, ctl
->req
.year
);
780 for (i
= &m1
; i
; i
= i
->next
)
781 cal_fill_month(i
, ctl
);
782 cal_output_header(&m1
, ctl
);
783 cal_output_months(&m1
, ctl
);
785 /* Is empty line at the end year output really needed? */
791 * return the 1 based day number within the year
793 static int day_in_year(int day
, int month
, int32_t year
)
797 leap
= leap_year(year
);
798 for (i
= 1; i
< month
; i
++)
799 day
+= days_in_month
[leap
][i
];
805 * return the 0 based day number for any date from 1 Jan. 1 to
806 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
807 * 3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
808 * during the period of 11 days.
810 static int day_in_week(int day
, int month
, int32_t year
)
812 static const int reform
[] = {
813 SUNDAY
, WEDNESDAY
, TUESDAY
, FRIDAY
, SUNDAY
, WEDNESDAY
,
814 FRIDAY
, MONDAY
, THURSDAY
, SATURDAY
, TUESDAY
, THURSDAY
816 static const int old
[] = {
817 FRIDAY
, MONDAY
, SUNDAY
, WEDNESDAY
, FRIDAY
, MONDAY
,
818 WEDNESDAY
, SATURDAY
, TUESDAY
, THURSDAY
, SUNDAY
, TUESDAY
823 year
-= (month
< 3) + 14;
824 if (REFORMATION_YEAR
< year
825 || (year
== REFORMATION_YEAR
&& 9 < month
)
826 || (year
== REFORMATION_YEAR
&& month
== 9 && 13 < day
))
827 return (year
+ (year
/ 4) - (year
/ 100) + (year
/ 400) + reform
[month
- 1] +
829 if (year
< REFORMATION_YEAR
830 || (year
== REFORMATION_YEAR
&& month
< 9)
831 || (year
== REFORMATION_YEAR
&& month
== 9 && day
< 3))
832 return (year
+ year
/ 4 + old
[month
- 1] + day
) % 7;
838 * return the week number of a given date, 1..53.
839 * Supports ISO-8601 and North American modes.
840 * Day may be given as Julian day of the year mode, in which
841 * case the month is disregarded entirely.
843 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
)
846 int wday
= day_in_week(1, 1, year
);
848 if (ctl
->weektype
& WEEK_NUM_ISO
)
849 fday
= wday
+ (wday
>= FRIDAY
? -2 : 5);
852 * - according to gcal, the first Sun is in the first week
853 * - according to wikipedia, the first Sat is in the first week
855 fday
= wday
+ (wday
== SUNDAY
? 6 : -1);
857 /* For julian dates the month can be set to 1, the global julian
858 * variable cannot be relied upon here, because we may recurse
859 * internally for 31.12. which would not work. */
863 yday
= day_in_year(day
,month
,year
);
864 if (year
== REFORMATION_YEAR
) {
865 if (yday
>= YDAY_AFTER_MISSING
)
866 fday
-= NUMBER_MISSING_DAYS
;
869 /* Last year is last year */
871 return week_number(31, 12, year
- 1, ctl
);
873 /* Or it could be part of the next year. The reformation year had less
874 * days than 365 making this check invalid, but reformation year ended
875 * on Sunday and in week 51, so it's ok here. */
876 if (ctl
->weektype
== WEEK_NUM_ISO
&& yday
>= 363
877 && day_in_week(day
, month
, year
) >= MONDAY
878 && day_in_week(day
, month
, year
) <= WEDNESDAY
879 && day_in_week(31, 12, year
) >= MONDAY
880 && day_in_week(31, 12, year
) <= WEDNESDAY
)
881 return week_number(1, 1, year
+ 1, ctl
);
883 return (yday
+ fday
) / 7;
888 * return the yday of the first day in a given week inside
889 * the given year. This may be something other than Monday
890 * for ISO-8601 modes. For North American numbering this
891 * always returns a Sunday.
893 static int week_to_day(const struct cal_control
*ctl
)
897 wday
= day_in_week(1, 1, ctl
->req
.year
);
898 yday
= ctl
->req
.week
* 7 - wday
;
900 if (ctl
->weektype
& WEEK_NUM_ISO
)
901 yday
-= (wday
>= FRIDAY
? -2 : 5);
903 yday
-= (wday
== SUNDAY
? 6 : -1); /* WEEK_NUM_US */
911 * Center string, handling multibyte characters appropriately.
912 * In addition if the string is too large for the width it's truncated.
913 * The number of trailing spaces may be 1 less than the number of leading spaces.
915 static int center_str(const char* src
, char* dest
,
916 size_t dest_size
, size_t width
)
918 return mbsalign(src
, dest
, dest_size
, &width
,
919 MBS_ALIGN_CENTER
, MBA_UNIBYTE_FALLBACK
);
922 static void center(const char *str
, size_t len
, int separate
)
924 char lineout
[FMT_ST_CHARS
];
926 center_str(str
, lineout
, ARRAY_SIZE(lineout
), len
);
927 my_putstring(lineout
);
930 snprintf(lineout
, sizeof(lineout
), "%*s", separate
, "");
931 my_putstring(lineout
);
935 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
937 fputs(USAGE_HEADER
, out
);
938 fprintf(out
, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name
);
940 fputs(USAGE_SEPARATOR
, out
);
941 fputs(_("Display a calendar, or some part of it.\n"), out
);
942 fputs(_("Without any arguments, display the current month.\n"), out
);
944 fputs(USAGE_OPTIONS
, out
);
945 fputs(_(" -1, --one show only a single month (default)\n"), out
);
946 fputs(_(" -3, --three show three months spanning the date\n"), out
);
947 fputs(_(" -s, --sunday Sunday as first day of week\n"), out
);
948 fputs(_(" -m, --monday Monday as first day of week\n"), out
);
949 fputs(_(" -j, --julian output Julian dates\n"), out
);
950 fputs(_(" -y, --year show the whole year\n"), out
);
951 fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out
);
952 fputs(_(" --color[=<when>] colorize messages (auto, always or never)\n"), out
);
954 fputs(USAGE_SEPARATOR
, out
);
955 fputs(USAGE_HELP
, out
);
956 fputs(USAGE_VERSION
, out
);
957 fprintf(out
, USAGE_MAN_TAIL("cal(1)"));
959 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);