* [v5 PATCH 0/3] Pull Request
@ 2018-01-16 21:37 J William Piggott
2018-01-16 21:46 ` [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct J William Piggott
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: J William Piggott @ 2018-01-16 21:37 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
The following changes since commit 7bd0c1ae18b1c9864b55c0362b737383e70f80c3:
include/debug: improve debug message (2018-01-12 13:46:49 +0100)
are available in the git repository at:
git@github.com:jwpi/util-linux.git 180115
for you to fetch changes up to a4b55ad0080d1024808a7e27a740c63bbef3464f:
cal: update man page (2018-01-16 13:51:43 -0500)
----------------------------------------------------------------
J William Piggott (3):
cal: move REFORMATION_YEAR to control struct
cal: add option to set Gregorian reform date
cal: update man page
misc-utils/cal.1 | 83 +++++++++++++++++++++++++++++----
misc-utils/cal.c | 137 +++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 171 insertions(+), 49 deletions(-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct
2018-01-16 21:37 [v5 PATCH 0/3] Pull Request J William Piggott
@ 2018-01-16 21:46 ` J William Piggott
2018-01-16 21:47 ` [v5 PATCH 2/3] cal: add option to set Gregorian reform date J William Piggott
2018-01-16 21:49 ` [v5 PATCH 3/3] cal: update man page J William Piggott
2 siblings, 0 replies; 5+ messages in thread
From: J William Piggott @ 2018-01-16 21:46 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
Signed-off-by: J William Piggott <elseifthen@gmx.com>
---
misc-utils/cal.c | 86 ++++++++++++++++++++++++++++++++------------------------
1 file changed, 49 insertions(+), 37 deletions(-)
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 39f2bdcba..ea582964c 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -160,7 +160,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 +205,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 +230,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,8 +238,10 @@ 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);
@@ -252,7 +254,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 = 1752,
.weekstart = SUNDAY,
.num_months = 1, /* default is "cal -1" */
.span_months = 0,
@@ -335,7 +339,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
@@ -451,10 +456,12 @@ int main(int argc, char **argv)
if (ctl.req.year == INT32_MAX)
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 +483,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 +539,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 +625,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 +651,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 +735,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 +856,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,7 +874,8 @@ 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)
{
/*
* The magic constants in the reform[] array are, in a simplified
@@ -886,20 +897,21 @@ static int day_in_week(int day, int month, int32_t year)
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 != REFORMATION_YEAR + 1)
+ 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)) {
+ if (ctl->reform_year < year
+ || (year == ctl->reform_year && REFORMATION_MONTH < month)
+ || (year == ctl->reform_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;
+ return (long_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))
+ if (year < ctl->reform_year
+ || (year == ctl->reform_year && month < REFORMATION_MONTH)
+ || (year == ctl->reform_year && month == REFORMATION_MONTH && day < 3))
return (year + year / 4 + old[month - 1] + day) % DAYS_IN_WEEK;
return NONEDAY;
}
@@ -914,7 +926,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);
@@ -930,8 +942,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 */
@@ -942,10 +954,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;
@@ -962,7 +974,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)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [v5 PATCH 2/3] cal: add option to set Gregorian reform date
2018-01-16 21:37 [v5 PATCH 0/3] Pull Request J William Piggott
2018-01-16 21:46 ` [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct J William Piggott
@ 2018-01-16 21:47 ` J William Piggott
2018-01-16 21:49 ` [v5 PATCH 3/3] cal: update man page J William Piggott
2 siblings, 0 replies; 5+ messages in thread
From: J William Piggott @ 2018-01-16 21:47 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
Create the new option: --reform <1752|gregorian|iso|julian>
This adds the capability to display either the proleptic Gregorian or
the Julian calendar systems exclusively.
Also create the option --iso as alias of --reform=gregorian.
Signed-off-by: J William Piggott <elseifthen@gmx.com>
---
misc-utils/cal.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 49 insertions(+), 4 deletions(-)
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index ea582964c..543fbc6e1 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -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,
@@ -246,6 +254,7 @@ static int week_number(int day, int month, int32_t year, const struct cal_contro
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)
@@ -256,7 +265,7 @@ int main(int argc, char **argv)
int ch = 0, yflag = 0, Yflag = 0;
static struct cal_control ctl = {
- .reform_year = 1752,
+ .reform_year = DEFAULT_REFORM_YEAR,
.weekstart = SUNDAY,
.num_months = 1, /* default is "cal -1" */
.span_months = 0,
@@ -269,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[] = {
@@ -283,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'},
@@ -395,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;
@@ -453,7 +472,7 @@ 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, ctl.req.year)]
@@ -1012,6 +1031,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;
@@ -1030,7 +1073,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);
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [v5 PATCH 3/3] cal: update man page
2018-01-16 21:37 [v5 PATCH 0/3] Pull Request J William Piggott
2018-01-16 21:46 ` [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct J William Piggott
2018-01-16 21:47 ` [v5 PATCH 2/3] cal: add option to set Gregorian reform date J William Piggott
@ 2018-01-16 21:49 ` J William Piggott
2 siblings, 0 replies; 5+ messages in thread
From: J William Piggott @ 2018-01-16 21:49 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
Update cal.1 with the new options --reform and --iso.
Also add information about the calendar systems used and
the difference between the --julian option and the Julian
calendar system.
Signed-off-by: J William Piggott <elseifthen@gmx.com>
---
misc-utils/cal.1 | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 74 insertions(+), 9 deletions(-)
diff --git a/misc-utils/cal.1 b/misc-utils/cal.1
index f1084edba..f99c6b495 100644
--- a/misc-utils/cal.1
+++ b/misc-utils/cal.1
@@ -34,7 +34,7 @@
.\"
.\" @(#)cal.1 8.1 (Berkeley) 6/6/93
.\"
-.TH CAL 1 "June 2015" "util-linux" "User Commands"
+.TH CAL 1 "January 2018" "util-linux" "User Commands"
.SH NAME
cal \- display a calendar
.SH SYNOPSIS
@@ -52,6 +52,22 @@ month is displayed.
.sp
The \fImonth\fR may be specified as a number (1-12), as a month name or as an
abbreviated month name according to the current locales.
+.sp
+Two different calendar systems are used, Gregorian and Julian. These are
+nearly identical systems with Gregorian making a small adjustment to the
+frequency of leap years; this facilitates improved synchronization with solar
+events like the equinoxes. The Gregorian calendar reform was introduced in
+1582, but its adoption continued up to 1923. By default
+.B cal
+uses the adoption date of 3 Sept 1752. From that date forward the Gregorian
+calendar is displayed; previous dates use the Julian calendar system. 11 days
+were removed at the time of adoption to bring the calendar in sync with solar
+events. So Sept 1752 has a mix of Julian and Gregorian dates by which the 2nd
+is followed by the 14th (the 3rd through the 13th are absent).
+.sp
+Optionally, either the proleptic Gregorian calendar or the Julian calendar may
+be used exclusively.
+.RB See\ \-\-reform\ below.
.SH OPTIONS
.TP
\fB\-1\fR, \fB\-\-one\fR
@@ -73,8 +89,60 @@ Display Sunday as the first day of the week.
\fB\-m\fR, \fB\-\-monday\fR
Display Monday as the first day of the week.
.TP
+.B \-\-iso
+Display the proleptic Gregorian calendar exclusively.
+.RB See\ \-\-reform\ below.
+.TP
\fB\-j\fR, \fB\-\-julian\fR
-Display Julian dates (days one-based, numbered from January 1).
+Use day-of-year numbering for all calendars. These are also called ordinal
+days. Ordinal days range from 1 to 366. This option does not switch from the
+Gregorian to the Julian calendar system, that is controlled by the
+.BR \-\-reform\ option.
+.sp
+Sometimes Gregorian calendars using ordinal dates are referred to as Julian
+calendars. This can be confusing due to the many date related conventions that
+use Julian in their name: (ordinal) julian date, julian (calendar) date,
+(astronomical) julian date, (modified) julian date, and more. This option is
+named julian, because ordinal days are identified as julian by the POSIX
+standard. However, be aware that
+.B cal
+also uses the Julian calendar system.
+.RB See\ DESCRIPTION\ above.
+.TP
+.BI \-\-reform\ val
+This option sets the adoption date of the Gregorian calendar reform. Calendar
+dates previous to reform use the Julian calendar system. Calendar dates
+after reform use the Gregorian calendar system. The argument
+.I val
+can be:
+.RS
+.IP \(bu 2
+.I 1752
+- sets 3 September 1752 as the reform date (default).
+This is when the Gregorian calendar reform was adopted by the British Empire.
+.IP \(bu 2
+.I gregorian
+- display Gregorian calendars exclusively. This special placeholder sets the
+reform date below the smallest year that
+.B cal
+can use; meaning all calendar output uses the Gregorian calendar system. This
+is called the proleptic Gregorian calendar, because dates prior to the calendar
+system's creation use extrapolated values.
+.IP \(bu 2
+.I iso
+- alias of
+.IR gregorian .
+The ISO 8601 standard for the representation of dates and times in information
+interchange requires using the proleptic Gregorian calendar.
+.IP \(bu 2
+.I julian
+- display Julian calendars exclusively. This special placeholder sets the reform date above the largest year that
+.B cal
+can use; meaning all
+calendar output uses the Julian calendar system.
+.PP
+.RB See\ \%DESCRIPTION\ above.
+.RE
.TP
\fB\-y\fR, \fB\-\-year\fR
Display a calendar for the whole year.
@@ -148,14 +216,11 @@ See
for more details about colorization configuration.
.SH BUGS
.PP
-The
+The default
.B cal
-program uses the 3rd of September 1752 as the date of the Gregorian calendar
-reformation -- that is when it happened in Great Britain and its colonies
-(including what is now the USA). Starting at that date, eleven days were eliminated
-by this reformation, so the calendar for that month is rather unusual.
-The actual historical dates at which the calendar reform happened in all the
-different countries (locales) are ignored.
+output uses 3 September 1752 as the Gregorian calendar reform date. The
+historical reform dates for the other locales, including its introduction in
+October 1582, are not implemented.
.PP
Alternative calendars, such as the Umm al-Qura, the Solar Hijri, the Ge'ez,
or the lunisolar Hindu, are not supported.
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct
2018-01-16 21:35 ` J William Piggott
@ 2018-01-16 21:39 ` J William Piggott
0 siblings, 0 replies; 5+ messages in thread
From: J William Piggott @ 2018-01-16 21:39 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
Signed-off-by: J William Piggott <elseifthen@gmx.com>
---
misc-utils/cal.c | 86 ++++++++++++++++++++++++++++++++------------------------
1 file changed, 49 insertions(+), 37 deletions(-)
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 39f2bdcba..ea582964c 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -160,7 +160,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 +205,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 +230,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,8 +238,10 @@ 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);
@@ -252,7 +254,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 = 1752,
.weekstart = SUNDAY,
.num_months = 1, /* default is "cal -1" */
.span_months = 0,
@@ -335,7 +339,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
@@ -451,10 +456,12 @@ int main(int argc, char **argv)
if (ctl.req.year == INT32_MAX)
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 +483,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 +539,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 +625,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 +651,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 +735,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 +856,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,7 +874,8 @@ 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)
{
/*
* The magic constants in the reform[] array are, in a simplified
@@ -886,20 +897,21 @@ static int day_in_week(int day, int month, int32_t year)
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 != REFORMATION_YEAR + 1)
+ 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)) {
+ if (ctl->reform_year < year
+ || (year == ctl->reform_year && REFORMATION_MONTH < month)
+ || (year == ctl->reform_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;
+ return (long_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))
+ if (year < ctl->reform_year
+ || (year == ctl->reform_year && month < REFORMATION_MONTH)
+ || (year == ctl->reform_year && month == REFORMATION_MONTH && day < 3))
return (year + year / 4 + old[month - 1] + day) % DAYS_IN_WEEK;
return NONEDAY;
}
@@ -914,7 +926,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);
@@ -930,8 +942,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 */
@@ -942,10 +954,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;
@@ -962,7 +974,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)
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-01-16 21:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-16 21:37 [v5 PATCH 0/3] Pull Request J William Piggott
2018-01-16 21:46 ` [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct J William Piggott
2018-01-16 21:47 ` [v5 PATCH 2/3] cal: add option to set Gregorian reform date J William Piggott
2018-01-16 21:49 ` [v5 PATCH 3/3] cal: update man page J William Piggott
-- strict thread matches above, loose matches on Subject: below --
2018-01-02 14:53 [v3 PATCH 00/11] Pull Request - changelog J William Piggott
2018-01-08 10:21 ` Karel Zak
2018-01-11 2:00 ` J William Piggott
2018-01-11 9:01 ` Karel Zak
2018-01-11 13:35 ` J William Piggott
2018-01-12 10:57 ` Karel Zak
2018-01-15 2:02 ` J William Piggott
2018-01-15 13:36 ` Karel Zak
2018-01-16 21:35 ` J William Piggott
2018-01-16 21:39 ` [v5 PATCH 1/3] cal: move REFORMATION_YEAR to control struct J William Piggott
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).