From 52bc20ace2c57b106c70a6de6d7f71a54667ee54 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Mon, 22 Oct 2007 15:53:46 -0600 Subject: [PATCH] Optimized calendar implementation. Year can now be computed with arithmetic, not a loop to find the current year. --- classpath/java/util/Calendar.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/classpath/java/util/Calendar.java b/classpath/java/util/Calendar.java index c479cc49fc..dbf16b3cc4 100644 --- a/classpath/java/util/Calendar.java +++ b/classpath/java/util/Calendar.java @@ -65,6 +65,9 @@ public abstract class Calendar { private static final int MILLIS_PER_SECOND = 1000; private static final int EPOCH_YEAR = 1970; + private static final int EPOCH_LEAP_YEAR = 1968; + private static final int DAYS_TO_EPOCH = 731; + private static final int[][] DAYS_IN_MONTH = new int[][] { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } @@ -85,22 +88,22 @@ public abstract class Calendar { return (year%4 == 0) && (year%100 != 0) || (year%400 == 0); } - private static int daysInYear(int year) { - return isLeapYear(year) ? 366 : 365; - } - private void parseIntoFields(long timeInMillis) { long days = timeInMillis / MILLIS_PER_DAY; - int year = EPOCH_YEAR; - while (days >= daysInYear(year)) { - days -= daysInYear(year++); - } + /* convert days since Jan 1, 1970 to days since Jan 1, 1968 */ + days += DAYS_TO_EPOCH; + long years = 4 * days / 1461; /* days/365.25 = 4*days/(4*365.25) */ + int year = (int)(EPOCH_LEAP_YEAR + years); + days -= 365 * years + years / 4; + if (!isLeapYear(year)) days--; + int month=0; int leapIndex = isLeapYear(year) ? 1 : 0; while (days >= DAYS_IN_MONTH[leapIndex][month]) { days -= DAYS_IN_MONTH[leapIndex][month++]; } days++; + int remainder = (int)(timeInMillis % MILLIS_PER_DAY); int hour = remainder / MILLIS_PER_HOUR; remainder = remainder % MILLIS_PER_HOUR;