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;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import de.sekmi.histream.AbnormalFlag;
import de.sekmi.histream.DateTimeAccuracy;
......@@ -29,7 +28,7 @@ public class DataDialect {
private String nullValueFlagCd;
private String nullValueTypeCd;
/** 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
public DataDialect(){
......@@ -76,7 +75,8 @@ public class DataDialect {
if( instant == null ){
return null;
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){
......@@ -90,7 +90,8 @@ public class DataDialect {
if( timestamp == null ){
return null;
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){
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.time.Instant;
import java.time.LocalDateTime;
......@@ -9,17 +13,24 @@ import static org.junit.Assert.*;
import org.junit.Test;
import de.sekmi.histream.DateTimeAccuracy;
import static org.junit.Assert.*;
public class TestDataDialect {
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());
public void verifySqlTimestampConversions(){
DataDialect dialect = new DataDialect();
LocalDateTime local = LocalDateTime.of(2001,2,3,4,5);
Timestamp ts = Timestamp.valueOf(local);
Timestamp ts;
Instant inst = Instant.parse("2001-02-03T04:05:06Z");
DateTimeAccuracy da = new DateTimeAccuracy(inst);
......@@ -34,4 +45,20 @@ public class TestDataDialect {
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'");
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));
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