public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 1/4] cal: use Claus Tøndering's day of the week algorithm
Date: Sun, 26 May 2013 10:14:07 +0100	[thread overview]
Message-ID: <1369559650-12341-2-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1369559650-12341-1-git-send-email-kerolasa@iki.fi>

Reference: http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#T.C3.B8ndering.27s_algorithm
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/cal.c | 50 ++++++++++++++++++++------------------------------
 1 file changed, 20 insertions(+), 30 deletions(-)

diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 4d45145..9575db4 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -234,8 +234,6 @@ int julian;
 
 /* function prototypes */
 static int leap_year(int year);
-static int centuries_since_1700(int year, int centuries);
-static int leap_years_since_year_1(int year);
 static char * ascii_day(char *, int);
 static int center_str(const char* src, char* dest, size_t dest_size, size_t width);
 static void center(const char *, size_t, int);
@@ -427,22 +425,6 @@ static int leap_year(int year)
 		return ( !(year % 4) && (year % 100) ) || !(year % 400);
 }
 
-/* number of centuries since 1700 */
-static int centuries_since_1700(int year, int n)
-{
-	if (year < REFORMATION_YEAR)
-		return 0;
-	else
-		return ((year / (100 * n)) - ((REFORMATION_YEAR / 100) / n));
-}
-
-/* number of leap years between year 1 and this year, not inclusive */
-static int leap_years_since_year_1(int year)
-{
-	return (year / 4 - centuries_since_1700(year, 1) +
-		centuries_since_1700(year, 4));
-}
-
 static void headers_init(int julian)
 {
 	int i, wd, spaces = julian ? J_DAY_LEN - 1 : DAY_LEN - 1;
@@ -681,18 +663,26 @@ day_in_year(int day, int month, int year) {
  *	during the period of 11 days.
  */
 static int
-day_in_week(int day, int month, int year) {
-	long temp;
-
-	temp =
-	    (long)(year - SMALLEST_YEAR) * DAYS_IN_YEAR +
-	    leap_years_since_year_1(year - SMALLEST_YEAR)
-	    + day_in_year(day, month, year);
-	if (temp < FIRST_MISSING_DAY)
-		return ((temp + (FIRST_WEEKDAY - 1)) % DAYS_IN_WEEK);
-	if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
-		return ((temp + (FIRST_WEEKDAY - 1 - NUMBER_MISSING_DAYS)) % DAYS_IN_WEEK);
-	return (NONEDAY);
+day_in_week(int d, int m, int y)
+{
+	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 (y != 1753)
+		y -= m < 3;
+	else
+		y -= (m < 3) + 14;
+	if (1752 < y || (y == 1752 && 9 < m) || (y == 1752 && m == 9 && 13 < d))
+		return (y + (y / 4) - (y / 100) + (y / 400) + reform[m - 1] +
+			d) % 7;
+	if (y < 1752 || (y == 1752 && m < 9) || (y == 1752 && m == 9 && d < 3))
+		return (y + y / 4 + old[m - 1] + d) % 7;
+	return NONEDAY;
 }
 
 static char *
-- 
1.8.2.3


  reply	other threads:[~2013-05-26  9:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-26  9:14 [PATCH 0000] pull: cal the branch four Sami Kerola
2013-05-26  9:14 ` Sami Kerola [this message]
2013-05-26  9:14 ` [PATCH 2/4] cal: remove arbitrary limit of maxium year being 9999 Sami Kerola
2013-05-26  9:14 ` [PATCH 3/4] tests: check cal works when year is far in future Sami Kerola
2013-05-26  9:14 ` [PATCH 4/4] docs: cal: stop telling year 9999 is upper limit Sami Kerola
2013-05-26 18:23 ` [PATCH 0000] pull: cal the branch four Bernhard Voelker
2013-05-26 18:55   ` Sami Kerola
2013-05-26 19:59     ` Sami Kerola
2013-05-28 13:49     ` Karel Zak
2013-05-29 16:20       ` Sami Kerola
2013-05-28 13:50 ` Karel Zak

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=1369559650-12341-2-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --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