Failing Daylight savings test with 1.4.182 ... was working on 1.3.153

32 views
Skip to first unread message

Rob Bygrave

unread,
Nov 28, 2014, 5:35:15 AM11/28/14
to h2-da...@googlegroups.com
Hi,

So I have a test that is now failing when upgrading from 1.3.153 to  1.4.182

The test code is:


  @Test
  public void testDirect() throws SQLException {

    // For it to fail, the time has to match the time at which the daylight saving changes
    // are applied in that time zone. Therefore specify it explicitly.

    // Get a connection ...
    EbeanServer server = Ebean.getServer(null);
    Transaction transaction = server.createTransaction();
    Connection connection = transaction.getConnection();

    PreparedStatement pstmt = connection.prepareStatement("create table dls_test (id bigint auto_increment not null, myts timestamp)");
    pstmt.execute();
    pstmt.close();

    TimeZone defaultTimeZone = TimeZone.getDefault();
    try {

      TimeZone.setDefault(TimeZone.getTimeZone("EET"));

      // Run the code and see how there is a 3600 second change
      Timestamp daylightSavingDate = new Timestamp(1351382400000l);
      // On a second run comment in the following date and see
      // how there is a 0 second change
      // daylightSavingDate = new Date(1361382400000l);

      pstmt = connection.prepareStatement("insert into dls_test (myts) values (?)");
      pstmt.setTimestamp(1, daylightSavingDate);
      assertEquals(1, pstmt.executeUpdate());
      pstmt.close();

      pstmt = connection.prepareStatement("select myts from dls_test ");
      ResultSet rset = pstmt.executeQuery();
      rset.next();
      Timestamp timestampBack = rset.getTimestamp(1);
      pstmt.close();
      rset.close();


      long diffMillis = daylightSavingDate.getTime() - timestampBack.getTime();

      System.out.println(" --- the date i put in   : " + daylightSavingDate);
      System.out.println("          as millis      : " + daylightSavingDate.getTime());
      System.out.println(" --- the date i get back : " + timestampBack);
      System.out.println("          as millis      : " + timestampBack.getTime());
      System.out.println("The difference is " + diffMillis / 1000 + " seconds");

      assertEquals(0L, diffMillis);

    } finally {
      TimeZone.setDefault(defaultTimeZone);
    }

  }



Using version 1.3.153 the test passes and the output is:

 --- the date i put in   : 2012-10-28 03:00:00.0
          as millis      : 1351382400000
 --- the date i get back : 2012-10-28 03:00:00.0
          as millis      : 1351382400000
The difference is 0 seconds



Using version 1.4.182 the test fails and the output is:


 --- the date i put in   : 2012-10-28 03:00:00.0
          as millis      : 1351382400000
 --- the date i get back : 2012-10-28 03:00:00.0
          as millis      : 1351386000000
The difference is -3600 seconds

java.lang.AssertionError: 
Expected :0
Actual   :-3600000
 <Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at com.avaje.tests.insert.TestSaveWithDaylightSavings.testDirect(TestSaveWithDaylightSavings.java:110)
...


Is the test valid or perhaps there is some bug introduced?


Thanks, Rob.


Thomas Mueller

unread,
Dec 15, 2014, 1:46:54 AM12/15/14
to h2-da...@googlegroups.com
Hi,

You get the same behavior if you do:

TimeZone.setDefault(TimeZone.getTimeZone("EET"));
Timestamp d = new Timestamp(1351382400000L);
System.out.println(d + " "  + d.getTime());
Timestamp d2 = Timestamp.valueOf(d.toString());
System.out.println(d2 + " " + d2.getTime());
System.out.println("difference " + (d.getTime() - d2.getTime()));

2012-10-28 03:00:00.0 1351382400000
2012-10-28 03:00:00.0 1351386000000
difference -3600000

So I don't consider this a bug of H2. You could call it a bug of the Java date / time processing, or at least unexpected behavior.

With H2, if you _don't_ use java.sql.Timestamp, but instead use String (even with "timestamp" data types), then you will not see this behavior.

Regards,
Thomas
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
To post to this group, send email to h2-da...@googlegroups.com.
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Rob Bygrave

unread,
Dec 15, 2014, 3:06:14 AM12/15/14
to h2-da...@googlegroups.com
Ok, great.

Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "H2 Database" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/h2-database/gYfGvxkNvaE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to h2-database...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages