/** 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); }
/** * Extract the simple unit string (no number), eg "s" or "m". * @return unit string with no value */ public String getUnitString() { return uu.getDerivedUnit().toString(); }
/** * Return true if this ucar.units.Unit is convertible to secs. * * @param uu check this Unit * @return true if its a Time */ static public boolean isTimeUnit(ucar.units.Unit uu) { return uu.isCompatible(secsUnit); }
/** * Return true if this ucar.units.Unit is a Date. * * @param uu check this Unit * @return true if its a Date */ static public boolean isDateUnit(ucar.units.Unit uu) { boolean ok = uu.isCompatible(dateReferenceUnit); if (!ok) return false; try { uu.getConverterTo(dateReferenceUnit); return true; } catch (ConversionException e) { return false; } }
/** * Indicates if numeric values in this unit are convertible with another * unit. * * @param that * The other unit. * @return <code>true</code> if and only if numeric values in this unit are * convertible the other unit. */ public boolean isCompatible(final Unit that) { // jeffm: for some reason just calling getDerivedUnit().equals(...) // with jikes 1.1.7 as the compiler causes the jvm to crash. // The Unit u1=... does not crash. final Unit u1 = getDerivedUnit(); return u1.equals(that.getDerivedUnit()); // return getDerivedUnit().equals(that.getDerivedUnit()); }
System.out.println("t1 = " + t1); System.out.println("t2 = " + t2); System.out.println("t1.isCompatible(t2) = " + t1.isCompatible(t2)); System.out.println("t2.convertTo(0.0, t1) = " + t2.convertTo(0.0, t1)); t1 = format.parse("years since 1930-07-27"); t1 = format.parse("years since 1930"); + '"'); System.out.println("format.parse(\"min\").toString() = \"" + format.parse("min").toString() + '"'); System.out.println("format.parse(\"60 s\") = \"" + format.parse("60 s") + '"'); + '"'); System.out.println("format.parse(\"Cel\").toString() = \"" + format.parse("Cel").toString() + '"'); System.out .println("format.parse(\"min @ 1970-01-01 00:00:00 UTC\") = \"" System.out .println("format.parse(\"min @ 2000-01-01 00 UTC\").toString() = \"" + format.parse("min @ 2000-01-01 00 UTC").toString() + '"'); System.out.println("format.parse(\"g/kg\") = \"" + format.parse("g/kg") Unit kelvin = format.parse("K"); System.out.println("celsius.getConverterTo(kelvin).convert(0) = \"" + celsius.getConverterTo(kelvin).convert(0) + '"'); System.out
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)); }
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)); }
final Unit s = parser.parse("s"); final Unit epoch = parser.parse("s @ 1970-01-01 00:00:00 UTC"); myAssert(parser, "m m", m.multiplyBy(m)); myAssert(parser, "m.m", m.multiplyBy(m)); myAssert(parser, "(m)(m)", m.multiplyBy(m)); myAssert(parser, "m/s/s", m.divideBy(s).divideBy(s)); myAssert(parser, "m2", m.raiseTo(2)); myAssert(parser, "m2.s", m.raiseTo(2).multiplyBy(s)); myAssert(parser, "m2/s", m.raiseTo(2).divideBy(s)); myAssert(parser, "m^2/s", m.raiseTo(2).divideBy(s)); myAssert(parser, "m s @ 5", m.multiplyBy(s).shiftTo(5.0)); myAssert(parser, "m2 s @ 5", m.raiseTo(2).multiplyBy(s).shiftTo(5)); myAssert(parser, "m2 s-1 @ 5", m.raiseTo(2).multiplyBy(s.raiseTo(-1)) .shiftTo(5)); myAssert(parser, "m s from 5", m.multiplyBy(s).shiftTo(5)); myAssert(parser, "s@19700101", epoch); myAssert(parser, "s@19700101T000000", epoch); myAssert(parser, "s @ 1970-01-01 00:00:00.00 +0", epoch); myAssert(parser, "s @ 1970-01-01T00:00:00.00 -12", epoch .shiftTo(new Date(12 * 60 * 60 * 1000))); if (!parser.parse("days since 2009-06-14 04:00:00").equals( parser.parse("days since 2009-06-14 04:00:00 +00:00"))) { throw new AssertionError(); myAssert(parser, "0.1 lg(re 1 mm)", m.multiplyBy(1e-3).log(10) .multiplyBy(0.1)); myAssert(parser, "m", m); myAssert(parser, "2 m s", m.multiplyBy(s).multiplyBy(2));
/** * Unit string representation. */ public String toString() { return uu.toString(); }
/** * Factory method for creating a unit converter. * @param fromUnit The unit from which to convert. * @param toUnit The unit to which to convert. * @throws ConversionException The units are not convertible. */ public static Converter create(Unit fromUnit, Unit toUnit) throws ConversionException { return fromUnit.getConverterTo(toUnit); }
/** * Divides this derived unit into another. * * @param that * The other unit. * @return The quotient of the two units. * @throws OperationException * Can't divide these units. */ @Override protected Unit myDivideInto(final Unit that) throws OperationException { return that.divideBy(this); }
jj_consume_token(DIVIDE); unit2 = powerExpr(unitDB); unit = unit.divideBy(unit2); break; default: unit = unit.multiplyBy(unit2); } else { jj_consume_token(-1);
public String getCanonicalString() { return uu.getCanonicalString(); }
System.out.println("t1 = " + t1); System.out.println("t2 = " + t2); System.out.println("t1.isCompatible(t2) = " + t1.isCompatible(t2)); System.out.println("t2.convertTo(0.0, t1) = " + t2.convertTo(0.0, t1)); t1 = format.parse("years since 1930-07-27"); t1 = format.parse("years since 1930"); Unit kelvin = format.parse("K"); System.out.println("celsius.getConverterTo(kelvin).convert(0) = \"" + celsius.getConverterTo(kelvin).convert(0) + '"'); System.out .println("kelvin.getConverterTo(celsius).convert(273.15) = \"" + kelvin.getConverterTo(celsius).convert(273.15) + '"'); myAssert(kelvin.isCompatible(celsius)); myAssert(!kelvin.equals(celsius)); myAssert(kelvin.hashCode() != celsius.hashCode()); Unit fahrenheit = format.parse("fahrenheit"); fahrenheit = fahrenheit.clone(UnitName.newUnitName("fahrenheit")); kelvin = kelvin.clone(UnitName.newUnitName("kelvin")); myAssert(kelvin.isCompatible(fahrenheit)); myAssert(fahrenheit.isCompatible(kelvin)); final Unit kelvinUnit = format.parse("kelvin").clone( UnitName.newUnitName("kelvin")); final Unit fahrenUnit = format.parse("fahrenheit").clone( UnitName.newUnitName("fahrenheit")); myAssert(kelvinUnit.isCompatible(fahrenUnit)); final Unit second = format.parse("s"); final Unit minute = format.parse("min"); System.out.println("second.getConverterTo(minute).convert(60) = \""
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)); }
/** * Return true if this ucar.units.Unit is a Date. * * @param uu check this Unit * @return true if its a Date */ static public boolean isDateUnit(ucar.units.Unit uu) { boolean ok = uu.isCompatible(dateReferenceUnit); if (!ok) return false; try { uu.getConverterTo(dateReferenceUnit); return true; } catch (ConversionException e) { return false; } }
final Unit s = parser.parse("s"); final Unit epoch = parser.parse("s @ 1970-01-01 00:00:00 UTC"); myAssert(parser, "m m", m.multiplyBy(m)); myAssert(parser, "m.m", m.multiplyBy(m)); myAssert(parser, "(m)(m)", m.multiplyBy(m)); myAssert(parser, "m/s/s", m.divideBy(s).divideBy(s)); myAssert(parser, "m2", m.raiseTo(2)); myAssert(parser, "m2.s", m.raiseTo(2).multiplyBy(s)); myAssert(parser, "m2/s", m.raiseTo(2).divideBy(s)); myAssert(parser, "m^2/s", m.raiseTo(2).divideBy(s)); myAssert(parser, "m s @ 5", m.multiplyBy(s).shiftTo(5.0)); myAssert(parser, "m2 s @ 5", m.raiseTo(2).multiplyBy(s).shiftTo(5)); myAssert(parser, "m2 s-1 @ 5", m.raiseTo(2).multiplyBy(s.raiseTo(-1)) .shiftTo(5)); myAssert(parser, "m s from 5", m.multiplyBy(s).shiftTo(5)); myAssert(parser, "s@19700101", epoch); myAssert(parser, "s@19700101T000000", epoch); myAssert(parser, "s @ 1970-01-01 00:00:00.00 +0", epoch); myAssert(parser, "s @ 1970-01-01T00:00:00.00 -12", epoch .shiftTo(new Date(12 * 60 * 60 * 1000))); if (!parser.parse("days since 2009-06-14 04:00:00").equals( parser.parse("days since 2009-06-14 04:00:00 +00:00"))) { throw new AssertionError(); myAssert(parser, "0.1 lg(re 1 mm)", m.multiplyBy(1e-3).log(10) .multiplyBy(0.1)); myAssert(parser, "m", m); myAssert(parser, "2 m s", m.multiplyBy(s).multiplyBy(2));
/** * Unit string representation. */ public String toString() { return uu.toString(); }
/** * Indicates if numeric values in this unit are convertible with another * unit. * * @param that * The other unit. * @return <code>true</code> if and only if numeric values in this unit are * convertible the other unit. */ public boolean isCompatible(final Unit that) { // jeffm: for some reason just calling getDerivedUnit().equals(...) // with jikes 1.1.7 as the compiler causes the jvm to crash. // The Unit u1=... does not crash. final Unit u1 = getDerivedUnit(); return u1.equals(that.getDerivedUnit()); // return getDerivedUnit().equals(that.getDerivedUnit()); }