summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-01-01 22:00:54 +0000
committerrubidium <rubidium@openttd.org>2007-01-01 22:00:54 +0000
commit1483434d2ca90be92477425fca49fb55bdf5e8f6 (patch)
treec2a58939fe7826d6294626df18a75fb5b8d21fe9
parentcaeabecea2176c125c5b1e6af124f89f46867210 (diff)
downloadopenttd-1483434d2ca90be92477425fca49fb55bdf5e8f6.tar.xz
(svn r7731) -Fix (r5999): off-by-one error in the date to YMD calculation for first 4 years of a century that was not divisable by 400.
-rw-r--r--date.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/date.c b/date.c
index 631e88ba3..66bade3c9 100644
--- a/date.c
+++ b/date.c
@@ -92,9 +92,22 @@ void ConvertDateToYMD(Date date, YearMonthDay *ymd)
int rem = date % (365 * 400 + 97);
uint16 x;
- /* There are 24 leap years in 100 years */
- yr += 100 * (rem / (365 * 100 + 24));
- rem = rem % (365 * 100 + 24);
+ if (rem >= 365 * 100 + 25) {
+ /* There are 25 leap years in the first 100 years after
+ * every 400th year, as every 400th year is a leap year */
+ yr += 100;
+ rem -= 365 * 100 + 25;
+
+ /* There are 24 leap years in the next couple of 100 years */
+ yr += 100 * (rem / (365 * 100 + 24));
+ rem = (rem % (365 * 100 + 24));
+ }
+
+ if (!IsLeapYear(yr) && rem >= 365 * 4) {
+ /* The first 4 year of the century are not always a leap year */
+ yr += 4;
+ rem -= 365 * 4;
+ }
/* There is 1 leap year every 4 years */
yr += 4 * (rem / (365 * 4 + 1));