Commit a4988374 authored by R.W.Majeed's avatar R.W.Majeed
Browse files

fixed wrong timestamp conversion from/to instant

parent e992755e
...@@ -3,7 +3,6 @@ package de.sekmi.histream.i2b2; ...@@ -3,7 +3,6 @@ package de.sekmi.histream.i2b2;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import de.sekmi.histream.AbnormalFlag; import de.sekmi.histream.AbnormalFlag;
import de.sekmi.histream.DateTimeAccuracy; import de.sekmi.histream.DateTimeAccuracy;
...@@ -29,7 +28,7 @@ public class DataDialect { ...@@ -29,7 +28,7 @@ public class DataDialect {
private String nullValueFlagCd; private String nullValueFlagCd;
private String nullValueTypeCd; private String nullValueTypeCd;
/** Timezone for timestamp / date time columns */ /** Timezone for timestamp / date time columns */
private ZoneId zoneId; private ZoneId zoneId; // TODO may not be needed since the db always uses epoch seconds
// TODO nullSexCd, nullInOutCd // TODO nullSexCd, nullInOutCd
public DataDialect(){ public DataDialect(){
...@@ -76,7 +75,8 @@ public class DataDialect { ...@@ -76,7 +75,8 @@ public class DataDialect {
if( instant == null ){ if( instant == null ){
return null; return null;
}else{ }else{
return Timestamp.from(instant.atZone(zoneId).toLocalDateTime().atOffset(ZoneOffset.UTC).toInstant()); return Timestamp.from(instant);
//return Timestamp.from(instant.atZone(zoneId).toLocalDateTime().atOffset(ZoneOffset.UTC).toInstant());
} }
} }
public Timestamp encodeInstantPartial(DateTimeAccuracy instant){ public Timestamp encodeInstantPartial(DateTimeAccuracy instant){
...@@ -90,7 +90,8 @@ public class DataDialect { ...@@ -90,7 +90,8 @@ public class DataDialect {
if( timestamp == null ){ if( timestamp == null ){
return null; return null;
}else{ }else{
return timestamp.toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime().atZone(zoneId).toInstant(); return timestamp.toInstant();
//return timestamp.toInstant().atOffset(ZoneOffset.UTC).toLocalDateTime().atZone(zoneId).toInstant();
} }
} }
public DateTimeAccuracy decodeInstantPartial(Timestamp timestamp){ public DateTimeAccuracy decodeInstantPartial(Timestamp timestamp){
......
package de.sekmi.histream.i2b2; package de.sekmi.histream.i2b2;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
...@@ -9,17 +13,24 @@ import static org.junit.Assert.*; ...@@ -9,17 +13,24 @@ import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import de.sekmi.histream.DateTimeAccuracy; import de.sekmi.histream.DateTimeAccuracy;
import static org.junit.Assert.*;
public class TestDataDialect { public class TestDataDialect {
@Test
public void localDateTimeVerbatimOutput(){
LocalDateTime local = LocalDateTime.of(2001,2,3,4,5);
// local date time will be encoded as is, without offset
assertEquals("2001-02-03T04:05", local.toString());
// SQL timestamp will use the local timezone.
// We should not use this method for conversion with explicit zones
Timestamp ts = Timestamp.valueOf(local);
System.out.println("Local time " + local + " to SQL Timestamp: "+ts.toInstant());
}
@Test @Test
public void verifySqlTimestampConversions(){ public void verifySqlTimestampConversions(){
DataDialect dialect = new DataDialect(); DataDialect dialect = new DataDialect();
dialect.setTimeZone(ZoneId.of("Asia/Shanghai")); dialect.setTimeZone(ZoneId.of("Asia/Shanghai"));
Timestamp ts;
LocalDateTime local = LocalDateTime.of(2001,2,3,4,5);
System.out.println(local.toString());
Timestamp ts = Timestamp.valueOf(local);
System.out.println(ts.toInstant());
Instant inst = Instant.parse("2001-02-03T04:05:06Z"); Instant inst = Instant.parse("2001-02-03T04:05:06Z");
DateTimeAccuracy da = new DateTimeAccuracy(inst); DateTimeAccuracy da = new DateTimeAccuracy(inst);
...@@ -34,4 +45,20 @@ public class TestDataDialect { ...@@ -34,4 +45,20 @@ public class TestDataDialect {
System.out.println(b); System.out.println(b);
assertEquals(inst, b); assertEquals(inst, b);
} }
public static void main(String[] args) throws SQLException{
// verify how timestamps get written to the database
Connection c = new TestExtractor().getConnection();
Statement s = c.createStatement();
s.executeUpdate("DELETE FROM source_master WHERE source_cd='db_test'");
s.close();
PreparedStatement ps = c.prepareStatement("INSERT INTO source_master(source_cd, create_date) VALUES(?,?)");
ps.setString(1, "db_test");
Instant inst = Instant.parse("2001-08-03T04:05:06Z");
ps.setTimestamp(2, Timestamp.from(inst));
ps.executeUpdate();
ps.close();
//c.commit();
c.close();
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment