summaryrefslogtreecommitdiffstats
path: root/misc-utils/cal.c
diff options
context:
space:
mode:
authorSami Kerola2013-04-29 23:30:51 +0200
committerSami Kerola2013-05-26 10:59:20 +0200
commitd68f076f1c362847a5e96abbfe3f51b23e7b72e6 (patch)
treeb77d0b07ea1a8cb2d90f3a75b4bf4931a59b53e2 /misc-utils/cal.c
parentcal: mark all functions static (diff)
downloadkernel-qcow2-util-linux-d68f076f1c362847a5e96abbfe3f51b23e7b72e6.tar.gz
kernel-qcow2-util-linux-d68f076f1c362847a5e96abbfe3f51b23e7b72e6.tar.xz
kernel-qcow2-util-linux-d68f076f1c362847a5e96abbfe3f51b23e7b72e6.zip
cal: simplify day_in_week() calculation
The day_in_week() can be made easier to read, and should return invalid weekday at the time of missing 11 days at September 1752. Reported-by: Thomas Bächler <thomas@archlinux.org> References: http://www.spinics.net/lists/util-linux-ng/msg07910.html Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'misc-utils/cal.c')
-rw-r--r--misc-utils/cal.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 159ad5ece..4d45145ac 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -160,11 +160,11 @@ enum {
THURSDAY,
FRIDAY,
SATURDAY,
- DAYS_IN_WEEK
+ DAYS_IN_WEEK,
+ NONEDAY
};
#define FIRST_WEEKDAY SATURDAY /* Jan 1st, 1 was a Saturday */
-#define REFORMATION_WEEKDAY THURSDAY /* after reformation it was Thursday */
#define REFORMATION_YEAR 1752 /* Signed-off-by: Lord Chesterfield */
#define REFORMATION_MONTH 9 /* September */
#define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
@@ -677,8 +677,8 @@ day_in_year(int day, int month, int year) {
* day_in_week
* return the 0 based day number for any date from 1 Jan. 1 to
* 31 Dec. 9999. Assumes the Gregorian reformation eliminates
- * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
- * missing days.
+ * 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, int year) {
@@ -689,10 +689,10 @@ day_in_week(int day, int month, int year) {
leap_years_since_year_1(year - SMALLEST_YEAR)
+ day_in_year(day, month, year);
if (temp < FIRST_MISSING_DAY)
- return ((temp - 1 + FIRST_WEEKDAY) % DAYS_IN_WEEK);
+ return ((temp + (FIRST_WEEKDAY - 1)) % DAYS_IN_WEEK);
if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
- return (((temp - 1 + FIRST_WEEKDAY) - NUMBER_MISSING_DAYS) % DAYS_IN_WEEK);
- return(REFORMATION_WEEKDAY);
+ return ((temp + (FIRST_WEEKDAY - 1 - NUMBER_MISSING_DAYS)) % DAYS_IN_WEEK);
+ return (NONEDAY);
}
static char *