From: J William Piggott <elseifthen@gmx.com>
To: Karel Zak <kzak@redhat.com>
Cc: util-linux@vger.kernel.org
Subject: [v5 PATCH 2/3] cal: add option to set Gregorian reform date
Date: Tue, 16 Jan 2018 16:41:10 -0500 [thread overview]
Message-ID: <faff0cac-fb47-1926-3ef0-dba6907f47d7@gmx.com> (raw)
In-Reply-To: <a638c05f-19dc-513f-51b0-f37937193196@gmx.com>
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);
next prev parent reply other threads:[~2018-01-16 21:41 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-02 14:53 [v3 PATCH 00/11] Pull Request - changelog J William Piggott
2018-01-02 14:54 ` [v3 PATCH 01/11] cal: change default output format J William Piggott
2018-01-02 14:55 ` [v3 PATCH 02/11] cal: change reformation to reform J William Piggott
2018-01-02 14:56 ` [v3 PATCH 03/11] cal: update man page and usage() J William Piggott
2018-01-02 14:57 ` [v3 PATCH 04/11] cal: rename --julian to --ordinal J William Piggott
2018-01-02 14:58 ` [v3 PATCH 05/11] cal: add private --caesar option J William Piggott
2018-01-02 14:59 ` [v3 PATCH 06/11] cal: make -w accept its optional argument J William Piggott
2018-01-02 14:59 ` [v3 PATCH 07/11] cal: update mutually exclusive options J William Piggott
2018-01-02 15:00 ` [v3 PATCH 08/11] cal: add short versions of new options J William Piggott
2018-01-02 15:01 ` [v3 PATCH 09/11] cal: remove the non-functional options J William Piggott
2018-01-02 15:02 ` [v3 PATCH 10/11] cal: fix first week calculation J William Piggott
2018-01-02 15:02 ` [v3 PATCH 11/11] cal: fix week calculations for --1752-reform J William Piggott
2018-01-03 10:06 ` [v3 PATCH 00/11] Pull Request - changelog Karel Zak
2018-01-08 10:21 ` Karel Zak
2018-01-09 1:17 ` J William Piggott
2018-01-24 18:13 ` Ruediger Meier
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
2018-01-16 21:41 ` J William Piggott [this message]
2018-01-16 21:42 ` [v5 PATCH 3/3] cal: update man page J William Piggott
2018-01-17 12:08 ` [v3 PATCH 00/11] Pull Request - changelog Karel Zak
2018-01-18 16:32 ` J William Piggott
2018-01-22 11:46 ` Karel Zak
-- strict thread matches above, loose matches on Subject: below --
2018-01-16 21:37 [v5 PATCH 0/3] Pull Request J William Piggott
2018-01-16 21:47 ` [v5 PATCH 2/3] cal: add option to set Gregorian reform date J William Piggott
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=faff0cac-fb47-1926-3ef0-dba6907f47d7@gmx.com \
--to=elseifthen@gmx.com \
--cc=kzak@redhat.com \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).