1 29 30 package com.caucho.util; 31 32 35 public class TimeUnit { 36 public static final TimeUnit NANOSECONDS = new TimeUnit(1L); 37 public static final TimeUnit MICROSECONDS = new TimeUnit(1000L); 38 public static final TimeUnit MILLISECONDS = new TimeUnit(1000000L); 39 public static final TimeUnit SECONDS = new TimeUnit(1000000000L); 40 41 private final long _nanos; 42 43 private TimeUnit(long nanos) 44 { 45 _nanos = nanos; 46 } 47 48 public long convert(long duration, TimeUnit unit) 49 { 50 long sourceNanos = unit._nanos; 51 52 if (sourceNanos <= _nanos) 53 return duration * (_nanos / sourceNanos); 54 else 55 return duration / (sourceNanos / _nanos); 56 } 57 58 public long toMillis(long duration) 59 { 60 long msNanos = 1000000L; 61 62 if (msNanos <= _nanos) 63 return duration * (_nanos / msNanos); 64 else 65 return duration / (msNanos / _nanos); 66 } 67 } 68 | Popular Tags |