use of org.h2.value.ValueTime in project h2database by h2database.
the class TestDate method testValueTime.
private void testValueTime() {
assertEquals("10:20:30", ValueTime.get(Time.valueOf("10:20:30")).getString());
assertEquals("00:00:00", ValueTime.fromNanos(0).getString());
assertEquals("23:59:59", ValueTime.parse("23:59:59").getString());
assertEquals("11:22:33.444555666", ValueTime.parse("11:22:33.444555666").getString());
if (SysProperties.UNLIMITED_TIME_RANGE) {
assertEquals("99:59:59", ValueTime.parse("99:59:59").getString());
assertEquals("-00:10:10", ValueTime.parse("-00:10:10").getString());
assertEquals("-99:02:03.001002003", ValueTime.parse("-99:02:03.001002003").getString());
assertEquals("-99:02:03.001002", ValueTime.parse("-99:02:03.001002000").getString());
assertEquals("-99:02:03", ValueTime.parse("-99:02:03.0000000000001").getString());
assertEquals("1999999:59:59.999999999", ValueTime.parse("1999999:59:59.999999999").getString());
assertEquals("-1999999:59:59.999999999", ValueTime.parse("-1999999:59:59.999999999").getString());
assertEquals("2562047:47:16.854775807", ValueTime.fromNanos(Long.MAX_VALUE).getString());
assertEquals("-2562047:47:16.854775808", ValueTime.fromNanos(Long.MIN_VALUE).getString());
} else {
try {
ValueTime.parse("-00:00:00.000000001");
fail();
} catch (DbException ex) {
assertEquals(ErrorCode.INVALID_DATETIME_CONSTANT_2, ex.getErrorCode());
}
try {
ValueTime.parse("24:00:00");
fail();
} catch (DbException ex) {
assertEquals(ErrorCode.INVALID_DATETIME_CONSTANT_2, ex.getErrorCode());
}
}
ValueTime t1 = ValueTime.parse("11:11:11");
assertEquals("11:11:11", t1.getTime().toString());
assertEquals("1970-01-01", t1.getDate().toString());
assertEquals("TIME '11:11:11'", t1.getSQL());
assertEquals("TIME '11:11:11'", t1.toString());
assertEquals(1, t1.getSignum());
assertEquals(0, t1.multiply(ValueInt.get(0)).getSignum());
assertEquals(0, t1.subtract(t1).getSignum());
assertEquals("05:35:35.5", t1.multiply(ValueDouble.get(0.5)).getString());
assertEquals("22:22:22", t1.divide(ValueDouble.get(0.5)).getString());
assertEquals(Value.TIME, t1.getType());
long nanos = t1.getNanos();
assertEquals((int) ((nanos >>> 32) ^ nanos), t1.hashCode());
// Literals return maximum precision
assertEquals(ValueTime.MAXIMUM_PRECISION, t1.getDisplaySize());
assertEquals(ValueTime.MAXIMUM_PRECISION, t1.getPrecision());
assertEquals("java.sql.Time", t1.getObject().getClass().getName());
ValueTime t1b = ValueTime.parse("11:11:11");
assertTrue(t1 == t1b);
Value.clearCache();
t1b = ValueTime.parse("11:11:11");
assertFalse(t1 == t1b);
assertTrue(t1.equals(t1));
assertTrue(t1.equals(t1b));
assertTrue(t1b.equals(t1));
assertEquals(0, t1.compareTo(t1b, null));
assertEquals(0, t1b.compareTo(t1, null));
ValueTime t2 = ValueTime.parse("22:22:22");
assertFalse(t1.equals(t2));
assertFalse(t2.equals(t1));
assertEquals(-1, t1.compareTo(t2, null));
assertEquals(1, t2.compareTo(t1, null));
if (SysProperties.UNLIMITED_TIME_RANGE) {
assertEquals(-1, t1.negate().getSignum());
assertEquals("-11:11:11", t1.negate().getString());
assertEquals("11:11:11", t1.negate().negate().getString());
assertEquals("33:33:33", t1.add(t2).getString());
assertEquals("33:33:33", t1.multiply(ValueInt.get(4)).subtract(t1).getString());
// can't convert using java.util.Date
assertEquals("1969-12-31 23:00:00.0", ValueTime.parse("-1:00:00").convertTo(Value.TIMESTAMP).getString());
assertEquals("1970-01-01", ValueTime.parse("-1:00:00").convertTo(Value.DATE).getString());
}
}
Aggregations