Returns the factor of the given unit from NanoSeconds

@param unit is the unit of time
@return the factor of the given unit from NanoSeconds
196524
public double factorOf(TimeUnit unit) {
        if (unit == TimeUnit.NanoSeconds) {
            return 1.0;
        } else if (unit == TimeUnit.MicroSeconds) {
            return 1.0e-3;
        } else if (unit == TimeUnit.MilliSeconds) {
            return 1.0e-6;
        } else if (unit == TimeUnit.Seconds) {
            return 1.0e-9;
        } else if (unit == TimeUnit.Minutes) {
            return 1.0e-9 / 60;
        } else if (unit == TimeUnit.Hours) {
            return 1.0e-9 / 3600;
        }
        return Double.NaN;
    }
1215674
public TimeDuration dividedBy(int factor) {
        return createWithCarry(sec / factor, ((sec % factor) * NANOSEC_IN_SEC + (long) nanoSec) / factor);
    }
1234246
public static TimeUnit toTimeUnit(final ChronoUnit tu) {
        if (tu == null) {
            return null;
        }
        switch (tu) {
            case DAYS:
                return TimeUnit.DAYS;
            case HOURS:
                return TimeUnit.HOURS;
            case MINUTES:
                return TimeUnit.MINUTES;
            case SECONDS:
                return TimeUnit.SECONDS;
            case MICROS:
                return TimeUnit.MICROSECONDS;
            case MILLIS:
                return TimeUnit.MILLISECONDS;
            case NANOS:
                return TimeUnit.NANOSECONDS;
            default:
                throw new UnsupportedOperationException("Temporal unit is not supported");
        }
    }
586782
@SuppressWarnings("unchecked")
   public <UNIT extends TimeUnit> UNIT getUnit(final Class<UNIT> unitType)
   {
      if (unitType == null)
         return null;

      for (TimeUnit unit : units.keySet()) {
         if (unitType.isAssignableFrom(unit.getClass()))
         {
            return (UNIT) unit;
         }
      }
      return null;
   }
989242
function getRes(unit) {
        var measured,
            begin,
            count = 30,
            divisor = 1e3,
            ns = timer.ns,
            sample = [];

        // Get average smallest measurable time.
        while (count--) {
          if (unit == 'us') {
            divisor = 1e6;
            if (ns.stop) {
              ns.start();
              while (!(measured = ns.microseconds())) {}
            } else {
              begin = ns();
              while (!(measured = ns() - begin)) {}
            }
          }
          else if (unit == 'ns') {
            divisor = 1e9;
            begin = (begin = ns())[0] + (begin[1] / divisor);
            while (!(measured = ((measured = ns())[0] + (measured[1] / divisor)) - begin)) {}
            divisor = 1;
          }
          else if (ns.now) {
            begin = ns.now();
            while (!(measured = ns.now() - begin)) {}
          }
          else {
            begin = new ns().getTime();
            while (!(measured = new ns().getTime() - begin)) {}
          }
          // Check for broken timers.
          if (measured > 0) {
            sample.push(measured);
          } else {
            sample.push(Infinity);
            break;
          }
        }
        // Convert to seconds.
        return getMean(sample) / divisor;
      }
558411
def get_conversion_factor(self, new_unit):
        
        uo_base, ofactor = self.as_base_units
        un_base, nfactor = Unit(new_unit).as_base_units
        units_new = sorted(un_base.items(),
                           key=lambda d: _UNAME2UTYPE[d[0]])
        units_old = sorted(uo_base.items(),
                           key=lambda d: _UNAME2UTYPE[d[0]])
        factor = ofactor / nfactor
        for uo, un in zip(units_old, units_new):
            if uo[1] != un[1]:
                raise UnitError("Units %s and %s are not compatible!" % (uo, un))
            c = ALL_UNITS[_UNAME2UTYPE[uo[0]]]
            factor *= (c[uo[0]] / c[un[0]]) ** uo[1]
        return factor
1143856
public TimeFormat removeUnit(final TimeUnit unit)
   {
      if (unit == null)
         return null;

      cachedUnits = null;

      return units.remove(unit);
   }
277131
public double convert(double time, TimeUnit from, TimeUnit to) {
        return time * factorOf(to) / factorOf(from);
    }
1277149
public List<UnitValue> sequence(UnitValue value, UnitFactorSet factorSet) {
    value = convert(value, factorSet.base());
    boolean negative = false;
    BigDecimal n = value.amount();
    
    // Use absolute value for all comparisons.
    if (n.signum() == -1) {
      negative = true;
      n = n.abs();
    }
    
    List<UnitValue> result = new ArrayList<>();
    
    List<UnitValue> factors = factorSet.factors();
    int size = factors.size();
    int last = size - 1;
    for (int i = 0; i < size; i++) {
      UnitValue factor = factors.get(i);
      BigDecimal divisor = factor.amount();
      if (divisor == null) {
        continue;
      }
      
      if (i == last && (n.compareTo(BigDecimal.ZERO) != 0 || result.isEmpty())) {
        // Include decimals on the last unit.
        int scale = n.precision() + divisor.precision();
        BigDecimal res = n.divide(divisor, scale, RoundingMode.HALF_EVEN);
        if (negative && result.isEmpty()) {
          res = res.negate();
        }
        result.add(new UnitValue(res.stripTrailingZeros(), factor.unit()));
        
      } else if (divisor.compareTo(n) <= 0) {
        BigDecimal[] res = n.divideAndRemainder(divisor);
        if (negative && result.isEmpty()) {
          res[0] = res[0].negate();
        }
        result.add(new UnitValue(res[0].stripTrailingZeros(), factor.unit()));
        n = res[1];
      }
    }

    return result;
  }
556183
public int dividedBy(TimeDuration duration) {
        // (a + b)/(c + d) = 1 / ((c + d) / a) + 1 / ((c + d) / b)
        // XXX This will not have the precision it should
        double thisDuration = (double) getSec() * (double) NANOSEC_IN_SEC + (double) getNanoSec();
        double otherDuration = (double) duration.getSec() * (double) NANOSEC_IN_SEC + (double) duration.getNanoSec();
        return (int) (thisDuration / otherDuration);
    }
1065269
@Pure
	public static double toSeconds(double value, TimeUnit inputUnit) {
		switch (inputUnit) {
		case DAYS:
			return value * 86400.;
		case HOURS:
			return value * 3600.;
		case MINUTES:
			return value * 60.;
		case SECONDS:
			break;
		case MILLISECONDS:
			return milli2unit(value);
		case MICROSECONDS:
			return micro2unit(value);
		case NANOSECONDS:
			return nano2unit(value);
		default:
			throw new IllegalArgumentException();
		}
		return value;
	}
603354
@javax.annotation.Nonnull
  public static TemporalUnit cvt(@javax.annotation.Nonnull final TimeUnit units) {
    switch (units) {
      case DAYS:
        return ChronoUnit.DAYS;
      case HOURS:
        return ChronoUnit.HOURS;
      case MINUTES:
        return ChronoUnit.MINUTES;
      case SECONDS:
        return ChronoUnit.SECONDS;
      case NANOSECONDS:
        return ChronoUnit.NANOS;
      case MICROSECONDS:
        return ChronoUnit.MICROS;
      case MILLISECONDS:
        return ChronoUnit.MILLIS;
      default:
        throw new IllegalArgumentException(units.toString());
    }
  }
737774
public static TimeUnit getParentUnit(TimeUnit unit)
    {
        TimeUnit result = null;

        switch (unit)
        {
        case HOURS:
            result = TimeUnit.DAYS;
            break;
        case MINUTES:
            result = TimeUnit.HOURS;
            break;
        case SECONDS:
            result = TimeUnit.MINUTES;
            break;
        case MILLISECONDS:
            result = TimeUnit.SECONDS;
            break;
        case MICROSECONDS:
            result = TimeUnit.MILLISECONDS;
            break;
        case NANOSECONDS:
            result = TimeUnit.MICROSECONDS;
            break;
        default:
            break;
        }

        return result;
    }
1022437
def convert_time_units(t):
    
    if t == 0:
        return '0 s'
    order = log10(t)
    if -9 < order < -6:
        time_units = 'ns'
        factor = 1000000000
    elif -6 <= order < -3:
        time_units = 'us'
        factor = 1000000
    elif -3 <= order < -1:
        time_units = 'ms'
        factor = 1000.
    elif -1 <= order:
        time_units = 's'
        factor = 1
    return "{:.3f} {}".format(factor * t, time_units)
257565
public UnitValue convert(UnitValue value, UnitFactorSet factorSet) {
    value = convert(value, factorSet.base());
    BigDecimal n = value.amount();
    
    // Use absolute value to simplify comparisons with divisors
    boolean negative = false;
    if (n.signum() == -1) {
      negative = true;
      n = n.abs();
    }
    
    List<UnitValue> factors = factorSet.factors();
    int size = factors.size();
    int last = size - 1;
    for (int i = 0; i < size; i++) {
      UnitValue factor = factors.get(i);
      BigDecimal divisor = factor.amount();
      if (divisor == null) {
        continue;
      }
      
      // Use this divisor if it is <= the number or if we've run out of
      // factors to try.
      if (divisor.compareTo(n) <= 0 || i == last) {
        int scale = n.precision() + divisor.precision();
        n = n.divide(divisor, scale, RoundingMode.HALF_EVEN);
        if (negative) {
          n = n.negate();
        }
        return new UnitValue(n.stripTrailingZeros(), factor.unit());
      }
    }

    // Should only be reached if list of factors is empty.
    return value;
  }
1326544
public UnitFactor get(Unit from, Unit to) {
    Map<Unit, UnitFactor> map = factors.get(from);
    if (map != null) {
      UnitFactor factor = map.get(to);
      if (factor != null) {
        return factor;
      }
    }
    return null;
  }
1103903
Instant getInstant() {
        // long tsSeconds = TimeUnit.NANOSECONDS.toSeconds(this.timestamp);
        long tsSeconds = this.timestamp / 1000000000L;
        long tsNanos = this.timestamp - (tsSeconds * 1000000000L);
        return Instant.ofEpochSecond(tsSeconds).plusNanos(tsNanos);
    }