01: /*
02: * Time.java
03: *
04: * Copyright (C) 2003-2004 Peter Graves
05: * $Id: Time.java,v 1.21 2004/06/23 01:50:39 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: import java.util.Date;
25: import java.util.TimeZone;
26:
27: public final class Time extends Lisp {
28: // ### %time
29: private static final Primitive1 _TIME = new Primitive1("%time",
30: PACKAGE_SYS, false) {
31: public LispObject execute(LispObject arg)
32: throws ConditionThrowable {
33: Cons.setCount(0);
34: long start = System.currentTimeMillis();
35: LispObject result = arg.execute(new LispObject[0]);
36: long elapsed = System.currentTimeMillis() - start;
37: long count = Cons.getCount();
38: Stream out = checkCharacterOutputStream(_TRACE_OUTPUT_
39: .symbolValue());
40: out.freshLine();
41: StringBuffer sb = new StringBuffer(String
42: .valueOf((float) elapsed / 1000));
43: sb.append(" seconds");
44: sb.append(System.getProperty("line.separator"));
45: if (count > 0) {
46: sb.append(count);
47: sb.append(" cons cell");
48: if (count > 1)
49: sb.append('s');
50: sb.append(System.getProperty("line.separator"));
51: }
52: out._writeString(sb.toString());
53: out._finishOutput();
54: return result;
55: }
56: };
57:
58: // ### get-internal-real-time
59: private static final Primitive0 GET_INTERNAL_REAL_TIME = new Primitive0(
60: "get-internal-real-time", "") {
61: public LispObject execute() throws ConditionThrowable {
62: return number(System.currentTimeMillis());
63: }
64: };
65:
66: // ### get-internal-run-time
67: private static final Primitive0 GET_INTERNAL_RUN_TIME = new Primitive0(
68: "get-internal-run-time", "") {
69: public LispObject execute() throws ConditionThrowable {
70: return number(System.currentTimeMillis()); // FIXME
71: }
72: };
73:
74: // ### get-universal-time
75: private static final Primitive0 GET_UNIVERSAL_TIME = new Primitive0(
76: "get-universal-time", "") {
77: public LispObject execute() {
78: return number(System.currentTimeMillis() / 1000 + 2208988800L);
79: }
80: };
81:
82: // ### default-time-zone
83: private static final Primitive0 DEFAULT_TIME_ZONE = new Primitive0(
84: "default-time-zone", PACKAGE_SYS, false) {
85: public LispObject execute() throws ConditionThrowable {
86: TimeZone tz = TimeZone.getDefault();
87: //int offset = tz.getOffset(System.currentTimeMillis());
88: // Classpath hasn't implemented TimeZone.getOffset(long).
89: int rawOffset = tz.getRawOffset();
90: if (tz.inDaylightTime(new Date(System.currentTimeMillis())))
91: rawOffset += tz.getDSTSavings();
92: // "Time zone values increase with motion to the west..."
93: // Convert milliseconds to hours.
94: return new Fixnum(-rawOffset).divideBy(new Fixnum(3600000));
95: }
96: };
97: }
|