KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > Time


1 /*
2  * Time.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: Time.java,v 1.21 2004/06/23 01:50:39 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25 import java.util.TimeZone JavaDoc;
26
27 public final class Time extends Lisp
28 {
29     // ### %time
30
private static final Primitive1 _TIME =
31         new Primitive1("%time", PACKAGE_SYS, false)
32     {
33         public LispObject execute(LispObject arg) throws ConditionThrowable
34         {
35             Cons.setCount(0);
36             long start = System.currentTimeMillis();
37             LispObject result = arg.execute(new LispObject[0]);
38             long elapsed = System.currentTimeMillis() - start;
39             long count = Cons.getCount();
40             Stream out =
41                 checkCharacterOutputStream(_TRACE_OUTPUT_.symbolValue());
42             out.freshLine();
43             StringBuffer JavaDoc sb =
44                 new StringBuffer JavaDoc(String.valueOf((float)elapsed/1000));
45             sb.append(" seconds");
46             sb.append(System.getProperty("line.separator"));
47             if (count > 0) {
48                 sb.append(count);
49                 sb.append(" cons cell");
50                 if (count > 1)
51                     sb.append('s');
52                 sb.append(System.getProperty("line.separator"));
53             }
54             out._writeString(sb.toString());
55             out._finishOutput();
56             return result;
57         }
58     };
59
60     // ### get-internal-real-time
61
private static final Primitive0 GET_INTERNAL_REAL_TIME =
62         new Primitive0("get-internal-real-time","") {
63         public LispObject execute() throws ConditionThrowable
64         {
65             return number(System.currentTimeMillis());
66         }
67     };
68
69     // ### get-internal-run-time
70
private static final Primitive0 GET_INTERNAL_RUN_TIME =
71         new Primitive0("get-internal-run-time","") {
72         public LispObject execute() throws ConditionThrowable
73         {
74             return number(System.currentTimeMillis()); // FIXME
75
}
76     };
77
78     // ### get-universal-time
79
private static final Primitive0 GET_UNIVERSAL_TIME =
80         new Primitive0("get-universal-time","") {
81         public LispObject execute()
82         {
83             return number(System.currentTimeMillis() / 1000 + 2208988800L);
84         }
85     };
86
87     // ### default-time-zone
88
private static final Primitive0 DEFAULT_TIME_ZONE =
89         new Primitive0("default-time-zone", PACKAGE_SYS, false)
90     {
91         public LispObject execute() throws ConditionThrowable
92         {
93             TimeZone JavaDoc tz = TimeZone.getDefault();
94             //int offset = tz.getOffset(System.currentTimeMillis());
95
// Classpath hasn't implemented TimeZone.getOffset(long).
96
int rawOffset = tz.getRawOffset();
97             if (tz.inDaylightTime(new Date JavaDoc(System.currentTimeMillis())))
98                 rawOffset += tz.getDSTSavings();
99             // "Time zone values increase with motion to the west..."
100
// Convert milliseconds to hours.
101
return new Fixnum(- rawOffset).divideBy(new Fixnum(3600000));
102         }
103     };
104 }
105
Popular Tags