|
From: <ls...@us...> - 2007-10-06 20:26:20
|
Revision: 3537
http://jnode.svn.sourceforge.net/jnode/?rev=3537&view=rev
Author: lsantha
Date: 2007-10-06 13:26:18 -0700 (Sat, 06 Oct 2007)
Log Message:
-----------
Added a selfcontained implementation of getTime(), with no dependence on java.util.Calendar which was very expensive.
Modified Paths:
--------------
trunk/core/src/driver/org/jnode/driver/system/cmos/def/RTC.java
trunk/core/src/test/org/jnode/test/CMOSTest.java
Modified: trunk/core/src/driver/org/jnode/driver/system/cmos/def/RTC.java
===================================================================
--- trunk/core/src/driver/org/jnode/driver/system/cmos/def/RTC.java 2007-10-06 20:17:35 UTC (rev 3536)
+++ trunk/core/src/driver/org/jnode/driver/system/cmos/def/RTC.java 2007-10-06 20:26:18 UTC (rev 3537)
@@ -21,8 +21,6 @@
package org.jnode.driver.system.cmos.def;
-import java.util.GregorianCalendar;
-
import org.jnode.driver.system.cmos.CMOSConstants;
import org.jnode.driver.system.cmos.CMOSService;
import org.jnode.util.BCDUtils;
@@ -35,11 +33,10 @@
public class RTC extends RTCService implements CMOSConstants {
private final CMOSService cmos;
- private final ThreadLocal calendar = new ThreadLocal();
-
+
/**
* Create a new instance
- * @param cmos
+ * @param cmos the CMOS service
*/
public RTC(CMOSService cmos) {
this.cmos = cmos;
@@ -91,7 +88,7 @@
* Gets the current day of the month
* @return int
*/
- public int getDate() {
+ public int getDay() {
final int control = cmos.getRegister(RTC_CONTROL);
final int date = cmos.getRegister(CMOS_RTC_DAY_OF_MONTH);
if ((control & RTC_DM_BINARY) != 0) {
@@ -139,14 +136,32 @@
* @see java.util.Calendar#setTimeInMillis(long)
* @return The time
*/
- public long getTime() {
- GregorianCalendar cal = (GregorianCalendar)calendar.get();
- if (cal == null) {
- cal = new GregorianCalendar(getYear(), getMonth()-1, getDate(), getHours(), getMinutes(), getSeconds());
- calendar.set(cal);
- } else {
- cal.set(getYear(), getMonth()-1, getDate(), getHours(), getMinutes(), getSeconds());
- }
- return cal.getTimeInMillis();
- }
+ public long getTime() {
+ return time2millis(getYear(), getMonth(), getDay(), getHours(), getMinutes(), getSeconds());
+ }
+
+ /**
+ * Converts Gregorian date to milliseconds since 1970-01-01 00:00:00 .
+ *
+ * @param year the year
+ * @param mon the month 1..12
+ * @param day the day of month 1..31
+ * @param hours hours of day 0..23
+ * @param mins minutes 0..59
+ * @param secs seconds 0..59
+ * @return the milliseconds since 1970-01-01 00:00:00
+ */
+ static long time2millis(int year, int mon, int day, int hours, int mins, int secs) {
+ if (0 >= (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
+ mon += 12; /* Puts Feb last since it has leap day */
+ year -= 1;
+ }
+
+ return ((((
+ ((long) (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) + year * 365 - 719499)) /* days */
+ * 24l + hours) /* hours */
+ * 60l + mins) /* minutes */
+ * 60l + secs) /* seconds */
+ * 1000l; /* milliseconds */
+ }
}
Modified: trunk/core/src/test/org/jnode/test/CMOSTest.java
===================================================================
--- trunk/core/src/test/org/jnode/test/CMOSTest.java 2007-10-06 20:17:35 UTC (rev 3536)
+++ trunk/core/src/test/org/jnode/test/CMOSTest.java 2007-10-06 20:26:18 UTC (rev 3537)
@@ -37,7 +37,7 @@
public static void main(String[] args)
throws ResourceNotFreeException, NamingException {
- CMOSService cmos = (CMOSService)InitialNaming.lookup(CMOSService.NAME);
+ CMOSService cmos = InitialNaming.lookup(CMOSService.NAME);
RTC rtc = new RTC(cmos);
for (int i = 0; i < 10; i++) {
@@ -45,7 +45,7 @@
}
System.out.println("time=" + rtc.getHours() + ":" + rtc.getMinutes() + ":" + rtc.getSeconds());
- System.out.println("date=" + rtc.getDate() + "-" + rtc.getMonth() + "-" + rtc.getYear());
+ System.out.println("date=" + rtc.getDay() + "-" + rtc.getMonth() + "-" + rtc.getYear());
int fp = cmos.getRegister(0x10);
System.out.println("floppy A: " + ((fp >> 4) & 0x0f));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|