1 package org.jacorb.util; 2 3 23 24 import org.omg.TimeBase.*; 25 import org.jacorb.orb.*; 26 27 33 public class Time 34 { 35 39 public static final long UNIX_OFFSET = 122192928000000000L; 40 41 44 public static UtcT corbaTime() 45 { 46 return corbaTime(System.currentTimeMillis()); 47 } 48 49 53 public static UtcT corbaTime(long unixTime) 54 { 55 UtcT result = new UtcT(); 56 57 result.time = (unixTime * 10000) + UNIX_OFFSET; 58 59 result.tdf = 0; 62 63 result.inacchi = 0; 65 result.inacclo = 0; 66 67 return result; 68 } 69 70 73 public static UtcT corbaTime(java.util.Date date) 74 { 75 return corbaTime(date.getTime()); 76 } 77 78 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 100 public static long millisTo(UtcT time) 101 { 102 long unixTime = (time.time - UNIX_OFFSET) / 10000; 103 104 if (time.tdf != 0) 106 unixTime = unixTime - (time.tdf * 60000); 107 108 return unixTime - System.currentTimeMillis(); 109 } 110 111 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 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 147 public static byte[] toCDR(UtcT time) 148 { 149 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 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 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 lock = new Object (); 180 synchronized (lock) 181 { 182 try 183 { 184 lock.wait(delta); 185 } 186 catch (InterruptedException e) 187 { 188 } 189 } 190 } 191 } 192 } 193 194 195 196 } 197 | Popular Tags |