001: /*
002: * Time.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: Time.java,v 1.6 2003/11/15 11:03:33 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.io.File;
025: import java.util.TimeZone;
026:
027: public final class Time extends Lisp {
028: // ### %time
029: private static final Primitive1 _TIME = new Primitive1("%time",
030: PACKAGE_SYS, false) {
031: public LispObject execute(LispObject arg)
032: throws ConditionThrowable {
033: Cons.setCount(0);
034: long start = System.currentTimeMillis();
035: LispObject result = arg.execute(new LispObject[0]);
036: long elapsed = System.currentTimeMillis() - start;
037: long count = Cons.getCount();
038: CharacterOutputStream out = getTraceOutput();
039: out.freshLine();
040: StringBuffer sb = new StringBuffer(String
041: .valueOf((float) elapsed / 1000));
042: sb.append(" seconds");
043: if (count > 0) {
044: sb.append(System.getProperty("line.separator"));
045: sb.append(count);
046: sb.append(" cons cell");
047: if (count > 1)
048: sb.append('s');
049: }
050: out.writeString(sb.toString());
051: out.flushOutput();
052: return result;
053: }
054: };
055:
056: // ### get-internal-real-time
057: private static final Primitive0 GET_INTERNAL_REAL_TIME = new Primitive0(
058: "get-internal-real-time") {
059: public LispObject execute() throws ConditionThrowable {
060: return number(System.currentTimeMillis());
061: }
062: };
063:
064: // ### get-internal-run-time
065: private static final Primitive0 GET_INTERNAL_RUN_TIME = new Primitive0(
066: "get-internal-run-time") {
067: public LispObject execute() throws ConditionThrowable {
068: return number(System.currentTimeMillis()); // FIXME
069: }
070: };
071:
072: // ### get-universal-time
073: private static final Primitive0 GET_UNIVERSAL_TIME = new Primitive0(
074: "get-universal-time") {
075: public LispObject execute() {
076: return number(System.currentTimeMillis() / 1000 + 2208988800L);
077: }
078: };
079:
080: // ### file-write-date
081: private static final Primitive1 FILE_WRITE_DATE = new Primitive1(
082: "file-write-date") {
083: public LispObject execute(LispObject arg)
084: throws ConditionThrowable {
085: String namestring;
086: if (arg instanceof LispString)
087: namestring = ((LispString) arg).getValue();
088: else if (arg instanceof Pathname)
089: namestring = ((Pathname) arg).getNamestring();
090: else
091: throw new ConditionThrowable(new TypeError(arg,
092: "pathname designator"));
093: File file = new File(namestring);
094: long lastModified = file.lastModified();
095: if (lastModified == 0)
096: return NIL;
097: return number(lastModified / 1000 + 2208988800L);
098: }
099: };
100:
101: // ### default-time-zone
102: private static final Primitive0 DEFAULT_TIME_ZONE = new Primitive0(
103: "default-time-zone", PACKAGE_SYS, false) {
104: public LispObject execute() throws ConditionThrowable {
105: TimeZone tz = TimeZone.getDefault();
106: int offset = tz.getOffset(System.currentTimeMillis());
107: // "Time zone values increase with motion to the west..."
108: // Convert milliseconds to hours.
109: return new Fixnum(-offset).divideBy(new Fixnum(3600000));
110: }
111: };
112: }
|