pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Make time/timestamp tests fair for binary transfers
5+ messages / 2 participants
[nested] [flat]

* Make time/timestamp tests fair for binary transfers
@ 2007-07-23 18:02  Mikko Tiihonen <mikko.tiihonen@iki.fi>
  0 siblings, 1 reply; 5+ messages in thread

From: Mikko Tiihonen @ 2007-07-23 18:02 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

The main purpose of the following patch is to make TimezoneTest fair
for binary transfers. The modified test cases now work both on text and
binary protocol (I'll post my new version of it soonish after I have
resolved all remaining unit test case failures).

The patch also alters what kind of java.sql.Time values are returned.
Before the patch the millisecond value of a ResultSet.getTime could be
between -24h<x<24h. The patch changes the returned value to be always
between 0h<=x<24h. If someone thinks this change is not acceptable I'll
go back and revert it re-fix the test cases to match.

The TimeTest was only altered because of the Time millisecond value
adjustments.

The TimezoneTest has a larger alteration. Previously the code did not
mostly test the parsing of time/date/timestamp values but instead
compared the values returned by ResultSet.getString, which are
formatted by the server.
The patched version does now two checks.
1) adds a new verify round that ensures that the insert was correct by
doing an explicit query with UTC timezone and casting all values to
varchar so that the server will format them.
2) Changes all other tests to use the getTime/getDate/getTimestamp
methods when comparing if the queried data is correct.

This basically means that the getString of a time type column is now
undefined and can change. But I think it's not a problem because
different server versions have had different string representation of
time already.

Attachments:

timetest-fixes.patch (text/x-patch, 26.8K) download diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/jdbc2/TimestampUtils.java pgjdbc/org/postgresql/jdbc2/TimestampUtils.java --- pgjdbc.alreadysent/org/postgresql/jdbc2/TimestampUtils.java 2007-04-16 21:31:38.000000000 +0300 +++ pgjdbc/org/postgresql/jdbc2/TimestampUtils.java 2007-07-23 19:56:25.000000000 +0300 @@ -26,6 +26,11 @@ * Misc utils for handling time and date values. */ public class TimestampUtils { + /** + * Number of milliseconds in one day. + */ + private static final int ONEDAY = 24 * 3600 * 1000; + private StringBuffer sbuf = new StringBuffer(); private Calendar defaultCal = new GregorianCalendar(); @@ -329,7 +334,13 @@ useCal.set(Calendar.SECOND, ts.second); useCal.set(Calendar.MILLISECOND, 0); - Timestamp result = new Timestamp(useCal.getTime().getTime()); + long millis = useCal.getTime().getTime(); + + if (!ts.hasDate && millis < 0) { + millis += ONEDAY; + } + + Timestamp result = new Timestamp(millis); result.setNanos(ts.nanos); showParse("timestamp", s, cal, result, useCal); return result; @@ -375,7 +386,12 @@ useCal.set(Calendar.MONTH, 0); useCal.set(Calendar.DAY_OF_MONTH, 1); - Time result = new Time(useCal.getTime().getTime()); + long millis = useCal.getTime().getTime(); + if (millis < 0) { + millis += ONEDAY; + } + + Time result = new Time(millis); showParse("time", s, cal, result, useCal); return result; } Only in pgjdbc/org/postgresql/jdbc2: TimestampUtils.java.~1.22.~ diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/test/jdbc2/TimeTest.java pgjdbc/org/postgresql/test/jdbc2/TimeTest.java --- pgjdbc.alreadysent/org/postgresql/test/jdbc2/TimeTest.java 2005-08-01 09:54:15.000000000 +0300 +++ pgjdbc/org/postgresql/test/jdbc2/TimeTest.java 2007-07-23 20:32:27.000000000 +0300 @@ -53,7 +53,7 @@ */ public void testGetTimeZone() throws Exception { - final Time midnight = new Time(0, 0, 0); + final Time midnight = makeTime(0, 0, 0); Statement stmt = con.createStatement(); Calendar cal = Calendar.getInstance(); @@ -81,7 +81,11 @@ assertEquals(midnight, time); time = rs.getTime(1, cal); - assertEquals(midnight.getTime() , time.getTime() - localOffset); + long millis = time.getTime() - localOffset; + if (millis < 0) { + millis += 24 * 3600 * 1000; + } + assertEquals(midnight.getTime() , millis); assertTrue(rs.next()); @@ -293,10 +297,19 @@ rs.close(); } + /** + * Returns a Time with given hour, minute and seconds in current time zone + * that has milliseconds value between 0 and 86400000. + */ private java.sql.Time makeTime(int h, int m, int s) { - return java.sql.Time.valueOf(TestUtil.fix(h, 2) + ":" + + Time t = java.sql.Time.valueOf(TestUtil.fix(h, 2) + ":" + TestUtil.fix(m, 2) + ":" + TestUtil.fix(s, 2)); + if (t.getTime() >= 0) { + return t; + } + + return new Time(t.getTime() + 24 * 3600 * 1000); } } diff -ur --exclude=CVS --exclude=build --exclude='*.jar' pgjdbc.alreadysent/org/postgresql/test/jdbc2/TimezoneTest.java pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java --- pgjdbc.alreadysent/org/postgresql/test/jdbc2/TimezoneTest.java 2007-07-23 20:27:16.000000000 +0300 +++ pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java 2007-07-23 20:33:55.000000000 +0300 @@ -15,6 +15,10 @@ import java.sql.*; import java.util.TimeZone; import java.util.Calendar; +import java.util.GregorianCalendar; + +import org.postgresql.PGStatement; +import org.postgresql.jdbc2.TimestampUtils; /* * Tests for time and date types with calendars involved. @@ -35,7 +39,7 @@ public class TimezoneTest extends TestCase { private Connection con; - private TimeZone saveTZ; + private static final TimeZone saveTZ = TimeZone.getDefault();; // // We set up everything in different timezones to try to exercise many cases: @@ -70,7 +74,6 @@ protected void setUp() throws Exception { // We must change the default TZ before establishing the connection. - saveTZ = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT+01")); // Arbitary timezone that doesn't match our test timezones con = TestUtil.openDB(); @@ -103,11 +106,13 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,t::varchar,tz::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03", + "2005-01-01"}); + Timestamp ts; @@ -181,9 +186,10 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("2005-01-01", rs.getString(3)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "2005-01-01"}); Date d; @@ -234,10 +240,11 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,t::varchar,tz::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03"}); Time t; @@ -251,7 +258,7 @@ t = rs.getTime(1, cGMT05); assertEquals(43200000L, t.getTime()); // 2005-01-01 07:00:00 -0500 -> 1970-01-01 07:00:00 -0500 t = rs.getTime(1, cGMT13); - assertEquals(-43200000L, t.getTime()); // 2005-01-02 01:00:00 +1300 -> 1970-01-01 01:00:00 +1300 + assertEquals(43200000L, t.getTime()); // 2005-01-02 01:00:00 +1300 -> 1970-01-01 01:00:00 +1300 // timestamp: 2005-01-01 15:00:00 t = rs.getTime(2); @@ -306,6 +313,7 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % (24 * 3600 * 1000)); // +0100 (JVM default) insertTimestamp.setInt(1, seq++); @@ -327,37 +335,40 @@ insertTimestamp.setTimestamp(2,instant,cGMT05); // 07:00:00 insertTimestamp.executeUpdate(); - if (min73) { // +1300 insertTimestamp.setInt(1, seq++); insertTimestamp.setTimestamp(2,instant,cGMT13); // 01:00:00 insertTimestamp.executeUpdate(); - } + + checkDatabaseContents("SELECT seq::varchar,t::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "13:00:00"}, + new String[]{"2", "12:00:00"}, + new String[]{"3", "15:00:00"}, + new String[]{"4", "07:00:00"}, + new String[]{"5", "01:00:00"}}); seq = 1; ResultSet rs = con.createStatement().executeQuery("SELECT seq,t FROM testtimezone ORDER BY seq"); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("13:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("12:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("07:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT05)); - if (min73) { - assertTrue(rs.next()); - assertEquals(seq++, rs.getInt(1)); - assertEquals("01:00:00", rs.getString(2)); - } + assertTrue(rs.next()); + assertEquals(seq++, rs.getInt(1)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT13)); assertTrue(!rs.next()); } @@ -369,6 +380,12 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % (24*3600*1000L)); + Timestamp instantDateJVM = new Timestamp(instant.getTime() - (instant.getTime() % (24*3600*1000L)) - TimeZone.getDefault().getRawOffset()); + Timestamp instantDateUTC = new Timestamp(instant.getTime() - (instant.getTime() % (24*3600*1000L)) - cUTC.getTimeZone().getRawOffset()); + Timestamp instantDateGMT03 = new Timestamp(instant.getTime() - (instant.getTime() % (24*3600*1000L)) - cGMT03.getTimeZone().getRawOffset()); + Timestamp instantDateGMT05 = new Timestamp(instant.getTime() - (instant.getTime() % (24*3600*1000L)) - cGMT05.getTimeZone().getRawOffset()); + Timestamp instantDateGMT13 = new Timestamp(instant.getTime() - (instant.getTime() % (24*3600*1000L)) - cGMT13.getTimeZone().getRawOffset() + 24*3600*1000); // +0100 (JVM default) insertTimestamp.setInt(1,seq++); @@ -414,6 +431,14 @@ insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,tstz::varchar,ts::varchar,tz::varchar,d::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2005-01-01 12:00:00+00", "2005-01-01 13:00:00", "13:00:00+01", "2005-01-01"}, + new String[]{"2", "2005-01-01 12:00:00+00", "2005-01-01 12:00:00", "12:00:00+00", "2005-01-01"}, + new String[]{"3", "2005-01-01 12:00:00+00", "2005-01-01 15:00:00", "15:00:00+03", "2005-01-01"}, + new String[]{"4", "2005-01-01 12:00:00+00", "2005-01-01 07:00:00", "07:00:00-05", "2005-01-01"}, + new String[]{"5", "2005-01-01 12:00:00+00", "2005-01-02 01:00:00", "01:00:00+13", "2005-01-02"}}); + // // check results // @@ -423,39 +448,39 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 13:00:00", rs.getString(3)); - assertEquals("13:00:00+01", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2)); + assertEquals(instant, rs.getTimestamp(3)); + assertEquals(instantTime, rs.getTimestamp(4)); + assertEquals(instantDateJVM, rs.getTimestamp(5)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 12:00:00", rs.getString(3)); - assertEquals("12:00:00+00", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cUTC)); + assertEquals(instant, rs.getTimestamp(3, cUTC)); + assertEquals(instantTime, rs.getTimestamp(4, cUTC)); + assertEquals(instantDateUTC, rs.getTimestamp(5, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT03)); + assertEquals(instant, rs.getTimestamp(3, cGMT03)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT03)); + assertEquals(instantDateGMT03, rs.getTimestamp(5, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 07:00:00", rs.getString(3)); - assertEquals("07:00:00-05", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT05)); + assertEquals(instant, rs.getTimestamp(3, cGMT05)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT05)); + assertEquals(instantDateGMT05, rs.getTimestamp(5, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-02 01:00:00", rs.getString(3)); - assertEquals("01:00:00+13", rs.getString(4)); - assertEquals("2005-01-02", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT13)); + assertEquals(instant, rs.getTimestamp(3, cGMT13)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT13)); + assertEquals(instantDateGMT13, rs.getTimestamp(5, cGMT13)); } assertTrue(!rs.next()); @@ -467,52 +492,59 @@ int seq = 1; - Date d; + Date dJVM, dUTC, dGMT03, dGMT05, dGMT13 = null; // +0100 (JVM default) - d = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 + dJVM = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d); // 2005-01-01 00:00:00 +0100 - insertTimestamp.setDate(3,d); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d); // 2005-01-01 + insertTimestamp.setDate(2,dJVM); // 2005-01-01 00:00:00 +0100 + insertTimestamp.setDate(3,dJVM); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dJVM); // 2005-01-01 insertTimestamp.executeUpdate(); // UTC - d = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 + dUTC = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cUTC); // 2005-01-01 00:00:00 +0000 - insertTimestamp.setDate(3,d,cUTC); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cUTC); // 2005-01-01 + insertTimestamp.setDate(2,dUTC,cUTC); // 2005-01-01 00:00:00 +0000 + insertTimestamp.setDate(3,dUTC,cUTC); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dUTC,cUTC); // 2005-01-01 insertTimestamp.executeUpdate(); // +0300 - d = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 + dGMT03 = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT03); // 2005-01-01 00:00:00 +0300 - insertTimestamp.setDate(3,d,cGMT03); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT03); // 2005-01-01 + insertTimestamp.setDate(2,dGMT03,cGMT03); // 2005-01-01 00:00:00 +0300 + insertTimestamp.setDate(3,dGMT03,cGMT03); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT03,cGMT03); // 2005-01-01 insertTimestamp.executeUpdate(); // -0500 - d = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 + dGMT05 = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT05); // 2005-01-01 00:00:00 -0500 - insertTimestamp.setDate(3,d,cGMT05); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT05); // 2005-01-01 + insertTimestamp.setDate(2,dGMT05,cGMT05); // 2005-01-01 00:00:00 -0500 + insertTimestamp.setDate(3,dGMT05,cGMT05); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT05,cGMT05); // 2005-01-01 insertTimestamp.executeUpdate(); if (min73) { // +1300 - d = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 + dGMT13 = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT13); // 2005-01-01 00:00:00 +1300 - insertTimestamp.setDate(3,d,cGMT13); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT13); // 2005-01-01 + insertTimestamp.setDate(2,dGMT13,cGMT13); // 2005-01-01 00:00:00 +1300 + insertTimestamp.setDate(3,dGMT13,cGMT13); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT13,cGMT13); // 2005-01-01 insertTimestamp.executeUpdate(); } insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,tstz::varchar,ts::varchar,d::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2004-12-31 23:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"2", "2005-01-01 00:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"3", "2004-12-31 21:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"4", "2005-01-01 05:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"5", "2004-12-31 11:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}}); // // check results // @@ -522,34 +554,34 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 02:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dJVM, rs.getDate(2)); + assertEquals(dJVM, rs.getDate(3)); + assertEquals(dJVM, rs.getDate(4)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 03:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dUTC, rs.getDate(2, cUTC)); + assertEquals(dUTC, rs.getDate(3, cUTC)); + assertEquals(dUTC, rs.getDate(4, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 00:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT03, rs.getDate(2, cGMT03)); + assertEquals(dGMT03, rs.getDate(3, cGMT03)); + assertEquals(dGMT03, rs.getDate(4, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 08:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT05, rs.getDate(2, cGMT05)); + assertEquals(dGMT05, rs.getDate(3, cGMT05)); + assertEquals(dGMT05, rs.getDate(4, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2004-12-31 14:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT13, rs.getDate(2, cGMT13)); + assertEquals(dGMT13, rs.getDate(3, cGMT13)); + assertEquals(dGMT13, rs.getDate(4, cGMT13)); } assertTrue(!rs.next()); @@ -567,45 +599,53 @@ int seq = 1; - Time t; + Time tJVM, tUTC, tGMT03, tGMT05, tGMT13; // +0100 (JVM default) - t = new Time(50400000L); // 1970-01-01 15:00:00 +0100 + tJVM = new Time(50400000L); // 1970-01-01 15:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t); // 15:00:00 - insertTimestamp.setTime(3,t); // 15:00:00+03 + insertTimestamp.setTime(2,tJVM); // 15:00:00 + insertTimestamp.setTime(3,tJVM); // 15:00:00+03 insertTimestamp.executeUpdate(); // UTC - t = new Time(54000000L); // 1970-01-01 15:00:00 +0000 + tUTC = new Time(54000000L); // 1970-01-01 15:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cUTC); // 15:00:00 - insertTimestamp.setTime(3,t,cUTC); // 15:00:00+00 + insertTimestamp.setTime(2,tUTC,cUTC); // 15:00:00 + insertTimestamp.setTime(3,tUTC,cUTC); // 15:00:00+00 insertTimestamp.executeUpdate(); // +0300 - t = new Time(43200000L); // 1970-01-01 15:00:00 +0300 + tGMT03 = new Time(43200000L); // 1970-01-01 15:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT03); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT03); // 15:00:00+03 + insertTimestamp.setTime(2,tGMT03,cGMT03); // 15:00:00 + insertTimestamp.setTime(3,tGMT03,cGMT03); // 15:00:00+03 insertTimestamp.executeUpdate(); // -0500 - t = new Time(72000000L); // 1970-01-01 15:00:00 -0500 + tGMT05 = new Time(72000000L); // 1970-01-01 15:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT05); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT05); // 15:00:00-05 + insertTimestamp.setTime(2,tGMT05,cGMT05); // 15:00:00 + insertTimestamp.setTime(3,tGMT05,cGMT05); // 15:00:00-05 insertTimestamp.executeUpdate(); // +1300 - t = new Time(7200000L); // 1970-01-01 15:00:00 +1300 + tGMT13 = new Time(7200000L); // 1970-01-01 15:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT13); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT13); // 15:00:00+13 + insertTimestamp.setTime(2,tGMT13,cGMT13); // 15:00:00 + insertTimestamp.setTime(3,tGMT13,cGMT13); // 15:00:00+13 insertTimestamp.executeUpdate(); insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,t::varchar,tz::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "15:00:00", "15:00:00+01",}, + new String[]{"2", "15:00:00", "15:00:00+00",}, + new String[]{"3", "15:00:00", "15:00:00+03",}, + new String[]{"4", "15:00:00", "15:00:00-05",}, + new String[]{"5", "15:00:00", "15:00:00+13",}}); + // // check results // @@ -615,28 +655,28 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+01", rs.getString(3)); + assertEquals(tJVM, rs.getTime(2)); + assertEquals(tJVM, rs.getTime(3)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+00", rs.getString(3)); + assertEquals(tUTC, rs.getTime(2, cUTC)); + assertEquals(tUTC, rs.getTime(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+03", rs.getString(3)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00-05", rs.getString(3)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+13", rs.getString(3)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); assertTrue(!rs.next()); } @@ -664,4 +704,34 @@ assertEquals(-1577929192000L, rs.getTimestamp(1).getTime()); } + /** + * Does a query in UTC time zone to database to check that the + * inserted values are correct. + * + * @param query The query to run. + * @param correct The correct answers in UTC time zone. + * @throws Exception + */ + private void checkDatabaseContents(String query, String[] correct) throws Exception { + checkDatabaseContents(query, new String[][]{correct}); + } + + private void checkDatabaseContents(String query, String[][] correct) throws Exception { + Connection con2 = TestUtil.openDB(); + Statement s = con2.createStatement(); + assertFalse(s.execute("set time zone 'UTC'")); + assertTrue(s.execute(query)); + ResultSet rs = s.getResultSet(); + for (int j = 0; j < correct.length; ++j) { + assertTrue(rs.next()); + for (int i = 0; i < correct[j].length; ++i) { + assertEquals("On row " + (j+1), correct[j][i], rs.getString(i+1)); + } + } + assertFalse(rs.next()); + rs.close(); + s.close(); + con2.close(); + } + }
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Make time/timestamp tests fair for binary transfers
@ 2007-07-23 22:30  Oliver Jowett <oliver@opencloud.com>
  parent: Mikko Tiihonen <mikko.tiihonen@iki.fi>
  0 siblings, 2 replies; 5+ messages in thread

From: Oliver Jowett @ 2007-07-23 22:30 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@iki.fi>; +Cc: pgsql-jdbc

Mikko Tiihonen wrote:

> The patch also alters what kind of java.sql.Time values are returned.
> Before the patch the millisecond value of a ResultSet.getTime could be
> between -24h<x<24h. The patch changes the returned value to be always
> between 0h<=x<24h. If someone thinks this change is not acceptable I'll
> go back and revert it re-fix the test cases to match.

I'm not sure if this is right. In theory Time values are meant to have a 
day/month/year of Jan 1 1970. Time (via Date) uses the local timezone to 
compute the day/month/year. But to represent all time values in all 
local timezones that means you need negative millisecond values (to 
handle timezones ahead of UTC). For example, Jan 1 1970 02:00 +1200 is 
actually Dec 31 1969 14:00 UTC, which is a negative milliseconds value.

There's an additional complication that if you're dealing with time 
values in different timezones, it'd imply that 2am +1200 is identical to 
2pm UTC, which is not entirely true.. they're on different days. 
Currently they'd return Time values that are exactly one day apart, with 
this change they'd return identical Time values.

As usual, the javadoc is fairly hopeless at telling you how Time is 
actually supposed to behave!

(I wonder what java.sql.Time.valueOf() returns in timezones ahead of 
UTC? Might be worth testing..)

-O



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Make time/timestamp tests fair for binary transfers
@ 2007-07-23 22:55  Oliver Jowett <oliver@opencloud.com>
  parent: Oliver Jowett <oliver@opencloud.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Oliver Jowett @ 2007-07-23 22:55 UTC (permalink / raw)
  To: Mikko Tiihonen <mikko.tiihonen@iki.fi>; +Cc: pgsql-jdbc

Oliver Jowett wrote:

> (I wonder what java.sql.Time.valueOf() returns in timezones ahead of 
> UTC? Might be worth testing..)

Time.valueOf("00:00:00").getTime() returns -43200000 in a +1200 
timezone, so I'd be happier keeping negative values to be consistent 
with that.

-O



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Make time/timestamp tests fair for binary transfers
@ 2007-07-24 00:01  Mikko Tiihonen <mikko.tiihonen@iki.fi>
  parent: Oliver Jowett <oliver@opencloud.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Mikko Tiihonen @ 2007-07-24 00:01 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

On Tue, 2007-07-24 at 10:30 +1200, Oliver Jowett wrote:
> Mikko Tiihonen wrote:
> 
> > The patch also alters what kind of java.sql.Time values are returned.
> > Before the patch the millisecond value of a ResultSet.getTime could be
> > between -24h<x<24h. The patch changes the returned value to be always
> > between 0h<=x<24h. If someone thinks this change is not acceptable I'll
> > go back and revert it re-fix the test cases to match.
> 
> I'm not sure if this is right. In theory Time values are meant to have a 
> day/month/year of Jan 1 1970. Time (via Date) uses the local timezone to 
> compute the day/month/year. But to represent all time values in all 
> local timezones that means you need negative millisecond values (to 
> handle timezones ahead of UTC). For example, Jan 1 1970 02:00 +1200 is 
> actually Dec 31 1969 14:00 UTC, which is a negative milliseconds value.

Yes. But the Time part is 14:00, and if you store the above to postgres
column with TIME type it will not print out nor return any negative
values.
The negative time is purely a Java problem.

> There's an additional complication that if you're dealing with time 
> values in different timezones, it'd imply that 2am +1200 is identical to 
> 2pm UTC, which is not entirely true.. they're on different days. 
> Currently they'd return Time values that are exactly one day apart, with 
> this change they'd return identical Time values.

Yes, but one would want to know both the day and the time of an event
then
a timestamp would have been a better choice.

> As usual, the javadoc is fairly hopeless at telling you how Time is 
> actually supposed to behave!
> (I wonder what java.sql.Time.valueOf() returns in timezones ahead of 
> UTC? Might be worth testing..)
... next post
> Time.valueOf("00:00:00").getTime() returns -43200000 in a +1200 
> timezone, so I'd be happier keeping negative values to be consistent 
> with that.

OK, lets keep using old way. It is most likely that the current
functionality is the one to which application writers have pushed all
JDBC driver implementations to converge to thus creating an undocumented
standard. Although everywhere I have seen the design guidelines for
portable JDBC code, the guideline is to store times to database either
in UTC milliseconds or as fully formatted strings with time zone. Thus I
believe that most applications have not exercised the corner cases
caused by varying time zones. 

I'll try again if I can get that to work with the binary transfers and
resend the patch.

-Mikko




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Make time/timestamp tests fair for binary transfers v2
@ 2007-07-24 10:42  Mikko Tiihonen <mikko.tiihonen@iki.fi>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Mikko Tiihonen @ 2007-07-24 10:42 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

On Tue, 2007-07-24 at 10:55 +1200, Oliver Jowett wrote:
> Oliver Jowett wrote:
> 
> > (I wonder what java.sql.Time.valueOf() returns in timezones ahead of 
> > UTC? Might be worth testing..)
> 
> Time.valueOf("00:00:00").getTime() returns -43200000 in a +1200 
> timezone, so I'd be happier keeping negative values to be consistent 
> with that.

OK. I managed to get everything working with binary transfer and the
time
zone dependant normalisation. Just to clarify: the values returned by
ResultSet.getTime are normalised as follows:
GMT-13:00 (+46800000..+133200000)
GMT+00:00 (+00000000..+086400000)
GMT+13:00 (-46800000..+039600000)

The original code already did it this way. Maybe it should be
documented somewhere explicitly so that it is a known decision
instead of just an implementation detail.

Patch description:
The main purpose of the following patch is to make TimezoneTest fair
for binary transfers. The modified test cases now work both on text and
binary protocol (I'll post my new version of it soonish after I have
resolved all remaining unit test case failures).

Previously the code mostly did not test the parsing of
time/date/timestamp values but instead compared the values returned by
ResultSet.getString, which are formatted by the server.
The patched version does now two checks:
1) adds a new verify round that ensures that the insert was correct by
doing an explicit query with UTC timezone and casting all values to
varchar so that the server will format them.
2) Changes all other tests to use the getTime/getDate/getTimestamp
methods when comparing if the queried data is correct.

This basically means that the getString of a time type column is now
undefined and can change. But I think it's not a problem because
different server versions have had different string representation of
time already.

-Mikko

Attachments:

timetest-fixes-v2.patch (text/x-patch, 24.5K) download Index: org/postgresql/test/jdbc2/TimezoneTest.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/TimezoneTest.java,v retrieving revision 1.4 diff -u -r1.4 TimezoneTest.java --- org/postgresql/test/jdbc2/TimezoneTest.java 16 Apr 2007 18:31:38 -0000 1.4 +++ org/postgresql/test/jdbc2/TimezoneTest.java 24 Jul 2007 10:39:46 -0000 @@ -15,10 +15,6 @@ import java.sql.*; import java.util.TimeZone; import java.util.Calendar; -import java.util.GregorianCalendar; - -import org.postgresql.PGStatement; -import org.postgresql.jdbc2.TimestampUtils; /* * Tests for time and date types with calendars involved. @@ -38,8 +34,9 @@ */ public class TimezoneTest extends TestCase { + private static final int DAY = 24 * 3600 * 1000; private Connection con; - private TimeZone saveTZ; + private static final TimeZone saveTZ = TimeZone.getDefault();; // // We set up everything in different timezones to try to exercise many cases: @@ -74,7 +71,6 @@ protected void setUp() throws Exception { // We must change the default TZ before establishing the connection. - saveTZ = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("GMT+01")); // Arbitary timezone that doesn't match our test timezones con = TestUtil.openDB(); @@ -97,20 +93,41 @@ TimeZone.setDefault(saveTZ); TestUtil.dropTable(con, "testtimezone"); + TestUtil.closeDB(con); } - + + private Time normalize(Time t, Calendar tz) { + return new Time(normalize(t.getTime(), tz.getTimeZone())); + } + private Timestamp normalize(Timestamp t, Calendar tz) { + return new Timestamp(normalize(t.getTime(), tz.getTimeZone())); + } + private long normalize(long t, TimeZone tz) { + long millis = t; + long low = - tz.getOffset(millis); + long high = low + DAY; + if (millis < low) { + do { millis += DAY; } while (millis < low); + } else if (millis >= high) { + do { millis -= DAY; } while (millis > high); + } + return millis; + } + public void testGetTimestamp() throws Exception { con.createStatement().executeUpdate("INSERT INTO testtimezone(tstz,ts,t,tz,d) VALUES('2005-01-01 15:00:00 +0300', '2005-01-01 15:00:00', '15:00:00', '15:00:00 +0300', '2005-01-01')"); - + ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,t::varchar,tz::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03", + "2005-01-01"}); + Timestamp ts; @@ -184,9 +201,10 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,d from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("2005-01-01", rs.getString(3)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "2005-01-01"}); Date d; @@ -201,7 +219,7 @@ assertEquals(1104555600000L, d.getTime()); d = rs.getDate(1, cGMT13); // 2005-01-02 01:00:00 +1300 -> 2005-01-02 00:00:00 +1300 assertEquals(1104577200000L, d.getTime()); - + // timestamp: 2005-01-01 15:00:00 d = rs.getDate(2); // 2005-01-01 00:00:00 +0100 assertEquals(1104534000000L, d.getTime()); @@ -237,10 +255,11 @@ ResultSet rs = con.createStatement().executeQuery("SELECT tstz,ts,t,tz from testtimezone"); assertTrue(rs.next()); - assertEquals("2005-01-01 15:00:00+03", rs.getString(1)); - assertEquals("2005-01-01 15:00:00", rs.getString(2)); - assertEquals("15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); + checkDatabaseContents("SELECT tstz::varchar,ts::varchar,t::varchar,tz::varchar,d::varchar from testtimezone", + new String[]{"2005-01-01 12:00:00+00", + "2005-01-01 15:00:00", + "15:00:00", + "15:00:00+03"}); Time t; @@ -309,6 +328,7 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % DAY); // +0100 (JVM default) insertTimestamp.setInt(1, seq++); @@ -330,37 +350,40 @@ insertTimestamp.setTimestamp(2,instant,cGMT05); // 07:00:00 insertTimestamp.executeUpdate(); - if (min73) { // +1300 insertTimestamp.setInt(1, seq++); insertTimestamp.setTimestamp(2,instant,cGMT13); // 01:00:00 insertTimestamp.executeUpdate(); - } + + checkDatabaseContents("SELECT seq::varchar,t::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "13:00:00"}, + new String[]{"2", "12:00:00"}, + new String[]{"3", "15:00:00"}, + new String[]{"4", "07:00:00"}, + new String[]{"5", "01:00:00"}}); seq = 1; ResultSet rs = con.createStatement().executeQuery("SELECT seq,t FROM testtimezone ORDER BY seq"); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("13:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("12:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("07:00:00", rs.getString(2)); + assertEquals(instantTime, rs.getTimestamp(2, cGMT05)); - if (min73) { - assertTrue(rs.next()); - assertEquals(seq++, rs.getInt(1)); - assertEquals("01:00:00", rs.getString(2)); - } + assertTrue(rs.next()); + assertEquals(seq++, rs.getInt(1)); + assertEquals(normalize(instantTime, cGMT13), rs.getTimestamp(2, cGMT13)); assertTrue(!rs.next()); } @@ -372,6 +395,12 @@ int seq = 1; Timestamp instant = new Timestamp(1104580800000L); // 2005-01-01 12:00:00 UTC + Timestamp instantTime = new Timestamp(instant.getTime() % DAY); + Timestamp instantDateJVM = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - TimeZone.getDefault().getRawOffset()); + Timestamp instantDateUTC = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cUTC.getTimeZone().getRawOffset()); + Timestamp instantDateGMT03 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT03.getTimeZone().getRawOffset()); + Timestamp instantDateGMT05 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT05.getTimeZone().getRawOffset()); + Timestamp instantDateGMT13 = new Timestamp(instant.getTime() - (instant.getTime() % DAY) - cGMT13.getTimeZone().getRawOffset() + DAY); // +0100 (JVM default) insertTimestamp.setInt(1,seq++); @@ -417,6 +446,14 @@ insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,tstz::varchar,ts::varchar,tz::varchar,d::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2005-01-01 12:00:00+00", "2005-01-01 13:00:00", "13:00:00+01", "2005-01-01"}, + new String[]{"2", "2005-01-01 12:00:00+00", "2005-01-01 12:00:00", "12:00:00+00", "2005-01-01"}, + new String[]{"3", "2005-01-01 12:00:00+00", "2005-01-01 15:00:00", "15:00:00+03", "2005-01-01"}, + new String[]{"4", "2005-01-01 12:00:00+00", "2005-01-01 07:00:00", "07:00:00-05", "2005-01-01"}, + new String[]{"5", "2005-01-01 12:00:00+00", "2005-01-02 01:00:00", "01:00:00+13", "2005-01-02"}}); + // // check results // @@ -426,39 +463,39 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 13:00:00", rs.getString(3)); - assertEquals("13:00:00+01", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2)); + assertEquals(instant, rs.getTimestamp(3)); + assertEquals(instantTime, rs.getTimestamp(4)); + assertEquals(instantDateJVM, rs.getTimestamp(5)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 12:00:00", rs.getString(3)); - assertEquals("12:00:00+00", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cUTC)); + assertEquals(instant, rs.getTimestamp(3, cUTC)); + assertEquals(instantTime, rs.getTimestamp(4, cUTC)); + assertEquals(instantDateUTC, rs.getTimestamp(5, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 15:00:00", rs.getString(3)); - assertEquals("15:00:00+03", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT03)); + assertEquals(instant, rs.getTimestamp(3, cGMT03)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT03)); + assertEquals(instantDateGMT03, rs.getTimestamp(5, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 07:00:00", rs.getString(3)); - assertEquals("07:00:00-05", rs.getString(4)); - assertEquals("2005-01-01", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT05)); + assertEquals(instant, rs.getTimestamp(3, cGMT05)); + assertEquals(instantTime, rs.getTimestamp(4, cGMT05)); + assertEquals(instantDateGMT05, rs.getTimestamp(5, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 15:00:00+03", rs.getString(2)); - assertEquals("2005-01-02 01:00:00", rs.getString(3)); - assertEquals("01:00:00+13", rs.getString(4)); - assertEquals("2005-01-02", rs.getString(5)); + assertEquals(instant, rs.getTimestamp(2, cGMT13)); + assertEquals(instant, rs.getTimestamp(3, cGMT13)); + assertEquals(normalize(instantTime, cGMT13), rs.getTimestamp(4, cGMT13)); + assertEquals(instantDateGMT13, rs.getTimestamp(5, cGMT13)); } assertTrue(!rs.next()); @@ -470,52 +507,59 @@ int seq = 1; - Date d; + Date dJVM, dUTC, dGMT03, dGMT05, dGMT13 = null; // +0100 (JVM default) - d = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 + dJVM = new Date(1104534000000L); // 2005-01-01 00:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d); // 2005-01-01 00:00:00 +0100 - insertTimestamp.setDate(3,d); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d); // 2005-01-01 + insertTimestamp.setDate(2,dJVM); // 2005-01-01 00:00:00 +0100 + insertTimestamp.setDate(3,dJVM); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dJVM); // 2005-01-01 insertTimestamp.executeUpdate(); // UTC - d = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 + dUTC = new Date(1104537600000L); // 2005-01-01 00:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cUTC); // 2005-01-01 00:00:00 +0000 - insertTimestamp.setDate(3,d,cUTC); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cUTC); // 2005-01-01 + insertTimestamp.setDate(2,dUTC,cUTC); // 2005-01-01 00:00:00 +0000 + insertTimestamp.setDate(3,dUTC,cUTC); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dUTC,cUTC); // 2005-01-01 insertTimestamp.executeUpdate(); // +0300 - d = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 + dGMT03 = new Date(1104526800000L); // 2005-01-01 00:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT03); // 2005-01-01 00:00:00 +0300 - insertTimestamp.setDate(3,d,cGMT03); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT03); // 2005-01-01 + insertTimestamp.setDate(2,dGMT03,cGMT03); // 2005-01-01 00:00:00 +0300 + insertTimestamp.setDate(3,dGMT03,cGMT03); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT03,cGMT03); // 2005-01-01 insertTimestamp.executeUpdate(); // -0500 - d = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 + dGMT05 = new Date(1104555600000L); // 2005-01-01 00:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT05); // 2005-01-01 00:00:00 -0500 - insertTimestamp.setDate(3,d,cGMT05); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT05); // 2005-01-01 + insertTimestamp.setDate(2,dGMT05,cGMT05); // 2005-01-01 00:00:00 -0500 + insertTimestamp.setDate(3,dGMT05,cGMT05); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT05,cGMT05); // 2005-01-01 insertTimestamp.executeUpdate(); if (min73) { // +1300 - d = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 + dGMT13 = new Date(1104490800000L); // 2005-01-01 00:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setDate(2,d,cGMT13); // 2005-01-01 00:00:00 +1300 - insertTimestamp.setDate(3,d,cGMT13); // 2005-01-01 00:00:00 - insertTimestamp.setDate(4,d,cGMT13); // 2005-01-01 + insertTimestamp.setDate(2,dGMT13,cGMT13); // 2005-01-01 00:00:00 +1300 + insertTimestamp.setDate(3,dGMT13,cGMT13); // 2005-01-01 00:00:00 + insertTimestamp.setDate(4,dGMT13,cGMT13); // 2005-01-01 insertTimestamp.executeUpdate(); } insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,tstz::varchar,ts::varchar,d::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "2004-12-31 23:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"2", "2005-01-01 00:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"3", "2004-12-31 21:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"4", "2005-01-01 05:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}, + new String[]{"5", "2004-12-31 11:00:00+00", "2005-01-01 00:00:00", "2005-01-01"}}); // // check results // @@ -525,34 +569,34 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 02:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dJVM, rs.getDate(2)); + assertEquals(dJVM, rs.getDate(3)); + assertEquals(dJVM, rs.getDate(4)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 03:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dUTC, rs.getDate(2, cUTC)); + assertEquals(dUTC, rs.getDate(3, cUTC)); + assertEquals(dUTC, rs.getDate(4, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 00:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT03, rs.getDate(2, cGMT03)); + assertEquals(dGMT03, rs.getDate(3, cGMT03)); + assertEquals(dGMT03, rs.getDate(4, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2005-01-01 08:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT05, rs.getDate(2, cGMT05)); + assertEquals(dGMT05, rs.getDate(3, cGMT05)); + assertEquals(dGMT05, rs.getDate(4, cGMT05)); if (min73) { assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("2004-12-31 14:00:00+03", rs.getString(2)); - assertEquals("2005-01-01 00:00:00", rs.getString(3)); - assertEquals("2005-01-01", rs.getString(4)); + assertEquals(dGMT13, rs.getDate(2, cGMT13)); + assertEquals(dGMT13, rs.getDate(3, cGMT13)); + assertEquals(dGMT13, rs.getDate(4, cGMT13)); } assertTrue(!rs.next()); @@ -570,45 +614,53 @@ int seq = 1; - Time t; + Time tJVM, tUTC, tGMT03, tGMT05, tGMT13; // +0100 (JVM default) - t = new Time(50400000L); // 1970-01-01 15:00:00 +0100 + tJVM = new Time(50400000L); // 1970-01-01 15:00:00 +0100 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t); // 15:00:00 - insertTimestamp.setTime(3,t); // 15:00:00+03 + insertTimestamp.setTime(2,tJVM); // 15:00:00 + insertTimestamp.setTime(3,tJVM); // 15:00:00+03 insertTimestamp.executeUpdate(); // UTC - t = new Time(54000000L); // 1970-01-01 15:00:00 +0000 + tUTC = new Time(54000000L); // 1970-01-01 15:00:00 +0000 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cUTC); // 15:00:00 - insertTimestamp.setTime(3,t,cUTC); // 15:00:00+00 + insertTimestamp.setTime(2,tUTC,cUTC); // 15:00:00 + insertTimestamp.setTime(3,tUTC,cUTC); // 15:00:00+00 insertTimestamp.executeUpdate(); // +0300 - t = new Time(43200000L); // 1970-01-01 15:00:00 +0300 + tGMT03 = new Time(43200000L); // 1970-01-01 15:00:00 +0300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT03); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT03); // 15:00:00+03 + insertTimestamp.setTime(2,tGMT03,cGMT03); // 15:00:00 + insertTimestamp.setTime(3,tGMT03,cGMT03); // 15:00:00+03 insertTimestamp.executeUpdate(); // -0500 - t = new Time(72000000L); // 1970-01-01 15:00:00 -0500 + tGMT05 = new Time(72000000L); // 1970-01-01 15:00:00 -0500 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT05); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT05); // 15:00:00-05 + insertTimestamp.setTime(2,tGMT05,cGMT05); // 15:00:00 + insertTimestamp.setTime(3,tGMT05,cGMT05); // 15:00:00-05 insertTimestamp.executeUpdate(); // +1300 - t = new Time(7200000L); // 1970-01-01 15:00:00 +1300 + tGMT13 = new Time(7200000L); // 1970-01-01 15:00:00 +1300 insertTimestamp.setInt(1,seq++); - insertTimestamp.setTime(2,t,cGMT13); // 15:00:00 - insertTimestamp.setTime(3,t,cGMT13); // 15:00:00+13 + insertTimestamp.setTime(2,tGMT13,cGMT13); // 15:00:00 + insertTimestamp.setTime(3,tGMT13,cGMT13); // 15:00:00+13 insertTimestamp.executeUpdate(); insertTimestamp.close(); + // check that insert went correctly by parsing the raw contents in UTC + checkDatabaseContents("SELECT seq::varchar,t::varchar,tz::varchar from testtimezone ORDER BY seq", + new String[][]{new String[]{"1", "15:00:00", "15:00:00+01",}, + new String[]{"2", "15:00:00", "15:00:00+00",}, + new String[]{"3", "15:00:00", "15:00:00+03",}, + new String[]{"4", "15:00:00", "15:00:00-05",}, + new String[]{"5", "15:00:00", "15:00:00+13",}}); + // // check results // @@ -618,28 +670,28 @@ assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+01", rs.getString(3)); + assertEquals(tJVM, rs.getTime(2)); + assertEquals(tJVM, rs.getTime(3)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+00", rs.getString(3)); + assertEquals(tUTC, rs.getTime(2, cUTC)); + assertEquals(tUTC, rs.getTime(2, cUTC)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+03", rs.getString(3)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); + assertEquals(tGMT03, rs.getTime(2, cGMT03)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00-05", rs.getString(3)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); + assertEquals(tGMT05, rs.getTime(2, cGMT05)); assertTrue(rs.next()); assertEquals(seq++, rs.getInt(1)); - assertEquals("15:00:00", rs.getString(2)); - assertEquals("15:00:00+13", rs.getString(3)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); + assertEquals(tGMT13, rs.getTime(2, cGMT13)); assertTrue(!rs.next()); } @@ -667,4 +719,34 @@ assertEquals(-1577929192000L, rs.getTimestamp(1).getTime()); } + /** + * Does a query in UTC time zone to database to check that the + * inserted values are correct. + * + * @param query The query to run. + * @param correct The correct answers in UTC time zone. + * @throws Exception + */ + private void checkDatabaseContents(String query, String[] correct) throws Exception { + checkDatabaseContents(query, new String[][]{correct}); + } + + private void checkDatabaseContents(String query, String[][] correct) throws Exception { + Connection con2 = TestUtil.openDB(); + Statement s = con2.createStatement(); + assertFalse(s.execute("set time zone 'UTC'")); + assertTrue(s.execute(query)); + ResultSet rs = s.getResultSet(); + for (int j = 0; j < correct.length; ++j) { + assertTrue(rs.next()); + for (int i = 0; i < correct[j].length; ++i) { + assertEquals("On row " + (j+1), correct[j][i], rs.getString(i+1)); + } + } + assertFalse(rs.next()); + rs.close(); + s.close(); + con2.close(); + } + }
^ permalink raw reply [nested|flat] 5+ messages in thread


end of thread, other threads:[~2007-07-24 10:42 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-07-23 18:02 Make time/timestamp tests fair for binary transfers Mikko Tiihonen <mikko.tiihonen@iki.fi>
2007-07-23 22:30 ` Oliver Jowett <oliver@opencloud.com>
2007-07-23 22:55   ` Oliver Jowett <oliver@opencloud.com>
2007-07-24 10:42     ` Make time/timestamp tests fair for binary transfers v2 Mikko Tiihonen <mikko.tiihonen@iki.fi>
2007-07-24 00:01   ` Mikko Tiihonen <mikko.tiihonen@iki.fi>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox