KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > util > Time


1 package org.jacorb.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2002-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23  
24 import org.omg.TimeBase.*;
25 import org.jacorb.orb.*;
26
27 /**
28  * Contains static methods to handle CORBA time values.
29  *
30  * @author Andre Spiegel <spiegel@gnu.org>
31  * @version $Id: Time.java,v 1.8 2004/05/06 12:40:01 nicolas Exp $
32  */

33 public class Time
34 {
35     /**
36      * Difference between the CORBA Epoch and the Unix Epoch: the time
37      * from 1582/10/15 00:00 until 1970/01/01 00:00 in 100 ns units.
38      */

39     public static final long UNIX_OFFSET = 122192928000000000L;
40
41     /**
42      * Returns the current time as a CORBA UtcT.
43      */

44     public static UtcT corbaTime()
45     {
46         return corbaTime(System.currentTimeMillis());
47     }
48
49     /**
50      * Converts the given unixTime into a CORBA UtcT.
51      * @param unixTime the number of milliseconds since 1970/01/01 00:00 UTC.
52      */

53     public static UtcT corbaTime(long unixTime)
54     {
55         UtcT result = new UtcT();
56
57         result.time = (unixTime * 10000) + UNIX_OFFSET;
58
59         // unixTime is always UTC.
60
// Therefore, no time zone offset.
61
result.tdf = 0;
62         
63         // nothing reasonable to put here
64
result.inacchi = 0;
65         result.inacclo = 0;
66         
67         return result;
68     }
69
70     /**
71      * Converts the given Java date into a CORBA UtcT.
72      */

73     public static UtcT corbaTime(java.util.Date JavaDoc date)
74     {
75         return corbaTime(date.getTime());
76     }
77     
78     /**
79      * Returns a CORBA UtcT that represents an instant that lies
80      * a given number of CORBA time units (100 ns) in the future.
81      * If the argument is negative, returns null.
82      */

83     public static UtcT corbaFuture(long corbaUnits)
84     {
85         if (corbaUnits < 0)
86             return null;
87         else
88         {
89             UtcT result = corbaTime();
90             result.time = result.time + corbaUnits;
91             return result;
92         }
93     }
94     
95     /**
96      * Returns the number of milliseconds between now and the given CORBA
97      * time. The value is positive if that time is in the future, and
98      * negative otherwise.
99      */

100     public static long millisTo(UtcT time)
101     {
102         long unixTime = (time.time - UNIX_OFFSET) / 10000;
103         
104         // if the time is not UTC, correct time zone
105
if (time.tdf != 0)
106             unixTime = unixTime - (time.tdf * 60000);
107         
108         return unixTime - System.currentTimeMillis();
109     }
110
111     /**
112      * Returns true if the instant represented by the given UtcT is
113      * already in the past, false otherwise. As a special convenience,
114      * this method also returns false if the argument is null.
115      */

116     public static boolean hasPassed(UtcT time)
117     {
118         if (time != null)
119             return millisTo(time) < 0;
120         else
121             return false;
122     }
123
124     /**
125      * Compares two UtcT time values and returns that which is earlier.
126      * Either argument can be null; this is considered as a time that
127      * lies indefinitely in the future. If both arguments are null,
128      * this method returns null itself.
129      */

130     public static UtcT earliest(UtcT timeA, UtcT timeB)
131     {
132         if (timeA == null)
133             if (timeB == null)
134                 return null;
135             else
136                 return timeB;
137         else
138             if (timeB == null || timeA.time <= timeB.time)
139                 return timeA;
140             else
141                 return timeB;
142     }
143
144     /**
145      * Returns a CDR encapsulation of the given UtcT.
146      */

147     public static byte[] toCDR(UtcT time)
148     {
149         // TODO: make this more efficient with mere bit shifting
150
byte[] buffer = new byte[25];
151         CDROutputStream out = new CDROutputStream(buffer);
152         out.beginEncapsulatedArray();
153         UtcTHelper.write(out, time);
154         return buffer;
155     }
156     
157     /**
158      * Decodes a CDR encapsulation of a UtcT.
159      */

160     public static UtcT fromCDR(byte[] buffer)
161     {
162         CDRInputStream in = new CDRInputStream(null, buffer);
163         in.openEncapsulatedArray();
164         return UtcTHelper.read(in);
165     }
166     
167     /**
168      * This method blocks until the given time has been reached.
169      * If the time is null, or it has already passed,
170      * then this method returns immediately.
171      */

172     public static void waitFor(UtcT time)
173     {
174         if (time != null)
175         {
176             long delta = Time.millisTo(time);
177             if (delta > 0)
178             {
179                 Object JavaDoc lock = new Object JavaDoc();
180                 synchronized (lock)
181                 {
182                     try
183                     {
184                         lock.wait(delta);
185                     }
186                     catch (InterruptedException JavaDoc e)
187                     {
188                     }
189                 }
190             }
191         }
192     }
193
194
195
196 }
197
Popular Tags