rtc.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 135 行

JAVA
135
字号
/*
 * $Id: RTC.java,v 1.2 2004/01/02 08:41:59 epr Exp $
 */
package org.jnode.driver.cmos.def;

import java.util.GregorianCalendar;

import org.jnode.driver.cmos.CMOSConstants;
import org.jnode.driver.cmos.CMOSService;
import org.jnode.util.BCDUtils;
import org.jnode.vm.RTCService;

/**
 * Real Time Clock
 * @author epr
 */
public class RTC extends RTCService implements CMOSConstants {
	
	private final CMOSService cmos;
	private final ThreadLocal calendar = new ThreadLocal(); 
	
	/**
	 * Create a new instance
	 * @param cmos
	 */
	public RTC(CMOSService cmos) {
		this.cmos = cmos;
	}
	
	/**
	 * Gets the current second
	 * @return  int 
	 */
	public int getSeconds() {
		final int control = cmos.getRegister(RTC_CONTROL);
		final int seconds = cmos.getRegister(CMOS_RTC_SECONDS);
		if ((control & RTC_DM_BINARY) != 0) {
			return seconds;
		} else {
			return BCDUtils.bcd2bin(seconds);
		}
	}

	/**
	 * Gets the current minute
	 * @return int
	 */
	public int getMinutes() {
		final int control = cmos.getRegister(RTC_CONTROL);
		final int minutes = cmos.getRegister(CMOS_RTC_MINUTES);
		if ((control & RTC_DM_BINARY) != 0) {
			return minutes;
		} else {
			return BCDUtils.bcd2bin(minutes);
		}
	}

	/**
	 * Gets the current hour
	 * @return int
	 */
	public int getHours() {
		final int control = cmos.getRegister(RTC_CONTROL);
		final int hours = cmos.getRegister(CMOS_RTC_HOURS);
		if ((control & RTC_DM_BINARY) != 0) {
			return hours;
		} else {
			return BCDUtils.bcd2bin(hours);
		}
	}

	/**
	 * Gets the current day of the month
	 * @return int
	 */
	public int getDate() {
		final int control = cmos.getRegister(RTC_CONTROL);
		final int date = cmos.getRegister(CMOS_RTC_DAY_OF_MONTH);
		if ((control & RTC_DM_BINARY) != 0) {
			return date;
		} else {
			return BCDUtils.bcd2bin(date);
		}
	}

	/**
	 * Gets the current month
	 * @return int
	 */
	public int getMonth() {
		final int control = cmos.getRegister(RTC_CONTROL);
		final int month = cmos.getRegister(CMOS_RTC_MONTH);
		if ((control & RTC_DM_BINARY) != 0) {
			return month;
		} else {
			return BCDUtils.bcd2bin(month);
		}
	}

	/**
	 * Gets the current year
	 * @return int
	 */
	public int getYear() {
		final int control = cmos.getRegister(RTC_CONTROL);
		int year = cmos.getRegister(CMOS_RTC_YEAR);
		if ((control & RTC_DM_BINARY) == 0) {
			year = BCDUtils.bcd2bin(year);
		}
		year += 1900;
		if (year < 1970) {
			year += 100;
		}
		return year;
	}
	
	/**
	 * Gets the time calculated from the RTC.
	 * This time value can be used as input of Date.
	 * @see java.util.Date#Date(long)
	 * @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();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?