summaryrefslogtreecommitdiff
path: root/ext/date/lib/dow.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/date/lib/dow.c')
-rw-r--r--ext/date/lib/dow.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/ext/date/lib/dow.c b/ext/date/lib/dow.c
index 1739566728..4438b723d2 100644
--- a/ext/date/lib/dow.c
+++ b/ext/date/lib/dow.c
@@ -23,9 +23,21 @@
static int m_table_common[13] = { -1, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
static int m_table_leap[13] = { -1, 6, 2, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 }; /* 1 = jan */
+static timelib_sll positive_mod(timelib_sll x, timelib_sll y)
+{
+ timelib_sll tmp;
+
+ tmp = x % y;
+ if (tmp < 0) {
+ tmp += y;
+ }
+
+ return tmp;
+}
+
static timelib_sll century_value(timelib_sll j)
{
- return 6 - (j % 4) * 2;
+ return 6 - positive_mod(j, 4) * 2;
}
static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_sll d, int iso)
@@ -36,9 +48,9 @@ static timelib_sll timelib_day_of_week_ex(timelib_sll y, timelib_sll m, timelib_
* Julian calendar. We just return the 'wrong' day of week to be
* consistent. */
c1 = century_value(y / 100);
- y1 = (y % 100);
+ y1 = positive_mod(y, 100);
m1 = timelib_is_leap(y) ? m_table_leap[m] : m_table_common[m];
- dow = (c1 + y1 + m1 + (y1 / 4) + d) % 7;
+ dow = positive_mod((c1 + y1 + m1 + (y1 / 4) + d), 7);
if (iso) {
if (dow == 0) {
dow = 7;