TDI Date DTime Timezone and SQL Timestamps

Get a SQL Timstamp

// This can be used in the connector's attribute assignment code, or more efficiently in a 
// prolog as a global variable. Note - Its assumed to be more efficient to use the System 
// method over creating a java.util.Date() object
ret.value = new java.sql.Timestamp(java.lang.System.currentTimeMillis());


Convert from DTimeStamp 

Delphi systems use a Date Time Stamps that are very different from Java. But a little math will convert it to millis and then offset for java epoch. 

// Convert DTimeStamp to Milliseconds since epoch
var dTime = conn.ACTUALDATETIME.getValue();
var millis = java.lang.Math.round((dTime - 25569) * 86400000);
ret.value = new java.sql.Timestamp(millis);

Convert to Local Time from GMT

// Convert that time from GMT to Localtime and accommodate daylight savings time 
// _at the time it happened_ by getting the offset at that time. Uses millis above
var localTimeZone = java.util.TimeZone.getDefault();
var offset = localTimeZone.getOffset(millis);
var millis = millis - offset; //have to check + or - of TZ to be really accurate
ret.value = new java.sql.Timestamp(millis);





http://www.delphibasics.co.uk/RTL.asp?Name=TDateTime
http://stackoverflow.com/questions/11399491/java-timezone-offset
Comments