]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/cal.c
cal: fix julian calendars for large years
[thirdparty/util-linux.git] / misc-utils / cal.c
index f73cbbf30ef48ba589d56bde5466dcc04e67d0a1..a854eaf9d073382d277bce99ea608746eeb0d064 100644 (file)
@@ -133,6 +133,14 @@ static const char *my_tgetstr(char *ss
 
 #include "widechar.h"
 
+enum {
+       GREGORIAN               = INT32_MIN,
+       ISO                     = INT32_MIN,
+       GB1752                  = 1752,
+       DEFAULT_REFORM_YEAR     = 1752,
+       JULIAN                  = INT32_MAX
+};
+
 enum {
        SUNDAY = 0,
        MONDAY,
@@ -160,7 +168,6 @@ enum {
        DECEMBER
 };
 
-#define REFORMATION_YEAR       1752            /* Signed-off-by: Lord Chesterfield */
 #define REFORMATION_MONTH      SEPTEMBER
 #define        NUMBER_MISSING_DAYS     11              /* 11 day correction */
 #define YDAY_AFTER_MISSING     258             /* 14th in Sep 1752 */
@@ -206,6 +213,7 @@ struct cal_control {
        const char *full_month[MONTHS_IN_YEAR]; /* month names */
        const char *abbr_month[MONTHS_IN_YEAR]; /* abbreviated month names */
 
+       int reform_year;                /* Gregorian reform year */
        int colormode;                  /* day and week number highlight */
        int num_months;                 /* number of requested months */
        int span_months;                /* span the date */
@@ -230,7 +238,7 @@ struct cal_month {
 };
 
 /* function prototypes */
-static int leap_year(int32_t year);
+static int leap_year(const struct cal_control *ctl, int32_t year);
 static int monthname_to_number(struct cal_control *ctl, const char *name);
 static void headers_init(struct cal_control *ctl);
 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl);
@@ -238,12 +246,15 @@ static void cal_output_header(struct cal_month *month, const struct cal_control
 static void cal_output_months(struct cal_month *month, const struct cal_control *ctl);
 static void monthly(const struct cal_control *ctl);
 static void yearly(const struct cal_control *ctl);
-static int day_in_year(int day, int month, int32_t year);
-static int day_in_week(int day, int month, int32_t year);
+static int day_in_year(const struct cal_control *ctl, int day,
+                      int month, int32_t year);
+static int day_in_week(const struct cal_control *ctl, int day,
+                      int month, int32_t year);
 static int week_number(int day, int month, int32_t year, const struct cal_control *ctl);
 static int week_to_day(const struct cal_control *ctl);
 static int center_str(const char *src, char *dest, size_t dest_size, size_t width);
 static void center(const char *str, size_t len, int separate);
+static int parse_reform_year(const char *reform_year);
 static void __attribute__((__noreturn__)) usage(void);
 
 int main(int argc, char **argv)
@@ -252,7 +263,9 @@ int main(int argc, char **argv)
        char *term;
        time_t now;
        int ch = 0, yflag = 0, Yflag = 0;
+
        static struct cal_control ctl = {
+               .reform_year = DEFAULT_REFORM_YEAR,
                .weekstart = SUNDAY,
                .num_months = 1,                /* default is "cal -1" */
                .span_months = 0,
@@ -265,7 +278,9 @@ int main(int argc, char **argv)
        };
 
        enum {
-               OPT_COLOR = CHAR_MAX + 1
+               OPT_COLOR = CHAR_MAX + 1,
+               OPT_ISO,
+               OPT_REFORM
        };
 
        static const struct option longopts[] = {
@@ -279,6 +294,8 @@ int main(int argc, char **argv)
                {"year", no_argument, NULL, 'y'},
                {"week", optional_argument, NULL, 'w'},
                {"color", optional_argument, NULL, OPT_COLOR},
+               {"reform", required_argument, NULL, OPT_REFORM},
+               {"iso", no_argument, NULL, OPT_ISO},
                {"version", no_argument, NULL, 'V'},
                {"twelve", no_argument, NULL, 'Y'},
                {"help", no_argument, NULL, 'h'},
@@ -335,7 +352,8 @@ int main(int argc, char **argv)
                val.string = nl_langinfo(_NL_TIME_WEEK_1STDAY);
 
                wfd = val.word;
-               wfd = day_in_week(wfd % 100, (wfd / 100) % 100, wfd / (100 * 100));
+               wfd = day_in_week(&ctl, wfd % 100, (wfd / 100) % 100,
+                                 wfd / (100 * 100));
                ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK;
        }
 #endif
@@ -390,6 +408,12 @@ int main(int argc, char **argv)
                                ctl.colormode = colormode_or_err(optarg,
                                                _("unsupported color mode"));
                        break;
+               case OPT_REFORM:
+                       ctl.reform_year = parse_reform_year(optarg);
+                       break;
+               case OPT_ISO:
+                       ctl.reform_year = ISO;
+                       break;
                case 'V':
                        printf(UTIL_LINUX_VERSION);
                        return EXIT_SUCCESS;
@@ -448,13 +472,15 @@ int main(int argc, char **argv)
                ctl.req.year = strtos32_or_err(*argv++, _("illegal year value"));
                if (ctl.req.year < SMALLEST_YEAR)
                        errx(EXIT_FAILURE, _("illegal year value: use positive integer"));
-               if (ctl.req.year == INT32_MAX)
+               if (ctl.req.year == JULIAN)
                        errx(EXIT_FAILURE, _("illegal year value"));
                if (ctl.req.day) {
-                       int dm = days_in_month[leap_year(ctl.req.year)][ctl.req.month];
+                       int dm = days_in_month[leap_year(&ctl, ctl.req.year)]
+                                             [ctl.req.month];
                        if (ctl.req.day > dm)
                                errx(EXIT_FAILURE, _("illegal day value: use 1-%d"), dm);
-                       ctl.req.day = day_in_year(ctl.req.day, ctl.req.month, ctl.req.year);
+                       ctl.req.day = day_in_year(&ctl, ctl.req.day,
+                                                 ctl.req.month, ctl.req.year);
                } else if ((int32_t) (local_time->tm_year + 1900) == ctl.req.year) {
                        ctl.req.day = local_time->tm_yday + 1;
                }
@@ -476,7 +502,7 @@ int main(int argc, char **argv)
 
        if (0 < ctl.req.week) {
                int yday = week_to_day(&ctl);
-               int leap = leap_year(ctl.req.year);
+               int leap = leap_year(&ctl, ctl.req.year);
                int m = 1;
 
                if (yday < 1)
@@ -532,9 +558,9 @@ int main(int argc, char **argv)
 }
 
 /* leap year -- account for gregorian reformation in 1752 */
-static int leap_year(int32_t year)
+static int leap_year(const struct cal_control *ctl, int32_t year)
 {
-       if (year <= REFORMATION_YEAR)
+       if (year <= ctl->reform_year)
                return !(year % 4);
        else
                return ( !(year % 4) && (year % 100) ) || !(year % 400);
@@ -618,15 +644,15 @@ static void headers_init(struct cal_control *ctl)
 
 static void cal_fill_month(struct cal_month *month, const struct cal_control *ctl)
 {
-       int first_week_day = day_in_week(1, month->month, month->year);
+       int first_week_day = day_in_week(ctl, 1, month->month, month->year);
        int month_days;
        int i, j, weeklines = 0;
 
        if (ctl->julian)
-               j = day_in_year(1, month->month, month->year);
+               j = day_in_year(ctl, 1, month->month, month->year);
        else
                j = 1;
-       month_days = j + days_in_month[leap_year(month->year)][month->month];
+       month_days = j + days_in_month[leap_year(ctl, month->year)][month->month];
 
        /* True when Sunday is not first day in the output week. */
        if (ctl->weekstart) {
@@ -644,7 +670,9 @@ static void cal_fill_month(struct cal_month *month, const struct cal_control *ct
                        continue;
                }
                if (j < month_days) {
-                       if (month->year == REFORMATION_YEAR && month->month == REFORMATION_MONTH && (j == 3 || j == 247))
+                       if (month->year == ctl->reform_year &&
+                           month->month == REFORMATION_MONTH &&
+                           (j == 3 || j == 247))
                                j += NUMBER_MISSING_DAYS;
                        month->days[i] = j;
                        j++;
@@ -726,9 +754,9 @@ static void cal_output_months(struct cal_month *month, const struct cal_control
                                if (ctl->julian)
                                        reqday = ctl->req.day;
                                else
-                                       reqday =
-                                           ctl->req.day + 1 - day_in_year(1, i->month,
-                                                                          i->year);
+                                       reqday = ctl->req.day + 1 -
+                                                day_in_year(ctl, 1, i->month,
+                                                            i->year);
                        }
 
                        if (ctl->weektype) {
@@ -847,11 +875,12 @@ static void yearly(const struct cal_control *ctl)
  * day_in_year --
  *     return the 1 based day number within the year
  */
-static int day_in_year(int day, int month, int32_t year)
+static int day_in_year(const struct cal_control *ctl,
+                      int day, int month, int32_t year)
 {
        int i, leap;
 
-       leap = leap_year(year);
+       leap = leap_year(ctl, year);
        for (i = 1; i < month; i++)
                day += days_in_month[leap][i];
        return day;
@@ -864,31 +893,46 @@ static int day_in_year(int day, int month, int32_t year)
  *     3 Sep. 1752 through 13 Sep. 1752, and returns invalid weekday
  *     during the period of 11 days.
  */
-static int day_in_week(int day, int month, int32_t year)
+static int day_in_week(const struct cal_control *ctl, int day,
+                      int month, int32_t year)
 {
-       static const int reform[] = {
-               SUNDAY, WEDNESDAY, TUESDAY, FRIDAY, SUNDAY, WEDNESDAY,
-               FRIDAY, MONDAY, THURSDAY, SATURDAY, TUESDAY, THURSDAY
-       };
-       static const int old[] = {
-               FRIDAY, MONDAY, SUNDAY, WEDNESDAY, FRIDAY, MONDAY,
-               WEDNESDAY, SATURDAY, TUESDAY, THURSDAY, SUNDAY, TUESDAY
-       };
-       if (year != REFORMATION_YEAR + 1)
+       /*
+       * The magic constants in the reform[] array are, in a simplified
+       * sense, the remaining days after slicing into one week periods the total
+       * days from the beginning of the year to the target month. That is,
+       * weeks + reform[] days gets us to the target month. The exception is,
+       * that for the months past February 'DOY - 1' must be used.
+       *
+       *   DoY (Day of Year): total days to the target month
+       *
+       *   Month            1  2  3  4   5   6   7   8   9  10  11  12
+       *   DoY              0 31 59 90 120 151 181 212 243 273 304 334
+       *   DoY % 7          0  3
+       *   DoY - 1 % 7      - --  2  5   0   3   5   1   4   6   2   4
+       *       reform[] = { 0, 3, 2, 5,  0,  3,  5,  1,  4,  6,  2,  4 };
+       *
+       *  Note: these calculations are for non leap years.
+       */
+       static const int reform[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
+       static const int old[]    = { 5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2 };
+
+       if (year != ctl->reform_year + 1)
                year -= month < MARCH;
        else
                year -= (month < MARCH) + 14;
-       if (REFORMATION_YEAR < year
-           || (year == REFORMATION_YEAR && REFORMATION_MONTH < month)
-           || (year == REFORMATION_YEAR && month == REFORMATION_MONTH && 13 < day)) {
-               int64_t long_year = year;
-               return (long_year + (year / 4) - (year / 100) + (year / 400) + reform[month - 1] +
-                       day) % DAYS_IN_WEEK;
+       if (ctl->reform_year < year
+           || (year == ctl->reform_year && REFORMATION_MONTH < month)
+           || (year == ctl->reform_year
+               && month == REFORMATION_MONTH && 13 < day)) {
+               return ((int64_t) year + (year / 4)
+                       - (year / 100) + (year / 400)
+                       + reform[month - 1] + day) % DAYS_IN_WEEK;
        }
-       if (year < REFORMATION_YEAR
-           || (year == REFORMATION_YEAR && month < REFORMATION_MONTH)
-           || (year == REFORMATION_YEAR && month == REFORMATION_MONTH && day < 3))
-               return (year + year / 4 + old[month - 1] + day) % DAYS_IN_WEEK;
+       if (year < ctl->reform_year
+           || (year == ctl->reform_year && month < REFORMATION_MONTH)
+           || (year == ctl->reform_year && month == REFORMATION_MONTH && day < 3))
+               return ((int64_t) year + year / 4 + old[month - 1] + day)
+                       % DAYS_IN_WEEK;
        return NONEDAY;
 }
 
@@ -902,7 +946,7 @@ static int day_in_week(int day, int month, int32_t year)
 static int week_number(int day, int month, int32_t year, const struct cal_control *ctl)
 {
        int fday = 0, yday;
-       const int wday = day_in_week(1, JANUARY, year);
+       const int wday = day_in_week(ctl, 1, JANUARY, year);
 
        if (ctl->weektype & WEEK_NUM_ISO)
                fday = wday + (wday >= FRIDAY ? -2 : 5);
@@ -918,8 +962,8 @@ static int week_number(int day, int month, int32_t year, const struct cal_contro
        if (day > DAYS_IN_MONTH)
                month = JANUARY;
 
-       yday = day_in_year(day,month,year);
-       if (year == REFORMATION_YEAR && yday >= YDAY_AFTER_MISSING)
+       yday = day_in_year(ctl, day, month, year);
+       if (year == ctl->reform_year && yday >= YDAY_AFTER_MISSING)
                fday -= NUMBER_MISSING_DAYS;
 
        /* Last year is last year */
@@ -930,10 +974,10 @@ static int week_number(int day, int month, int32_t year, const struct cal_contro
         * days than 365 making this check invalid, but reformation year ended
         * on Sunday and in week 51, so it's ok here. */
        if (ctl->weektype == WEEK_NUM_ISO && yday >= 363
-           && day_in_week(day, month, year) >= MONDAY
-           && day_in_week(day, month, year) <= WEDNESDAY
-           && day_in_week(31, DECEMBER, year) >= MONDAY
-           && day_in_week(31, DECEMBER, year) <= WEDNESDAY)
+           && day_in_week(ctl, day, month, year) >= MONDAY
+           && day_in_week(ctl, day, month, year) <= WEDNESDAY
+           && day_in_week(ctl, 31, DECEMBER, year) >= MONDAY
+           && day_in_week(ctl, 31, DECEMBER, year) <= WEDNESDAY)
                return week_number(1, JANUARY, year + 1, ctl);
 
        return (yday + fday) / DAYS_IN_WEEK;
@@ -950,7 +994,7 @@ static int week_to_day(const struct cal_control *ctl)
 {
        int yday, wday;
 
-       wday = day_in_week(1, JANUARY, ctl->req.year);
+       wday = day_in_week(ctl, 1, JANUARY, ctl->req.year);
        yday = ctl->req.week * DAYS_IN_WEEK - wday;
 
        if (ctl->weektype & WEEK_NUM_ISO)
@@ -988,6 +1032,30 @@ static void center(const char *str, size_t len, int separate)
        }
 }
 
+static int parse_reform_year(const char *reform_year)
+{
+       size_t i;
+
+       struct reform {
+               char *name;
+               int val;
+       };
+
+       struct reform years[] = {
+       {"gregorian",   GREGORIAN},
+       {"iso",         ISO},
+       {"1752",        GB1752},
+       {"julian",      JULIAN},
+       };
+
+       for (i = 0; i < ARRAY_SIZE(years); i++) {
+               if (strcasecmp(reform_year, years[i].name) == 0) {
+                       return years[i].val;
+               }
+       }
+       errx(EXIT_FAILURE, "invalid --reform value: '%s'", reform_year);
+}
+
 static void __attribute__((__noreturn__)) usage(void)
 {
        FILE *out = stdout;
@@ -1006,7 +1074,9 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -S, --span            span the date when displaying multiple months\n"), out);
        fputs(_(" -s, --sunday          Sunday as first day of week\n"), out);
        fputs(_(" -m, --monday          Monday as first day of week\n"), out);
-       fputs(_(" -j, --julian          output Julian dates\n"), out);
+       fputs(_(" -j, --julian          use day-of-year for all calendars\n"), out);
+       fputs(_("     --reform <val>    Gregorian reform date (1752|gregorian|iso|julian)\n"), out);
+       fputs(_("     --iso             alias for --reform=iso\n"), out);
        fputs(_(" -y, --year            show the whole year\n"), out);
        fputs(_(" -Y, --twelve          show the next twelve months\n"), out);
        fputs(_(" -w, --week[=<num>]    show US or ISO-8601 week numbers\n"), out);
@@ -1015,9 +1085,8 @@ static void __attribute__((__noreturn__)) usage(void)
                "                         %s\n", USAGE_COLORS_DEFAULT);
 
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
-       fprintf(out, USAGE_MAN_TAIL("cal(1)"));
+       printf(USAGE_HELP_OPTIONS(23));
+       printf(USAGE_MAN_TAIL("cal(1)"));
 
        exit(EXIT_SUCCESS);
 }