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 overridable default MONTHS_IN_ROW 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>
71 #include "closestream.h"
77 #include "timeutils.h"
79 static int has_term
= 0;
80 static const char *Senter
= "", *Sexit
= ""; /* enter and exit standout mode */
82 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
83 # ifdef HAVE_NCURSES_H
85 # elif defined(HAVE_NCURSES_NCURSES_H)
86 # include <ncurses/ncurses.h>
91 static int setup_terminal(char *term
92 #if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
93 __attribute__((__unused__
))
97 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
100 if (setupterm(term
, STDOUT_FILENO
, &ret
) != OK
|| ret
!= 1)
106 static void my_putstring(char *s
)
108 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
116 static const char *my_tgetstr(char *ss
117 #if !defined(HAVE_LIBNCURSES) && !defined(HAVE_LIBNCURSESW)
118 __attribute__((__unused__
))
122 const char *ret
= NULL
;
124 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW)
128 if (!ret
|| ret
== (char *)-1)
133 #include "widechar.h"
162 #define REFORMATION_YEAR 1752 /* Signed-off-by: Lord Chesterfield */
163 #define REFORMATION_MONTH SEPTEMBER
164 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
165 #define YDAY_AFTER_MISSING 258 /* 14th in Sep 1752 */
167 #define MONTHS_IN_YEAR DECEMBER
168 #define DAYS_IN_MONTH 31
169 #define MAXDAYS 42 /* slots in a month array */
170 #define SPACE -1 /* used in day array */
172 #define SMALLEST_YEAR 1
174 #define DAY_LEN 3 /* 3 spaces per day */
175 #define WEEK_LEN (DAYS_IN_WEEK * DAY_LEN)
176 #define MONTHS_IN_YEAR_ROW 3 /* month columns in year view */
179 #define FMT_ST_CHARS 300 /* 90 suffices in most locales */
181 static const int days_in_month
[2][13] = {
182 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
183 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
187 WEEK_NUM_DISABLED
= 0,
193 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
194 static char day_headings
[(WEEK_LEN
+ 1) * 6 + 1];
205 const char *full_month
[MONTHS_IN_YEAR
]; /* month names */
206 const char *abbr_month
[MONTHS_IN_YEAR
]; /* abbreviated month names */
208 int colormode
; /* day and week number highlight */
209 int num_months
; /* number of requested months */
210 int span_months
; /* span the date */
211 int months_in_row
; /* number of months horizontally in print out */
212 int weekstart
; /* day the week starts, often Sun or Mon */
213 int weektype
; /* WEEK_TYPE_{NONE,ISO,US} */
214 size_t day_width
; /* day width in characters in printout */
215 size_t week_width
; /* 7 * day_width + possible week num */
216 int gutter_width
; /* spaces in between horizontal month outputs */
217 struct cal_request req
; /* the times user is interested */
218 unsigned int julian
:1, /* julian output */
219 header_year
:1, /* print year number */
220 header_hint
:1; /* does month name + year need two lines to fit */
224 int days
[MAXDAYS
]; /* the day numbers, or SPACE */
225 int weeks
[MAXDAYS
/ DAYS_IN_WEEK
];
228 struct cal_month
*next
;
231 /* function prototypes */
232 static int leap_year(int32_t year
);
233 static int monthname_to_number(struct cal_control
*ctl
, const char *name
);
234 static void headers_init(struct cal_control
*ctl
);
235 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
);
236 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
);
237 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
);
238 static void monthly(const struct cal_control
*ctl
);
239 static void yearly(const struct cal_control
*ctl
);
240 static int day_in_year(int day
, int month
, int32_t year
);
241 static int day_in_week(int day
, int month
, int32_t year
);
242 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
);
243 static int week_to_day(const struct cal_control
*ctl
);
244 static int center_str(const char *src
, char *dest
, size_t dest_size
, size_t width
);
245 static void center(const char *str
, size_t len
, int separate
);
246 static void __attribute__((__noreturn__
)) usage(FILE *out
);
248 int main(int argc
, char **argv
)
250 struct tm
*local_time
;
253 int ch
= 0, yflag
= 0, Yflag
= 0;
254 static struct cal_control ctl
= {
256 .num_months
= 1, /* default is "cal -1" */
258 .colormode
= UL_COLORMODE_UNDEF
,
259 .weektype
= WEEK_NUM_DISABLED
,
260 .day_width
= DAY_LEN
,
267 OPT_COLOR
= CHAR_MAX
+ 1
270 static const struct option longopts
[] = {
271 {"one", no_argument
, NULL
, '1'},
272 {"three", no_argument
, NULL
, '3'},
273 {"sunday", no_argument
, NULL
, 's'},
274 {"monday", no_argument
, NULL
, 'm'},
275 {"julian", no_argument
, NULL
, 'j'},
276 {"months", required_argument
, NULL
, 'n'},
277 {"span", no_argument
, NULL
, 'S'},
278 {"year", no_argument
, NULL
, 'y'},
279 {"week", optional_argument
, NULL
, 'w'},
280 {"color", optional_argument
, NULL
, OPT_COLOR
},
281 {"version", no_argument
, NULL
, 'V'},
282 {"twelve", no_argument
, NULL
, 'Y'},
283 {"help", no_argument
, NULL
, 'h'},
287 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
291 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
293 setlocale(LC_ALL
, "");
294 bindtextdomain(PACKAGE
, LOCALEDIR
);
296 atexit(close_stdout
);
298 term
= getenv("TERM");
300 has_term
= setup_terminal(term
) == 0;
302 Senter
= my_tgetstr("smso");
303 Sexit
= my_tgetstr("rmso");
308 * The traditional Unix cal utility starts the week at Sunday,
309 * while ISO 8601 starts at Monday. We read the start day from
310 * the locale database, which can be overridden with the
311 * -s (Sunday) or -m (Monday) options.
313 #if HAVE_DECL__NL_TIME_WEEK_1STDAY
315 * You need to use 2 locale variables to get the first day of the week.
316 * This is needed to support first_weekday=2 and first_workday=1 for
317 * the rare case where working days span across 2 weeks.
318 * This shell script shows the combinations and calculations involved:
320 * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
321 * printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
322 * date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
325 * en_US: 19971130 + 1 -1 = 0 #0 = sunday
326 * ru_RU: 19971130 + 2 -1 = 1
327 * fr_FR: 19971201 + 1 -1 = 1
328 * csb_PL: 19971201 + 2 -1 = 2
329 * POSIX: 19971201 + 7 -1 = 0
333 union { unsigned int word
; char *string
; } val
;
334 val
.string
= nl_langinfo(_NL_TIME_WEEK_1STDAY
);
337 wfd
= day_in_week(wfd
% 100, (wfd
/ 100) % 100, wfd
/ (100 * 100));
338 ctl
.weekstart
= (wfd
+ *nl_langinfo(_NL_TIME_FIRST_WEEKDAY
) - 1) % DAYS_IN_WEEK
;
341 while ((ch
= getopt_long(argc
, argv
, "13mjn:sSywYVh", longopts
, NULL
)) != -1) {
343 err_exclusive_options(ch
, longopts
, excl
, excl_st
);
352 ctl
.months_in_row
= 3;
355 ctl
.weekstart
= SUNDAY
; /* default */
358 ctl
.weekstart
= MONDAY
;
362 ctl
.day_width
= DAY_LEN
+ 1;
371 ctl
.num_months
= strtou32_or_err(optarg
,
372 _("invalid month argument"));
379 ctl
.req
.week
= strtos32_or_err(optarg
,
380 _("invalid week argument"));
381 if (ctl
.req
.week
< 1 || 54 < ctl
.req
.week
)
382 errx(EXIT_FAILURE
,_("illegal week value: use 1-54"));
384 ctl
.weektype
= WEEK_NUM_US
; /* default per weekstart */
387 ctl
.colormode
= UL_COLORMODE_AUTO
;
389 ctl
.colormode
= colormode_or_err(optarg
,
390 _("unsupported color mode"));
393 printf(UTIL_LINUX_VERSION
);
398 errtryhelp(EXIT_FAILURE
);
406 ctl
.weektype
= ctl
.req
.week
& WEEK_NUM_MASK
;
407 ctl
.weektype
|= (ctl
.weekstart
== MONDAY
? WEEK_NUM_ISO
: WEEK_NUM_US
);
408 ctl
.week_width
= (ctl
.day_width
* DAYS_IN_WEEK
) + WNUM_LEN
;
410 ctl
.week_width
= ctl
.day_width
* DAYS_IN_WEEK
;
412 if (argc
== 1 && !isdigit_string(*argv
)) {
414 /* cal <timestamp> */
415 if (parse_timestamp(*argv
, &x
) == 0)
416 now
= (time_t) (x
/ 1000000);
417 /* cal <monthname> */
418 else if ((ctl
.req
.month
= monthname_to_number(&ctl
, *argv
)) > 0)
419 time(&now
); /* this year */
421 errx(EXIT_FAILURE
, _("failed to parse timestamp or unknown month name: %s"), *argv
);
426 local_time
= localtime(&now
);
430 ctl
.req
.day
= strtos32_or_err(*argv
++, _("illegal day value"));
431 if (ctl
.req
.day
< 1 || DAYS_IN_MONTH
< ctl
.req
.day
)
432 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), DAYS_IN_MONTH
);
436 ctl
.req
.month
= strtos32_or_err(*argv
++, _("illegal month value: use 1-12"));
438 ctl
.req
.month
= monthname_to_number(&ctl
, *argv
);
439 if (ctl
.req
.month
< 0)
440 errx(EXIT_FAILURE
, _("unknown month name: %s"), *argv
);
443 if (ctl
.req
.month
< 1 || MONTHS_IN_YEAR
< ctl
.req
.month
)
444 errx(EXIT_FAILURE
, _("illegal month value: use 1-12"));
447 ctl
.req
.year
= strtos32_or_err(*argv
++, _("illegal year value"));
448 if (ctl
.req
.year
< SMALLEST_YEAR
)
449 errx(EXIT_FAILURE
, _("illegal year value: use positive integer"));
450 if (ctl
.req
.year
== INT32_MAX
)
451 errx(EXIT_FAILURE
, _("illegal year value"));
453 int dm
= days_in_month
[leap_year(ctl
.req
.year
)][ctl
.req
.month
];
454 if (ctl
.req
.day
> dm
)
455 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), dm
);
456 ctl
.req
.day
= day_in_year(ctl
.req
.day
, ctl
.req
.month
, ctl
.req
.year
);
457 } else if ((int32_t) (local_time
->tm_year
+ 1900) == ctl
.req
.year
) {
458 ctl
.req
.day
= local_time
->tm_yday
+ 1;
460 if (!ctl
.req
.month
&& !ctl
.req
.week
) {
461 ctl
.req
.month
= local_time
->tm_mon
+ 1;
466 ctl
.req
.day
= local_time
->tm_yday
+ 1;
467 ctl
.req
.year
= local_time
->tm_year
+ 1900;
469 ctl
.req
.month
= local_time
->tm_mon
+ 1;
475 if (0 < ctl
.req
.week
) {
476 int yday
= week_to_day(&ctl
);
477 int leap
= leap_year(ctl
.req
.year
);
481 errx(EXIT_FAILURE
, _("illegal week value: year %d "
482 "doesn't have week %d"),
483 ctl
.req
.year
, ctl
.req
.week
);
484 while (m
<= DECEMBER
&& yday
> days_in_month
[leap
][m
])
485 yday
-= days_in_month
[leap
][m
++];
486 if (DECEMBER
< m
&& ctl
.weektype
& WEEK_NUM_ISO
) {
487 /* In some years (e.g. 2010 in ISO mode) it's possible
488 * to have a remnant of week 53 starting the year yet
489 * the year in question ends during 52, in this case
490 * we're assuming that early remnant is being referred
491 * to if 53 is given as argument. */
492 if (ctl
.req
.week
!= week_number(31, DECEMBER
, ctl
.req
.year
- 1, &ctl
))
494 _("illegal week value: year %d "
495 "doesn't have week %d"),
496 ctl
.req
.year
, ctl
.req
.week
);
499 ctl
.req
.month
= MONTHS_IN_YEAR
< m
? 1 : m
;
504 if (!colors_init(ctl
.colormode
, "cal")) {
506 ctl
.weektype
&= ~WEEK_NUM_MASK
;
509 if (yflag
|| Yflag
) {
510 ctl
.gutter_width
= 3;
511 ctl
.num_months
= MONTHS_IN_YEAR
;
513 ctl
.req
.start_month
= 1; /* start from Jan */
514 ctl
.header_year
= 1; /* print year number */
518 if (ctl
.num_months
> 1 && ctl
.months_in_row
== 0)
519 ctl
.months_in_row
= ctl
.julian
? MONTHS_IN_YEAR_ROW
- 1 :
521 else if (!ctl
.months_in_row
)
522 ctl
.months_in_row
= 1;
532 /* leap year -- account for gregorian reformation in 1752 */
533 static int leap_year(int32_t year
)
535 if (year
<= REFORMATION_YEAR
)
538 return ( !(year
% 4) && (year
% 100) ) || !(year
% 400);
541 static void init_monthnames(struct cal_control
*ctl
)
545 if (ctl
->full_month
[0] != '\0')
546 return; /* already initialized */
548 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++)
549 ctl
->full_month
[i
] = nl_langinfo(MON_1
+ i
);
552 static void init_abbr_monthnames(struct cal_control
*ctl
)
556 if (ctl
->abbr_month
[0] != '\0')
557 return; /* already initialized */
559 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++)
560 ctl
->abbr_month
[i
] = nl_langinfo(ABMON_1
+ i
);
563 static int monthname_to_number(struct cal_control
*ctl
, const char *name
)
567 init_monthnames(ctl
);
568 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++)
569 if (strcasecmp(ctl
->full_month
[i
], name
) == 0)
572 init_abbr_monthnames(ctl
);
573 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++)
574 if (strcasecmp(ctl
->abbr_month
[i
], name
) == 0)
580 static void headers_init(struct cal_control
*ctl
)
583 char *cur_dh
= day_headings
;
584 char tmp
[FMT_ST_CHARS
];
587 year_len
= snprintf(tmp
, sizeof(tmp
), "%04d", ctl
->req
.year
);
589 if (year_len
< 0 || (size_t)year_len
>= sizeof(tmp
)) {
590 /* XXX impossible error */
594 for (i
= 0; i
< DAYS_IN_WEEK
; i
++) {
596 wd
= (i
+ ctl
->weekstart
) % DAYS_IN_WEEK
;
599 strcat(cur_dh
++, " ");
600 space_left
= sizeof(day_headings
) - (cur_dh
- day_headings
);
602 if (space_left
<= (ctl
->day_width
- 1))
604 cur_dh
+= center_str(nl_langinfo(ABDAY_1
+ wd
), cur_dh
,
605 space_left
, ctl
->day_width
- 1);
608 init_monthnames(ctl
);
610 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++) {
611 /* The +1 after year_len is space in between month and year. */
612 if (ctl
->week_width
< strlen(ctl
->full_month
[i
]) + year_len
+ 1)
613 ctl
->header_hint
= 1;
617 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
)
619 int first_week_day
= day_in_week(1, month
->month
, month
->year
);
621 int i
, j
, weeklines
= 0;
624 j
= day_in_year(1, month
->month
, month
->year
);
627 month_days
= j
+ days_in_month
[leap_year(month
->year
)][month
->month
];
629 /* True when Sunday is not first day in the output week. */
630 if (ctl
->weekstart
) {
631 first_week_day
-= ctl
->weekstart
;
632 if (first_week_day
< 0)
633 first_week_day
= DAYS_IN_WEEK
- ctl
->weekstart
;
634 month_days
+= ctl
->weekstart
- 1;
637 /* Fill day array. */
638 for (i
= 0; i
< MAXDAYS
; i
++) {
639 if (0 < first_week_day
) {
640 month
->days
[i
] = SPACE
;
644 if (j
< month_days
) {
645 if (month
->year
== REFORMATION_YEAR
&& month
->month
== REFORMATION_MONTH
&& (j
== 3 || j
== 247))
646 j
+= NUMBER_MISSING_DAYS
;
651 month
->days
[i
] = SPACE
;
655 /* Add week numbers */
657 int weeknum
= week_number(1, month
->month
, month
->year
, ctl
);
658 weeklines
= MAXDAYS
/ DAYS_IN_WEEK
- weeklines
/ DAYS_IN_WEEK
;
659 for (i
= 0; i
< MAXDAYS
/ DAYS_IN_WEEK
; i
++) {
662 weeknum
= week_number(month
->days
[i
* DAYS_IN_WEEK
], month
->month
, month
->year
, ctl
);
663 month
->weeks
[i
] = weeknum
++;
665 month
->weeks
[i
] = SPACE
;
671 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
)
673 char out
[FMT_ST_CHARS
];
676 if (ctl
->header_hint
|| ctl
->header_year
) {
677 for (i
= month
; i
; i
= i
->next
) {
678 sprintf(out
, _("%s"), ctl
->full_month
[i
->month
- 1]);
679 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
681 if (!ctl
->header_year
) {
683 for (i
= month
; i
; i
= i
->next
) {
684 sprintf(out
, _("%04d"), i
->year
);
685 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
689 for (i
= month
; i
; i
= i
->next
) {
690 sprintf(out
, _("%s %04d"), ctl
->full_month
[i
->month
- 1], i
->year
);
691 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
695 for (i
= month
; i
; i
= i
->next
) {
698 sprintf(out
, "%*s%s", (int)ctl
->day_width
- 1, "", day_headings
);
700 sprintf(out
, "%*s%s", (int)ctl
->day_width
, "", day_headings
);
703 my_putstring(day_headings
);
704 if (i
->next
!= NULL
) {
705 sprintf(out
, "%*s", ctl
->gutter_width
, "");
712 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
)
714 char out
[FMT_ST_CHARS
];
715 int reqday
, week_line
, d
;
719 for (week_line
= 0; week_line
< MAXDAYS
/ DAYS_IN_WEEK
; week_line
++) {
720 for (i
= month
; i
; i
= i
->next
) {
721 /* Determine the day that should be highlighted. */
723 if (i
->month
== ctl
->req
.month
&& i
->year
== ctl
->req
.year
) {
725 reqday
= ctl
->req
.day
;
728 ctl
->req
.day
+ 1 - day_in_year(1, i
->month
,
733 if (0 < i
->weeks
[week_line
]) {
734 if ((ctl
->weektype
& WEEK_NUM_MASK
) ==
736 sprintf(out
, "%s%2d%s", Senter
, i
->weeks
[week_line
],
739 sprintf(out
, "%2d", i
->weeks
[week_line
]);
741 sprintf(out
, "%2s", "");
743 skip
= ctl
->day_width
;
745 /* First day of the week is one char narrower than the other days,
746 * unless week number is printed. */
747 skip
= ctl
->day_width
- 1;
749 for (d
= DAYS_IN_WEEK
* week_line
;
750 d
< DAYS_IN_WEEK
* week_line
+ DAYS_IN_WEEK
; d
++) {
751 if (0 < i
->days
[d
]) {
752 if (reqday
== i
->days
[d
])
753 sprintf(out
, "%*s%s%*d%s", skip
- (ctl
->julian
? 3 : 2),
754 "", Senter
, (ctl
->julian
? 3 : 2),
757 sprintf(out
, "%*d", skip
, i
->days
[d
]);
759 sprintf(out
, "%*s", skip
, "");
761 if (skip
< (int)ctl
->day_width
)
764 if (i
->next
!= NULL
) {
765 sprintf(out
, "%*s", ctl
->gutter_width
, "");
770 int extra
= ctl
->num_months
> 3 ? 0 : 1;
771 sprintf(out
, "%*s\n", ctl
->gutter_width
- extra
, "");
777 static void monthly(const struct cal_control
*ctl
)
779 struct cal_month m1
,m2
,m3
, *m
;
780 int i
, rows
, new_month
, month
= ctl
->req
.start_month
? ctl
->req
.start_month
: ctl
->req
.month
;
781 int32_t year
= ctl
->req
.year
;
783 /* cal -3, cal -Y --span, etc. */
784 if (ctl
->span_months
) {
785 new_month
= month
- ctl
->num_months
/ 2;
787 month
= new_month
+ MONTHS_IN_YEAR
;
794 m1
.next
= (ctl
->months_in_row
> 1) ? &m2
: NULL
;
795 m2
.next
= (ctl
->months_in_row
> 2) ? &m3
: NULL
;
798 rows
= (ctl
->num_months
- 1) / ctl
->months_in_row
;
799 for (i
= 0; i
< rows
+ 1 ; i
++){
801 switch (ctl
->num_months
% ctl
->months_in_row
){
810 for (m
= &m1
; m
; m
= m
->next
){
813 if (MONTHS_IN_YEAR
< month
) {
817 cal_fill_month(m
, ctl
);
819 cal_output_header(&m1
, ctl
);
820 cal_output_months(&m1
, ctl
);
824 static void yearly(const struct cal_control
*ctl
)
826 char out
[FMT_ST_CHARS
];
829 year_width
+= (ctl
->week_width
+ 1) * (ctl
->julian
? 2 : 3);
833 if (ctl
->header_year
) {
834 sprintf(out
, "%04d", ctl
->req
.year
);
835 center(out
, year_width
, 0);
836 my_putstring("\n\n");
840 /* Is empty line at the end year output really needed? */
846 * return the 1 based day number within the year
848 static int day_in_year(int day
, int month
, int32_t year
)
852 leap
= leap_year(year
);
853 for (i
= 1; i
< month
; i
++)
854 day
+= days_in_month
[leap
][i
];
860 * return the 0 based day number for any date from 1 Jan. 1 to
861 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
862 * 3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
863 * during the period of 11 days.
865 static int day_in_week(int day
, int month
, int32_t year
)
867 static const int reform
[] = {
868 SUNDAY
, WEDNESDAY
, TUESDAY
, FRIDAY
, SUNDAY
, WEDNESDAY
,
869 FRIDAY
, MONDAY
, THURSDAY
, SATURDAY
, TUESDAY
, THURSDAY
871 static const int old
[] = {
872 FRIDAY
, MONDAY
, SUNDAY
, WEDNESDAY
, FRIDAY
, MONDAY
,
873 WEDNESDAY
, SATURDAY
, TUESDAY
, THURSDAY
, SUNDAY
, TUESDAY
875 if (year
!= REFORMATION_YEAR
+ 1)
876 year
-= month
< MARCH
;
878 year
-= (month
< MARCH
) + 14;
879 if (REFORMATION_YEAR
< year
880 || (year
== REFORMATION_YEAR
&& REFORMATION_MONTH
< month
)
881 || (year
== REFORMATION_YEAR
&& month
== REFORMATION_MONTH
&& 13 < day
)) {
882 int64_t long_year
= year
;
883 return (long_year
+ (year
/ 4) - (year
/ 100) + (year
/ 400) + reform
[month
- 1] +
886 if (year
< REFORMATION_YEAR
887 || (year
== REFORMATION_YEAR
&& month
< REFORMATION_MONTH
)
888 || (year
== REFORMATION_YEAR
&& month
== REFORMATION_MONTH
&& day
< 3))
889 return (year
+ year
/ 4 + old
[month
- 1] + day
) % DAYS_IN_WEEK
;
895 * return the week number of a given date, 1..54.
896 * Supports ISO-8601 and North American modes.
897 * Day may be given as Julian day of the year mode, in which
898 * case the month is disregarded entirely.
900 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
)
903 const int wday
= day_in_week(1, JANUARY
, year
);
905 if (ctl
->weektype
& WEEK_NUM_ISO
)
906 fday
= wday
+ (wday
>= FRIDAY
? -2 : 5);
908 /* WEEK_NUM_US: Jan 1 is always First week, that may
909 * begin previous year. That means there is very seldom
910 * more than 52 weeks, */
913 /* For julian dates the month can be set to 1, the global julian
914 * variable cannot be relied upon here, because we may recurse
915 * internally for 31.12. which would not work. */
916 if (day
> DAYS_IN_MONTH
)
919 yday
= day_in_year(day
,month
,year
);
920 if (year
== REFORMATION_YEAR
&& yday
>= YDAY_AFTER_MISSING
)
921 fday
-= NUMBER_MISSING_DAYS
;
923 /* Last year is last year */
924 if (yday
+ fday
< DAYS_IN_WEEK
)
925 return week_number(31, DECEMBER
, year
- 1, ctl
);
927 /* Or it could be part of the next year. The reformation year had less
928 * days than 365 making this check invalid, but reformation year ended
929 * on Sunday and in week 51, so it's ok here. */
930 if (ctl
->weektype
== WEEK_NUM_ISO
&& yday
>= 363
931 && day_in_week(day
, month
, year
) >= MONDAY
932 && day_in_week(day
, month
, year
) <= WEDNESDAY
933 && day_in_week(31, DECEMBER
, year
) >= MONDAY
934 && day_in_week(31, DECEMBER
, year
) <= WEDNESDAY
)
935 return week_number(1, JANUARY
, year
+ 1, ctl
);
937 return (yday
+ fday
) / DAYS_IN_WEEK
;
942 * return the yday of the first day in a given week inside
943 * the given year. This may be something other than Monday
944 * for ISO-8601 modes. For North American numbering this
945 * always returns a Sunday.
947 static int week_to_day(const struct cal_control
*ctl
)
951 wday
= day_in_week(1, JANUARY
, ctl
->req
.year
);
952 yday
= ctl
->req
.week
* DAYS_IN_WEEK
- wday
;
954 if (ctl
->weektype
& WEEK_NUM_ISO
)
955 yday
-= (wday
>= FRIDAY
? -2 : 5);
957 yday
-= (wday
== SUNDAY
? 6 : -1); /* WEEK_NUM_US */
965 * Center string, handling multibyte characters appropriately.
966 * In addition if the string is too large for the width it's truncated.
967 * The number of trailing spaces may be 1 less than the number of leading spaces.
969 static int center_str(const char* src
, char* dest
,
970 size_t dest_size
, size_t width
)
972 return mbsalign(src
, dest
, dest_size
, &width
,
973 MBS_ALIGN_CENTER
, MBA_UNIBYTE_FALLBACK
);
976 static void center(const char *str
, size_t len
, int separate
)
978 char lineout
[FMT_ST_CHARS
];
980 center_str(str
, lineout
, ARRAY_SIZE(lineout
), len
);
981 my_putstring(lineout
);
984 snprintf(lineout
, sizeof(lineout
), "%*s", separate
, "");
985 my_putstring(lineout
);
989 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
991 fputs(USAGE_HEADER
, out
);
992 fprintf(out
, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name
);
993 fprintf(out
, _(" %s [options] <timestamp|monthname>\n"), program_invocation_short_name
);
995 fputs(USAGE_SEPARATOR
, out
);
996 fputs(_("Display a calendar, or some part of it.\n"), out
);
997 fputs(_("Without any arguments, display the current month.\n"), out
);
999 fputs(USAGE_OPTIONS
, out
);
1000 fputs(_(" -1, --one show only a single month (default)\n"), out
);
1001 fputs(_(" -3, --three show three months spanning the date\n"), out
);
1002 fputs(_(" -n, --months <num> show num months starting with date's month\n"), out
);
1003 fputs(_(" -S, --span span the date when displaying multiple months\n"), out
);
1004 fputs(_(" -s, --sunday Sunday as first day of week\n"), out
);
1005 fputs(_(" -m, --monday Monday as first day of week\n"), out
);
1006 fputs(_(" -j, --julian output Julian dates\n"), out
);
1007 fputs(_(" -y, --year show the whole year\n"), out
);
1008 fputs(_(" -Y, --twelve show the next twelve months\n"), out
);
1009 fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out
);
1010 fputs(_(" --color[=<when>] colorize messages (auto, always or never)\n"), out
);
1012 " %s\n", USAGE_COLORS_DEFAULT
);
1014 fputs(USAGE_SEPARATOR
, out
);
1015 fputs(USAGE_HELP
, out
);
1016 fputs(USAGE_VERSION
, out
);
1017 fprintf(out
, USAGE_MAN_TAIL("cal(1)"));
1019 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);