/** Convert given value of this unit to the new unit. * <em>NOTE: the current value of this unit ignored, the given value is used instead. * This is different than ucar.units or SimpleUnit.</em> * @param value in the units of this "base unit" * @param outputUnit convert to this base type, must be convertible to units of "seconds" * @return new value in the units of the "outputUnit * @throws ConversionException */ public double convertTo(double value, TimeUnit outputUnit) throws ConversionException { return uu.convertTo( value, outputUnit.uu); }
/** Convert given value of this unit to the new unit. * <em>NOTE: the current value of this unit ignored, the given value is used instead. * This is different than ucar.units or SimpleUnit.</em> * @param value in the units of this "base unit" * @param outputUnit convert to this base type, must be convertible to units of "seconds" * @return new value in the units of the "outputUnit * @throws ConversionException */ public double convertTo(double value, TimeUnit outputUnit) throws ConversionException { return uu.convertTo( value, outputUnit.uu); }
/** Convert given value of this unit to the new unit. * <em>NOTE: the current value of this unit ignored, the given value is used instead. * This is different than ucar.units or SimpleUnit.</em> * @param value in the units of this "base unit" * @param outputUnit convert to this base type, must be convertible to units of "seconds" * @return new value in the units of the "outputUnit * @throws ConversionException */ public double convertTo(double value, TimeUnit outputUnit) throws ConversionException { return uu.convertTo( value, outputUnit.uu); }
/** * Constructor from a value and a unit name. * @param value amount of the unit. * @param unitString Time unit string from udunits. * @throws Exception if parse fails */ public TimeUnit(double value, String unitString) throws Exception { this.value = value; this.unitString = unitString; uu = SimpleUnit.makeUnit( unitString); factor = uu.convertTo( 1.0, SimpleUnit.secsUnit); }
/** * Constructor from a value and a unit name. * @param value amount of the unit. * @param unitString Time unit string from udunits. * @throws UnitException if parse fails */ public TimeUnit(double value, String unitString) throws UnitException { this.value = value; this.unitString = unitString; uu = SimpleUnit.makeUnit( unitString); factor = uu.convertTo( 1.0, SimpleUnit.secsUnit); }
/** * Constructor from a value and a unit name. * @param value amount of the unit. * @param unitString Time unit string from udunits. * @throws Exception if parse fails */ public TimeUnit(double value, String unitString) throws Exception { this.value = value; this.unitString = unitString; uu = SimpleUnit.makeUnit( unitString); factor = uu.convertTo( 1.0, SimpleUnit.secsUnit); }
/** * Convert given value of this unit to the new unit. * * @param value value in this unit * @param outputUnit convert to this unit * @return value in outputUnit * @throws IllegalArgumentException if outputUnit not convertible from this unit */ public double convertTo(double value, SimpleUnit outputUnit) throws IllegalArgumentException { try { return uu.convertTo(value, outputUnit.getUnit()); } catch (ConversionException e) { throw new IllegalArgumentException(e.getMessage()); } }
/** * Convert given value of this unit to the new unit. * * @param value value in this unit * @param outputUnit convert to this unit * @return value in outputUnit * @throws IllegalArgumentException if outputUnit not convertible from this unit */ public double convertTo(double value, SimpleUnit outputUnit) throws IllegalArgumentException { try { return uu.convertTo(value, outputUnit.getUnit()); } catch (ConversionException e) { throw new IllegalArgumentException(e.getMessage()); } }
/** * Convert given value of this unit to the new unit. * * @param value value in this unit * @param outputUnit convert to this unit * @return value in outputUnit * @throws IllegalArgumentException if outputUnit not convertible from this unit */ public double convertTo(double value, SimpleUnit outputUnit) throws IllegalArgumentException { try { return uu.convertTo(value, outputUnit.getUnit()); } catch (ConversionException e) { throw new IllegalArgumentException(e.getMessage()); } }
/** * Constructor from a String. * @param text [value] <time unit> eg "hours" or "13 hours". Time unit is from udunits. * @throws UnitException is bad format */ public TimeUnit(String text) throws UnitException { super(); if (text == null) { this.value = 1.0; this.unitString = "secs"; this.uu = SimpleUnit.makeUnit( unitString); // always a base unit return; } StringTokenizer stoker = new StringTokenizer(text); int ntoke = stoker.countTokens(); if (ntoke == 1) { this.value = 1.0; this.unitString = stoker.nextToken(); } else if (ntoke == 2){ this.value = Double.parseDouble(stoker.nextToken()); this.unitString = stoker.nextToken(); } else throw new IllegalArgumentException("Not TimeUnit = "+text); uu = SimpleUnit.makeUnit( unitString); // always a base unit factor = uu.convertTo( 1.0, SimpleUnit.secsUnit); }
/** * Constructor from a String. * @param text [value] <time unit> eg "hours" or "13 hours". Time unit is from udunits. * @throws Exception is bad format */ public TimeUnit(String text) throws Exception { super(); if (text == null) { this.value = 1.0; this.unitString = "secs"; this.uu = SimpleUnit.makeUnit( unitString); // always a base unit return; } StringTokenizer stoker = new StringTokenizer(text); int ntoke = stoker.countTokens(); if (ntoke == 1) { this.value = 1.0; this.unitString = stoker.nextToken(); } else if (ntoke == 2){ this.value = Double.parseDouble(stoker.nextToken()); this.unitString = stoker.nextToken(); } else throw new IllegalArgumentException("Not TimeUnit = "+text); uu = SimpleUnit.makeUnit( unitString); // always a base unit factor = uu.convertTo( 1.0, SimpleUnit.secsUnit); }
@Override public Unit shiftTo(final double origin) throws ShiftException { Date newOrigin; try { newOrigin = new Date(_origin.getTime() + (long) (_unit.convertTo(origin, SECOND) * 1000)); } catch (final ConversionException e) { throw (ShiftException) new ShiftException(this, origin) .initCause(e); } try { return new TimeScaleUnit(_unit, newOrigin); } catch (final BadUnitException e) { throw new AssertionError(); } catch (final UnitSystemException e) { throw (ShiftException) new ShiftException(this, origin) .initCause(e); } }
@Override public Unit shiftTo(final double origin) throws ShiftException { Date newOrigin; try { newOrigin = new Date(_origin.getTime() + (long) (_unit.convertTo(origin, SECOND) * 1000)); } catch (final ConversionException e) { throw (ShiftException) new ShiftException(this, origin) .initCause(e); } try { return new TimeScaleUnit(_unit, newOrigin); } catch (final BadUnitException e) { throw new AssertionError(); } catch (final UnitSystemException e) { throw (ShiftException) new ShiftException(this, origin) .initCause(e); } }
private double findConversion(double measure, String unit1, String unit2) { UnitDef unit = userDefinedUnits.get(unit1); if (unit != null) { return findConversion(unit.calibrator.toOutput(measure), unit.baseUnit, unit2); } unit = userDefinedUnits.get(unit2); if (unit != null) { return findConversion(unit.calibrator.toInput(measure), unit1, unit.baseUnit); } try { final Unit u1 = parseUnit(unit1); final Unit u2 = parseUnit(unit2); return u1.convertTo(measure, u2); } catch (final ConversionException e) { throw new IllegalArgumentException(e); } }
private double findConversion(double measure, String unit1, String unit2) { UnitDef unit = userDefinedUnits.get(unit1); if (unit != null) { return findConversion(unit.calibrator.toOutput(measure), unit.baseUnit, unit2); } unit = userDefinedUnits.get(unit2); if (unit != null) { return findConversion(unit.calibrator.toInput(measure), unit1, unit.baseUnit); } try { final Unit u1 = parseUnit(unit1); final Unit u2 = parseUnit(unit2); return u1.convertTo(measure, u2); } catch (final ConversionException e) { throw new IllegalArgumentException(e); } }
private double findConversion(double measure, String unit1, String unit2) { UnitDef unit = userDefinedUnits.get(unit1); if (unit != null) { return findConversion(unit.calibrator.toOutput(measure), unit.baseUnit, unit2); } unit = userDefinedUnits.get(unit2); if (unit != null) { return findConversion(unit.calibrator.toInput(measure), unit1, unit.baseUnit); } try { final Unit u1 = parseUnit(unit1); final Unit u2 = parseUnit(unit2); return u1.convertTo(measure, u2); } catch (final ConversionException e) { throw new IllegalArgumentException(e); } }
public void testTimeConversion() throws UnitException { UnitFormat format = UnitFormatManager.instance(); Unit t1, t2; t1 = format.parse("secs since 1999-01-01 00:00:00"); t2 = format.parse("secs since 1999-01-02 00:00:00"); assert(t1.isCompatible( t2)); logger.debug("t2.convertTo(0.0, t1) = {}", t2.convertTo(0.0, t1)); Assert2.assertNearlyEquals(86400.0, t2.convertTo(0.0, t1)); }
void doTime2(double value, String name, boolean ok) { ucar.units.UnitFormat format = UnitFormatManager.instance(); ucar.units.Unit timeUnit; try { timeUnit = format.parse("secs since 1970-01-01 00:00:00"); } catch (Exception e) { System.out.println("SimpleUnit initialization failed " + e); return; } ucar.units.Unit uu; try { uu = format.parse(name); } catch (Exception e) { System.out.println("Parse " + name + " got Exception " + e); return; } System.out.println("isCompatible=" + uu.isCompatible(timeUnit)); try { System.out.println("convert " + uu.convertTo(value, timeUnit)); } catch (Exception e) { System.out.println("convert " + name + " got Exception " + e); return; } }
public void testBasic() throws UnitException { UnitFormat format = UnitFormatManager.instance(); Unit meter = format.parse("meter"); Unit second = format.parse("second"); Unit meterPerSecondUnit = meter.divideBy(second); Unit knot = format.parse("knot"); assert (meterPerSecondUnit.isCompatible(knot)); logger.debug("5 knots is {} {}", knot.convertTo(5, meterPerSecondUnit), format.format(meterPerSecondUnit)); Assert2.assertNearlyEquals(2.5722222f, knot.convertTo(5, meterPerSecondUnit)); }
public void testTimeConversion2() throws UnitException { UnitFormat format = UnitFormatManager.instance(); Unit t1, t2; t1 = format.parse("hours since 1999-01-01 00:00:00"); t2 = format.parse("hours since 1999-01-02 00:00:00"); assert(t1.isCompatible( t2)); Assert2.assertNearlyEquals(24.0, t2.convertTo(0.0, t1)); }