1 10 package org.mmbase.util; 11 12 import java.util.*; 13 import org.mmbase.util.logging.*; 14 15 24 public class RelativeTime { 25 26 private final static int HOUR_POS = 0; 28 private final static int MINUTE_POS = 1; 29 private final static int SECOND_POS = 2; 30 private final static int MILLI_POS = 3; 31 32 private static Logger log = Logging.getLoggerInstance(RelativeTime.class.getName()); 34 35 40 public static int getHours(int timeValue) { 41 return getTimeValue(timeValue,HOUR_POS); 42 } 43 44 49 public static int getMinutes(int timeValue) { 50 return getTimeValue(timeValue,MINUTE_POS); 51 } 52 53 58 public static int getSeconds(int timeValue) { 59 return getTimeValue(timeValue,SECOND_POS); 60 } 61 62 67 public static int getMillis(int timeValue) { 68 return getTimeValue(timeValue,MILLI_POS); 69 } 70 71 78 private static int getTimeValue(int timeValue,int timePos) { 79 int HOUR_IN_MILLIS = 60*60*1000; 80 int MINUTE_IN_MILLIS = 60*1000; 81 int SECOND_IN_MILLIS = 1000; 82 int timeLeft, hoursLeft, minutesLeft, secondsLeft, millisLeft; 83 84 if (timeValue >= 0) { 85 timeLeft = timeValue; 86 hoursLeft = timeLeft / HOUR_IN_MILLIS; 87 if (timePos == HOUR_POS) { 88 return hoursLeft; 89 } 90 91 timeLeft -= hoursLeft * HOUR_IN_MILLIS; 92 minutesLeft = timeLeft / MINUTE_IN_MILLIS; 93 if (timePos == MINUTE_POS) { 94 return minutesLeft; 95 } 96 97 timeLeft -= minutesLeft * MINUTE_IN_MILLIS; 98 secondsLeft = timeLeft / SECOND_IN_MILLIS; 99 if (timePos == SECOND_POS) { 100 return secondsLeft; 101 } 102 103 timeLeft -= secondsLeft * SECOND_IN_MILLIS; 104 millisLeft = timeLeft; 105 if (timePos == MILLI_POS) { 106 return millisLeft; 107 } 108 109 log.warn("Invalid timePos used -> timePos="+timePos+" returning -1"); 111 return -1; 112 113 } else { log.warn("Negative timeValue used at position "+timePos+" -> timeValue="+timeValue+" returning -1"); 115 return -1; 116 } 117 } 118 119 124 public static String convertIntToTime(int timeValue) { 125 126 int h = getHours(timeValue); 127 int m = getMinutes(timeValue); 128 int s = getSeconds(timeValue); 129 int ms = getMillis(timeValue); 130 if (!testTimeValue(h,HOUR_POS)) h = -1; 131 if (!testTimeValue(m,MINUTE_POS)) m = -1; 132 if (!testTimeValue(s,SECOND_POS)) s = -1; 133 if (!testTimeValue(ms,MILLI_POS)) ms= -1; 134 135 if (ms < 0) { 137 return (h+":"+m+":"+s+"."+ms); 138 } else if ((ms/100.0) >= 1) { 139 return (h+":"+m+":"+s+"."+ms); 140 } else if ((ms/10.0) >= 1) { 141 return (h+":"+m+":"+s+".0"+ms); 142 } else { 143 return (h+":"+m+":"+s+".00"+ms); 144 } 145 } 146 147 155 public static int convertTimeToInt(int h,int m,int s,int ms) { 156 if (ms < 0) { 158 return convertTimeToInt(h+":"+m+":"+s+"."+ms); 159 } else if ((ms/100.0) >= 1) { 160 return convertTimeToInt(h+":"+m+":"+s+"."+ms); 161 } else if ((ms/10.0) >= 1) { 162 return convertTimeToInt(h+":"+m+":"+s+".0"+ms); 163 } else { 164 return convertTimeToInt(h+":"+m+":"+s+".00"+ms); 165 } 166 } 167 168 173 public static int convertTimeToInt(String time) { 174 int result = 0; String attrStrValues[] = new String [4]; 176 177 int timePos=0; 179 StringTokenizer st = new StringTokenizer(time, ":."); 180 while (st.hasMoreTokens()) { 181 attrStrValues[timePos] = new String (st.nextToken()); 182 timePos++; 183 } 184 185 for (int i=0; i<attrStrValues.length; i++) { 187 if (!testTimeValue(Integer.parseInt(attrStrValues[i]),i)) { 188 return -1; 189 } 190 } 191 192 int attrIntValue = Integer.parseInt(attrStrValues[HOUR_POS]); 194 result += attrIntValue*3600*1000; 195 attrIntValue = Integer.parseInt(attrStrValues[MINUTE_POS]); 196 result += attrIntValue*60*1000; 197 attrIntValue = Integer.parseInt(attrStrValues[SECOND_POS]); 198 result += attrIntValue*1000; 199 200 attrIntValue = Integer.parseInt(attrStrValues[MILLI_POS]); 202 if (attrStrValues[MILLI_POS].length() == 3) { 203 result += attrIntValue; 204 } else if (attrStrValues[MILLI_POS].length() == 2) { 205 result += 10 * attrIntValue; 206 } else if (attrStrValues[MILLI_POS].length() == 1) { 207 result += 100 * attrIntValue; 208 } 209 210 return result; 211 } 212 213 219 220 private static boolean testTimeValue(int timeAttrValue, int timePos) { 221 if (timeAttrValue < 0) { 223 log.warn("Negative timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false"); 224 return false; 225 } 226 if (timePos == HOUR_POS) { 227 if (timeAttrValue >23) { 228 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false"); 229 return false; 230 } 231 } else if (timePos == MINUTE_POS) { 232 if (timeAttrValue >59) { 233 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false"); 234 return false; 235 } 236 } else if (timePos == SECOND_POS) { 237 if (timeAttrValue >59) { 238 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false"); 239 return false; 240 } 241 } else if (timePos == MILLI_POS) { 242 if (timeAttrValue >999) { 243 log.warn("Invalid timeAttrValue used at position "+timePos+" -> timeAttrValue="+timeAttrValue+" returning false"); 244 return false; 245 } 246 } else { log.warn("Invalid timePos provided -> timePos="+timePos+" returning false"); 248 return false; 249 } 250 251 return true; 252 } 253 254 258 public static void main(String args[]) { 259 Enumeration e; 261 String timeKey; 262 int timeValue; 263 Properties testProps = new Properties(); 264 testProps.put("23:59:59.999", new Integer (86399999)); 265 testProps.put("0:0:0.0" , new Integer (0)); 266 testProps.put("23:0:0.0" , new Integer (82800000)); 267 testProps.put("0:59:0.0" , new Integer (3540000)); 268 testProps.put("0:0:59.0" , new Integer (59000)); 269 testProps.put("0:0:0.999" , new Integer (999)); 270 testProps.put("1:33:59.5" , new Integer (5639500)); 271 testProps.put("1:33:59.52" , new Integer (5639520)); 272 testProps.put("1:33:59.521" , new Integer (5639521)); 273 testProps.put("1:33:59.012" , new Integer (5639012)); 274 testProps.put("1:33:09.002" , new Integer (5589002)); 275 testProps.put("24:33:09.002", new Integer (88389002)); 276 testProps.put("0:0:2.100" , new Integer (2100)); 277 testProps.put("0:0:2.010" , new Integer (2010)); 278 279 log.info("----------------------------------------"); 280 log.info("|Testing RelativeTime util class |"); 281 log.info("----------------------------------------"); 282 283 285 if ((args.length <1) || (args.length >1)) { 286 log.info("Usage: RelativeTime methodName"); 287 log.info("Methods available: getHours,getMinutes,getSeconds,getMillis,convertIntToTime,convertTimeToInt,convertTimeToInt2"); 288 } else { 289 if (args[0].equals("getHours")) { 290 log.info("Testing method: "+args[0]); 291 e = testProps.keys(); 292 while (e.hasMoreElements()) { 293 timeKey = (String )e.nextElement(); 294 timeValue = ((Integer )testProps.get(timeKey)).intValue(); 295 log.info("getHours using time = "+timeKey+" ->"+getHours(timeValue)); 296 } 297 } else if (args[0].equals("getMinutes")) { 298 log.info("Testing method: "+args[0]); 299 e = testProps.keys(); 300 while (e.hasMoreElements()) { 301 timeKey = (String )e.nextElement(); 302 timeValue = ((Integer )testProps.get(timeKey)).intValue(); 303 log.info("getMinutes using time = "+timeKey+" ->"+getMinutes(timeValue)); 304 } 305 } else if (args[0].equals("getSeconds")) { 306 log.info("Testing method: "+args[0]); 307 e = testProps.keys(); 308 while (e.hasMoreElements()) { 309 timeKey = (String )e.nextElement(); 310 timeValue = ((Integer )testProps.get(timeKey)).intValue(); 311 log.info("getSeconds using time = "+timeKey+" ->"+getSeconds(timeValue)); 312 } 313 } else if (args[0].equals("getMillis")) { 314 log.info("Testing method: "+args[0]); 315 e = testProps.keys(); 316 while (e.hasMoreElements()) { 317 timeKey = (String )e.nextElement(); 318 timeValue = ((Integer )testProps.get(timeKey)).intValue(); 319 log.info("getMillis using time = "+timeKey+" ->"+getMillis(timeValue)); 320 } 321 } else if (args[0].equals("convertIntToTime")) { 322 log.info("Testing method: "+args[0]); 323 e = testProps.elements(); 324 while (e.hasMoreElements()) { 325 timeValue = ((Integer )e.nextElement()).intValue(); 326 log.info("convertIntToTime using timeValue = "+timeValue+" ->"+convertIntToTime(timeValue)); 327 } 328 } else if (args[0].equals("convertTimeToInt")) { 329 log.info("Testing method: "+args[0]); 330 e = testProps.keys(); 331 while (e.hasMoreElements()) { 332 timeKey = (String )e.nextElement(); 333 timeValue = convertTimeToInt(timeKey); 334 log.info("convertTimeToInt using timeKey="+timeKey+" , timeValue="+timeValue+" in testProps? "+testProps.contains(new Integer (timeValue))); 335 } 336 } else if (args[0].equals("convertTimeToInt2")) { 337 log.info("Testing method: "+args[0]); 338 timeValue = convertTimeToInt(4,33,9,32); 339 log.info("convertTimeToInt2 using 4:33:9:32 , timeValue="+timeValue); 340 } 341 } 342 } 343 } 344 | Popular Tags |