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},
204 WEEK_NUM_DISABLED
= 0,
210 /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */
211 static char day_headings
[(WEEK_LEN
+ 1) * 6 + 1];
221 const char *full_month
[MONTHS_IN_YEAR
]; /* month names */
222 int colormode
; /* day and week number highlight */
223 int num_months
; /* number of months horizontally in print out */
224 int weekstart
; /* day the week starts, often Sun or Mon */
225 int weektype
; /* WEEK_TYPE_{NONE,ISO,US} */
226 size_t day_width
; /* day width in characters in printout */
227 size_t week_width
; /* 7 * day_width + possible week num */
228 int gutter_width
; /* spaces in between horizontal month outputs */
229 struct cal_request req
; /* the times user is interested */
230 unsigned int julian
:1, /* julian output */
231 yflag
:1, /* print whole year */
232 header_hint
:1; /* does month name + year need two lines to fit */
236 int days
[MAXDAYS
]; /* the day numbers, or SPACE */
237 int weeks
[MAXDAYS
/ DAYS_IN_WEEK
];
240 struct cal_month
*next
;
243 /* function prototypes */
244 static int leap_year(int32_t year
);
245 static void headers_init(struct cal_control
*ctl
);
246 static void set_consecutive_months(struct cal_month
*month
, int m
, int32_t y
);
247 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
);
248 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
);
249 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
);
250 static void monthly(const struct cal_control
*ctl
);
251 static void monthly3(const struct cal_control
*ctl
);
252 static void yearly(const struct cal_control
*ctl
);
253 static int day_in_year(int day
, int month
, int32_t year
);
254 static int day_in_week(int day
, int month
, int32_t year
);
255 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
);
256 static int week_to_day(const struct cal_control
*ctl
);
257 static int center_str(const char *src
, char *dest
, size_t dest_size
, size_t width
);
258 static void center(const char *str
, size_t len
, int separate
);
259 static void __attribute__((__noreturn__
)) usage(FILE *out
);
261 int main(int argc
, char **argv
)
263 struct tm
*local_time
;
266 static struct cal_control ctl
= {
268 .num_months
= NUM_MONTHS
,
269 .colormode
= UL_COLORMODE_UNDEF
,
270 .weektype
= WEEK_NUM_DISABLED
,
271 .day_width
= DAY_LEN
,
278 OPT_COLOR
= CHAR_MAX
+ 1
281 static const struct option longopts
[] = {
282 {"one", no_argument
, NULL
, '1'},
283 {"three", no_argument
, NULL
, '3'},
284 {"sunday", no_argument
, NULL
, 's'},
285 {"monday", no_argument
, NULL
, 'm'},
286 {"julian", no_argument
, NULL
, 'j'},
287 {"year", no_argument
, NULL
, 'y'},
288 {"week", optional_argument
, NULL
, 'w'},
289 {"color", optional_argument
, NULL
, OPT_COLOR
},
290 {"version", no_argument
, NULL
, 'V'},
291 {"help", no_argument
, NULL
, 'h'},
295 setlocale(LC_ALL
, "");
296 bindtextdomain(PACKAGE
, LOCALEDIR
);
298 atexit(close_stdout
);
300 #if defined(HAVE_LIBNCURSES) || defined(HAVE_LIBNCURSESW) || defined(HAVE_LIBTERMCAP)
301 if ((term
= getenv("TERM"))) {
303 my_setupterm(term
, STDOUT_FILENO
, &ret
);
305 Senter
= my_tgetstr("so","smso");
306 Sexit
= my_tgetstr("se","rmso");
307 Slen
= strlen(Senter
) + strlen(Sexit
);
313 * The traditional Unix cal utility starts the week at Sunday,
314 * while ISO 8601 starts at Monday. We read the start day from
315 * the locale database, which can be overridden with the
316 * -s (Sunday) or -m (Monday) options.
318 #if HAVE_DECL__NL_TIME_WEEK_1STDAY
320 * You need to use 2 locale variables to get the first day of the week.
321 * This is needed to support first_weekday=2 and first_workday=1 for
322 * the rare case where working days span across 2 weeks.
323 * This shell script shows the combinations and calculations involved:
325 * for LANG in en_US ru_RU fr_FR csb_PL POSIX; do
326 * printf "%s:\t%s + %s -1 = " $LANG $(locale week-1stday first_weekday)
327 * date -d"$(locale week-1stday) +$(($(locale first_weekday)-1))day" +%w
330 * en_US: 19971130 + 1 -1 = 0 #0 = sunday
331 * ru_RU: 19971130 + 2 -1 = 1
332 * fr_FR: 19971201 + 1 -1 = 1
333 * csb_PL: 19971201 + 2 -1 = 2
334 * POSIX: 19971201 + 7 -1 = 0
338 union { unsigned int word
; char *string
; } val
;
339 val
.string
= nl_langinfo(_NL_TIME_WEEK_1STDAY
);
342 wfd
= day_in_week(wfd
% 100, (wfd
/ 100) % 100, wfd
/ (100 * 100));
343 ctl
.weekstart
= (wfd
+ *nl_langinfo(_NL_TIME_FIRST_WEEKDAY
) - 1) % DAYS_IN_WEEK
;
347 while ((ch
= getopt_long(argc
, argv
, "13mjsywVh", longopts
, NULL
)) != -1)
350 ctl
.num_months
= 1; /* default */
356 ctl
.weekstart
= SUNDAY
; /* default */
359 ctl
.weekstart
= MONDAY
;
363 ctl
.day_width
= DAY_LEN
+ 1;
370 ctl
.req
.week
= strtos32_or_err(optarg
,
371 _("invalid week argument"));
372 if (ctl
.req
.week
< 1 || 53 < ctl
.req
.week
)
373 errx(EXIT_FAILURE
,_("illegal week value: use 1-53"));
375 ctl
.weektype
= WEEK_NUM_US
; /* default per weekstart */
379 ctl
.colormode
= colormode_or_err(optarg
,
380 _("unsupported color mode"));
383 printf(UTIL_LINUX_VERSION
);
395 ctl
.weektype
= ctl
.req
.week
& WEEK_NUM_MASK
;
396 ctl
.weektype
|= (ctl
.weekstart
== MONDAY
? WEEK_NUM_ISO
: WEEK_NUM_US
);
397 ctl
.week_width
= (ctl
.day_width
* DAYS_IN_WEEK
) + WNUM_LEN
;
399 ctl
.week_width
= ctl
.day_width
* DAYS_IN_WEEK
;
402 local_time
= localtime(&now
);
406 ctl
.req
.day
= strtos32_or_err(*argv
++, _("illegal day value"));
407 if (ctl
.req
.day
< 1 || DAYS_IN_MONTH
< ctl
.req
.day
)
408 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), DAYS_IN_MONTH
);
411 ctl
.req
.month
= strtos32_or_err(*argv
++, _("illegal month value: use 1-12"));
412 if (ctl
.req
.month
< 1 || MONTHS_IN_YEAR
< ctl
.req
.month
)
413 errx(EXIT_FAILURE
, _("illegal month value: use 1-12"));
416 ctl
.req
.year
= strtos32_or_err(*argv
++, _("illegal year value"));
417 if (ctl
.req
.year
< SMALLEST_YEAR
)
418 errx(EXIT_FAILURE
, _("illegal year value: use positive integer"));
419 if (ctl
.req
.year
== INT32_MAX
)
420 errx(EXIT_FAILURE
, _("illegal year value"));
422 int dm
= days_in_month
[leap_year(ctl
.req
.year
)][ctl
.req
.month
];
423 if (ctl
.req
.day
> dm
)
424 errx(EXIT_FAILURE
, _("illegal day value: use 1-%d"), dm
);
425 ctl
.req
.day
= day_in_year(ctl
.req
.day
, ctl
.req
.month
, ctl
.req
.year
);
426 } else if ((int32_t) (local_time
->tm_year
+ 1900) == ctl
.req
.year
) {
427 ctl
.req
.day
= local_time
->tm_yday
+ 1;
429 if (!ctl
.req
.month
&& !ctl
.req
.week
) {
430 ctl
.req
.month
= local_time
->tm_mon
+ 1;
435 ctl
.req
.day
= local_time
->tm_yday
+ 1;
436 ctl
.req
.year
= local_time
->tm_year
+ 1900;
437 ctl
.req
.month
= local_time
->tm_mon
+ 1;
443 if (0 < ctl
.req
.week
) {
444 int yday
= week_to_day(&ctl
);
445 int leap
= leap_year(ctl
.req
.year
);
449 errx(EXIT_FAILURE
, _("illegal week value: year %d "
450 "doesn't have week %d"),
451 ctl
.req
.year
, ctl
.req
.week
);
452 while (m
<= 12 && yday
> days_in_month
[leap
][m
])
453 yday
-= days_in_month
[leap
][m
++];
455 /* In some years (e.g. 2010 in ISO mode) it's possible
456 * to have a remnant of week 53 starting the year yet
457 * the year in question ends during 52, in this case
458 * we're assuming that early remnant is being referred
459 * to if 53 is given as argument. */
460 if (ctl
.req
.week
!= week_number(31, 12, ctl
.req
.year
- 1, &ctl
))
462 _("illegal week value: year %d "
463 "doesn't have week %d"),
464 ctl
.req
.year
, ctl
.req
.week
);
467 ctl
.req
.month
= 12 < m
? 1 : m
;
472 if (!colors_init(ctl
.colormode
, "cal")) {
474 ctl
.weektype
&= ~WEEK_NUM_MASK
;
479 ctl
.num_months
= MONTH_COLS
- 1;
481 ctl
.num_months
= MONTH_COLS
;
482 ctl
.gutter_width
= 3;
484 } else if (ctl
.num_months
== 1)
486 else if (ctl
.num_months
== 3)
492 /* leap year -- account for gregorian reformation in 1752 */
493 static int leap_year(int32_t year
)
495 if (year
<= REFORMATION_YEAR
)
498 return ( !(year
% 4) && (year
% 100) ) || !(year
% 400);
501 static void headers_init(struct cal_control
*ctl
)
504 char *cur_dh
= day_headings
;
505 char tmp
[FMT_ST_CHARS
];
508 year_len
= snprintf(tmp
, sizeof(tmp
), "%d", ctl
->req
.year
);
510 for (i
= 0; i
< DAYS_IN_WEEK
; i
++) {
512 wd
= (i
+ ctl
->weekstart
) % DAYS_IN_WEEK
;
515 strcat(cur_dh
++, " ");
516 space_left
= sizeof(day_headings
) - (cur_dh
- day_headings
);
518 if (space_left
<= (ctl
->day_width
- 1))
520 cur_dh
+= center_str(nl_langinfo(ABDAY_1
+ wd
), cur_dh
,
521 space_left
, ctl
->day_width
- 1);
524 for (i
= 0; i
< MONTHS_IN_YEAR
; i
++) {
525 ctl
->full_month
[i
] = nl_langinfo(MON_1
+ i
);
526 /* The +1 after year_len is space in between month and year. */
527 if (ctl
->week_width
< strlen(ctl
->full_month
[i
]) + year_len
+ 1)
528 ctl
->header_hint
= 1;
532 static void set_consecutive_months(struct cal_month
*month
, int m
, int32_t y
)
535 for (i
= month
; i
; i
= i
->next
) {
538 if (MONTHS_IN_YEAR
< m
) {
545 static void cal_fill_month(struct cal_month
*month
, const struct cal_control
*ctl
)
547 int first_week_day
= day_in_week(1, month
->month
, month
->year
);
549 int i
, j
, weeklines
= 0;
552 j
= day_in_year(1, month
->month
, month
->year
);
555 month_days
= j
+ days_in_month
[leap_year(month
->year
)][month
->month
];
557 /* True when Sunday is not first day in the output week. */
558 if (ctl
->weekstart
) {
559 first_week_day
-= ctl
->weekstart
;
560 if (first_week_day
< 0)
561 first_week_day
= DAYS_IN_WEEK
- ctl
->weekstart
;
562 month_days
+= ctl
->weekstart
- 1;
565 /* Fill day array. */
566 for (i
= 0; i
< MAXDAYS
; i
++) {
567 if (0 < first_week_day
) {
568 month
->days
[i
] = SPACE
;
572 if (j
< month_days
) {
573 if (month
->year
== 1752 && month
->month
== 9 && (j
== 3 || j
== 247))
574 j
+= NUMBER_MISSING_DAYS
;
579 month
->days
[i
] = SPACE
;
583 /* Add week numbers */
585 int weeknum
= week_number(1, month
->month
, month
->year
, ctl
);
586 weeklines
= MAXDAYS
/ DAYS_IN_WEEK
- weeklines
/ DAYS_IN_WEEK
;
587 for (i
= 0; i
< MAXDAYS
/ DAYS_IN_WEEK
; i
++) {
589 month
->weeks
[i
] = weeknum
++;
591 month
->weeks
[i
] = SPACE
;
593 if (52 < weeknum
&& i
== 0)
594 weeknum
= week_number(month
->days
[DAYS_IN_WEEK
* (i
+ 1)], 1, month
->year
, ctl
);
595 else if (52 < weeknum
)
596 weeknum
= week_number(31, 12, month
->year
, ctl
);
601 static void cal_output_header(struct cal_month
*month
, const struct cal_control
*ctl
)
603 char out
[FMT_ST_CHARS
];
606 if (ctl
->header_hint
|| ctl
->yflag
) {
607 for (i
= month
; i
; i
= i
->next
) {
608 sprintf(out
, _("%s"), ctl
->full_month
[i
->month
- 1]);
609 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
613 for (i
= month
; i
; i
= i
->next
) {
614 sprintf(out
, _("%d"), i
->year
);
615 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
619 for (i
= month
; i
; i
= i
->next
) {
620 sprintf(out
, _("%s %d"), ctl
->full_month
[i
->month
- 1], i
->year
);
621 center(out
, ctl
->week_width
- 1, i
->next
== NULL
? 0 : ctl
->gutter_width
);
625 for (i
= month
; i
; i
= i
->next
) {
628 sprintf(out
, "%*s%s", (int)ctl
->day_width
- 1, "", day_headings
);
630 sprintf(out
, "%*s%s", (int)ctl
->day_width
, "", day_headings
);
633 my_putstring(day_headings
);
634 if (i
->next
!= NULL
) {
635 sprintf(out
, "%*s", ctl
->gutter_width
, "");
642 static void cal_output_months(struct cal_month
*month
, const struct cal_control
*ctl
)
644 char out
[FMT_ST_CHARS
];
645 int reqday
, week_line
, d
;
649 for (week_line
= 0; week_line
< MAXDAYS
/ DAYS_IN_WEEK
; week_line
++) {
650 for (i
= month
; i
; i
= i
->next
) {
651 /* Determine the day that should be highlighted. */
653 if (i
->month
== ctl
->req
.month
&& i
->year
== ctl
->req
.year
) {
655 reqday
= ctl
->req
.day
;
658 ctl
->req
.day
+ 1 - day_in_year(1, i
->month
,
663 if (0 < i
->weeks
[week_line
]) {
664 if ((ctl
->weektype
& WEEK_NUM_MASK
) ==
666 sprintf(out
, "%s%2d%s", Senter
, i
->weeks
[week_line
],
669 sprintf(out
, "%2d", i
->weeks
[week_line
]);
671 sprintf(out
, "%2s", "");
673 skip
= ctl
->day_width
;
675 /* First day of the week is one char narrower than the other days,
676 * unless week number is printed. */
677 skip
= ctl
->day_width
- 1;
679 for (d
= DAYS_IN_WEEK
* week_line
;
680 d
< DAYS_IN_WEEK
* week_line
+ DAYS_IN_WEEK
; d
++) {
681 if (0 < i
->days
[d
]) {
682 if (reqday
== i
->days
[d
])
683 sprintf(out
, "%*s%s%*d%s", skip
- (ctl
->julian
? 3 : 2),
684 "", Senter
, (ctl
->julian
? 3 : 2),
687 sprintf(out
, "%*d", skip
, i
->days
[d
]);
689 sprintf(out
, "%*s", skip
, "");
691 if (skip
< (int)ctl
->day_width
)
694 if (i
->next
!= NULL
) {
695 sprintf(out
, "%*s", ctl
->gutter_width
, "");
700 sprintf(out
, "%*s\n", ctl
->gutter_width
- (ctl
->yflag
? 0 : 1), "");
706 static void monthly(const struct cal_control
*ctl
)
708 struct cal_month month
;
710 month
.month
= ctl
->req
.month
;
711 month
.year
= ctl
->req
.year
;
714 cal_fill_month(&month
, ctl
);
716 cal_output_header(&month
, ctl
);
717 cal_output_months(&month
, ctl
);
720 static void monthly3(const struct cal_control
*ctl
)
722 struct cal_month m1
, m2
, m3
, *i
;
730 if (ctl
->req
.month
== 1) {
731 first_month
= MONTHS_IN_YEAR
;
732 first_year
= ctl
->req
.year
- 1;
734 first_month
= ctl
->req
.month
- 1;
735 first_year
= ctl
->req
.year
;
738 set_consecutive_months(&m1
, first_month
, first_year
);
739 for (i
= &m1
; i
; i
= i
->next
)
740 cal_fill_month(i
, ctl
);
741 cal_output_header(&m1
, ctl
);
742 cal_output_months(&m1
, ctl
);
745 static void yearly(const struct cal_control
*ctl
)
747 struct cal_month m1
, m2
, m3
, *i
;
749 char out
[FMT_ST_CHARS
];
761 for (i
= &m1
; i
; i
= i
->next
)
762 year_width
+= ctl
->week_width
+ 1;
765 sprintf(out
, "%d", ctl
->req
.year
);
766 center(out
, year_width
, 0);
767 my_putstring("\n\n");
769 for (month
= 1; month
< MONTHS_IN_YEAR
; month
+= ctl
->julian
? 2 : 3) {
770 set_consecutive_months(&m1
, month
, ctl
->req
.year
);
771 for (i
= &m1
; i
; i
= i
->next
)
772 cal_fill_month(i
, ctl
);
773 cal_output_header(&m1
, ctl
);
774 cal_output_months(&m1
, ctl
);
776 /* Is empty line at the end year output really needed? */
782 * return the 1 based day number within the year
784 static int day_in_year(int day
, int month
, int32_t year
)
788 leap
= leap_year(year
);
789 for (i
= 1; i
< month
; i
++)
790 day
+= days_in_month
[leap
][i
];
796 * return the 0 based day number for any date from 1 Jan. 1 to
797 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
798 * 3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
799 * during the period of 11 days.
801 static int day_in_week(int day
, int month
, int32_t year
)
803 static const int reform
[] = {
804 SUNDAY
, WEDNESDAY
, TUESDAY
, FRIDAY
, SUNDAY
, WEDNESDAY
,
805 FRIDAY
, MONDAY
, THURSDAY
, SATURDAY
, TUESDAY
, THURSDAY
807 static const int old
[] = {
808 FRIDAY
, MONDAY
, SUNDAY
, WEDNESDAY
, FRIDAY
, MONDAY
,
809 WEDNESDAY
, SATURDAY
, TUESDAY
, THURSDAY
, SUNDAY
, TUESDAY
814 year
-= (month
< 3) + 14;
815 if (REFORMATION_YEAR
< year
816 || (year
== REFORMATION_YEAR
&& 9 < month
)
817 || (year
== REFORMATION_YEAR
&& month
== 9 && 13 < day
))
818 return (year
+ (year
/ 4) - (year
/ 100) + (year
/ 400) + reform
[month
- 1] +
820 if (year
< REFORMATION_YEAR
821 || (year
== REFORMATION_YEAR
&& month
< 9)
822 || (year
== REFORMATION_YEAR
&& month
== 9 && day
< 3))
823 return (year
+ year
/ 4 + old
[month
- 1] + day
) % 7;
829 * return the week number of a given date, 1..53.
830 * Supports ISO-8601 and North American modes.
831 * Day may be given as Julian day of the year mode, in which
832 * case the month is disregarded entirely.
834 static int week_number(int day
, int month
, int32_t year
, const struct cal_control
*ctl
)
837 int wday
= day_in_week(1, 1, year
);
839 if (ctl
->weektype
& WEEK_NUM_ISO
)
840 fday
= wday
+ (wday
>= FRIDAY
? -2 : 5);
843 * - according to gcal, the first Sun is in the first week
844 * - according to wikipedia, the first Sat is in the first week
846 fday
= wday
+ (wday
== SUNDAY
? 6 : -1);
848 /* For julian dates the month can be set to 1, the global julian
849 * variable cannot be relied upon here, because we may recurse
850 * internally for 31.12. which would not work. */
854 yday
= day_in_year(day
,month
,year
);
855 if (year
== REFORMATION_YEAR
) {
856 if (yday
>= YDAY_AFTER_MISSING
)
857 fday
-= NUMBER_MISSING_DAYS
;
860 /* Last year is last year */
862 return week_number(31, 12, year
- 1, ctl
);
864 /* Or it could be part of the next year. The reformation year had less
865 * days than 365 making this check invalid, but reformation year ended
866 * on Sunday and in week 51, so it's ok here. */
867 if (ctl
->weektype
== WEEK_NUM_ISO
&& yday
>= 363
868 && day_in_week(day
, month
, year
) >= MONDAY
869 && day_in_week(day
, month
, year
) <= WEDNESDAY
870 && day_in_week(31, 12, year
) >= MONDAY
871 && day_in_week(31, 12, year
) <= WEDNESDAY
)
872 return week_number(1, 1, year
+ 1, ctl
);
874 return (yday
+ fday
) / 7;
879 * return the yday of the first day in a given week inside
880 * the given year. This may be something other than Monday
881 * for ISO-8601 modes. For North American numbering this
882 * always returns a Sunday.
884 static int week_to_day(const struct cal_control
*ctl
)
888 wday
= day_in_week(1, 1, ctl
->req
.year
);
889 yday
= ctl
->req
.week
* 7 - wday
;
891 if (ctl
->weektype
& WEEK_NUM_ISO
)
892 yday
-= (wday
>= FRIDAY
? -2 : 5);
894 yday
-= (wday
== SUNDAY
? 6 : -1); /* WEEK_NUM_US */
902 * Center string, handling multibyte characters appropriately.
903 * In addition if the string is too large for the width it's truncated.
904 * The number of trailing spaces may be 1 less than the number of leading spaces.
906 static int center_str(const char* src
, char* dest
,
907 size_t dest_size
, size_t width
)
909 return mbsalign(src
, dest
, dest_size
, &width
,
910 MBS_ALIGN_CENTER
, MBA_UNIBYTE_FALLBACK
);
913 static void center(const char *str
, size_t len
, int separate
)
915 char lineout
[FMT_ST_CHARS
];
917 center_str(str
, lineout
, ARRAY_SIZE(lineout
), len
);
918 my_putstring(lineout
);
921 snprintf(lineout
, sizeof(lineout
), "%*s", separate
, "");
922 my_putstring(lineout
);
926 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
928 fputs(USAGE_HEADER
, out
);
929 fprintf(out
, _(" %s [options] [[[day] month] year]\n"), program_invocation_short_name
);
931 fputs(USAGE_SEPARATOR
, out
);
932 fputs(_("Display a calendar, or some part of it.\n"), out
);
933 fputs(_("Without any arguments, display the current month.\n"), out
);
935 fputs(USAGE_OPTIONS
, out
);
936 fputs(_(" -1, --one show only a single month (default)\n"), out
);
937 fputs(_(" -3, --three show three months spanning the date\n"), out
);
938 fputs(_(" -s, --sunday Sunday as first day of week\n"), out
);
939 fputs(_(" -m, --monday Monday as first day of week\n"), out
);
940 fputs(_(" -j, --julian output Julian dates\n"), out
);
941 fputs(_(" -y, --year show the whole year\n"), out
);
942 fputs(_(" -w, --week[=<num>] show US or ISO-8601 week numbers\n"), out
);
943 fputs(_(" --color[=<when>] colorize messages (auto, always or never)\n"), out
);
945 fputs(USAGE_SEPARATOR
, out
);
946 fputs(USAGE_HELP
, out
);
947 fputs(USAGE_VERSION
, out
);
948 fprintf(out
, USAGE_MAN_TAIL("cal(1)"));
950 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);