001: package org.jacorb.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 2002-2004 Gerald Brose
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: import org.omg.TimeBase.*;
025: import org.jacorb.orb.*;
026:
027: /**
028: * Contains static methods to handle CORBA time values.
029: *
030: * @author Andre Spiegel <spiegel@gnu.org>
031: * @version $Id: Time.java,v 1.10 2006/07/07 10:57:59 alphonse.bendt Exp $
032: */
033: public class Time {
034: private Time() {
035: // utility class
036: super ();
037: }
038:
039: /**
040: * Difference between the CORBA Epoch and the Unix Epoch: the time
041: * from 1582/10/15 00:00 until 1970/01/01 00:00 in 100 ns units.
042: */
043: public static final long UNIX_OFFSET = 122192928000000000L;
044:
045: /**
046: * Returns the current time as a CORBA UtcT.
047: */
048: public static UtcT corbaTime() {
049: return corbaTime(System.currentTimeMillis());
050: }
051:
052: /**
053: * Converts the given unixTime into a CORBA UtcT.
054: * @param unixTime the number of milliseconds since 1970/01/01 00:00 UTC.
055: */
056: public static UtcT corbaTime(long unixTime) {
057: UtcT result = new UtcT();
058:
059: result.time = (unixTime * 10000) + UNIX_OFFSET;
060:
061: // unixTime is always UTC.
062: // Therefore, no time zone offset.
063: result.tdf = 0;
064:
065: // nothing reasonable to put here
066: result.inacchi = 0;
067: result.inacclo = 0;
068:
069: return result;
070: }
071:
072: /**
073: * Converts the given Java date into a CORBA UtcT.
074: */
075: public static UtcT corbaTime(java.util.Date date) {
076: return corbaTime(date.getTime());
077: }
078:
079: /**
080: * Returns a CORBA UtcT that represents an instant that lies
081: * a given number of CORBA time units (100 ns) in the future.
082: * If the argument is negative, returns null.
083: */
084: public static UtcT corbaFuture(long corbaUnits) {
085: if (corbaUnits < 0) {
086: return null;
087: }
088:
089: UtcT result = corbaTime();
090: result.time = result.time + corbaUnits;
091: return result;
092: }
093:
094: /**
095: * Returns the number of milliseconds between now and the given CORBA
096: * time. The value is positive if that time is in the future, and
097: * negative otherwise.
098: */
099: public static long millisTo(UtcT time) {
100: long unixTime = (time.time - UNIX_OFFSET) / 10000;
101:
102: // if the time is not UTC, correct time zone
103: if (time.tdf != 0) {
104: unixTime = unixTime - (time.tdf * 60000);
105: }
106:
107: return unixTime - System.currentTimeMillis();
108: }
109:
110: /**
111: * Returns true if the instant represented by the given UtcT is
112: * already in the past, false otherwise. As a special convenience,
113: * this method also returns false if the argument is null.
114: */
115: public static boolean hasPassed(UtcT time) {
116: if (time != null) {
117: return millisTo(time) < 0;
118: }
119:
120: return false;
121: }
122:
123: /**
124: * Compares two UtcT time values and returns that which is earlier.
125: * Either argument can be null; this is considered as a time that
126: * lies indefinitely in the future. If both arguments are null,
127: * this method returns null itself.
128: */
129: public static UtcT earliest(UtcT timeA, UtcT timeB) {
130: if (timeA == null) {
131: if (timeB == null) {
132: return null;
133: }
134: return timeB;
135: }
136:
137: if (timeB == null || timeA.time <= timeB.time) {
138: return timeA;
139: }
140: return timeB;
141: }
142:
143: /**
144: * Returns a CDR encapsulation of the given UtcT.
145: */
146: public static byte[] toCDR(UtcT time) {
147: // TODO: make this more efficient with mere bit shifting
148: byte[] buffer = new byte[25];
149: CDROutputStream out = new CDROutputStream(buffer);
150: out.beginEncapsulatedArray();
151: UtcTHelper.write(out, time);
152: return out.getBufferCopy();
153: }
154:
155: /**
156: * Decodes a CDR encapsulation of a UtcT.
157: */
158: public static UtcT fromCDR(byte[] buffer) {
159: CDRInputStream in = new CDRInputStream(null, buffer);
160: in.openEncapsulatedArray();
161: return UtcTHelper.read(in);
162: }
163:
164: /**
165: * This method blocks until the given time has been reached.
166: * If the time is null, or it has already passed,
167: * then this method returns immediately.
168: */
169: public static void waitFor(UtcT time) {
170: if (time != null) {
171: long now = System.currentTimeMillis();
172: long delta = Time.millisTo(time);
173: long then = now + delta;
174:
175: while (delta > 0) {
176: try {
177: Thread.sleep(delta);
178: } catch (InterruptedException e) {
179: // ignored
180: }
181:
182: delta = then - System.currentTimeMillis();
183: }
184: }
185: }
186: }
|