Skip to Content.
Sympa Menu

freetds - Sybase "datetime" JDBC problem and solution

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Jason Goemaat <jasong AT netins.net>
  • To: 'FreeTDS' <freetds AT franklin.oit.unc.edu>
  • Subject: Sybase "datetime" JDBC problem and solution
  • Date: Wed, 14 Jul 1999 19:56:43 -0500


Client: JDK 1.1 and 1.2 on Windows NT and Solaris
Server: Sybase Adaptive Server Enterprise 11.5.1 on Solaris

I came across a problem with getting dates from a JDBC ResultSet. Sybase
doesn't have a Date column, just datetime. FreeTDS_jdbc gets the value as a
long and creates a Timestamp from the value stored in Sybase. The problem
is that our Syase server stores the datetime values as offsets from
1/1/1970 in the current time zone, not as GMT. Seeing as other software,
the JDBC-ODBC bridge, and Sybase's JConnect driver all returned Date
objects with the correct date and time in them, I decided to change
FreeTDS.

I made the following modifications to Tds.getDatetimeValue():

Snippet of fixed code:
//---------- begin code ----------
case 8:
{
// It appears that a datetime is made of 2 32bit ints
// The first one is the number of days since 1900
// The second is the number of seconds*300
// JLG - To account for timezone differences
TimeZone tz = TimeZone.getDefault();
int offset = tz.getRawOffset();

int days = comm.getTdsInt();
int time = comm.getTdsInt();
long seconds = (long)((days - 25567)*(24*60*60))
+ (long)(time/300);
Date d = new Date(seconds * 1000 - offset);
if (tz.inDaylightTime(d))
seconds -= 60 * 60;

result = new Timestamp(seconds * 1000 - offset);
break;
}
//---------- end code ----------

Snippet of original code:
//---------- begin code ----------
case 8:
{
// It appears that a datetime is made of of 2 32bit ints
// The first one is the number of days since 1900
// The second is the number of seconds*300
int days = comm.getTdsInt();
int time = comm.getTdsInt();
long seconds = (long)((days - 25567)*(24*60*60))
+ (long)(time/300);

result = new Timestamp(seconds * 1000);
break;
}
//---------- end code ----------

The timezone offset is added to the date and if the date is in daylight
savings time in the default timezone, another hour is subtracted. I've
tested this quite a bit and it seems to work great.

Jason Goemaat
jasong AT netins.net



  • Sybase "datetime" JDBC problem and solution, Jason Goemaat, 07/14/1999

Archive powered by MHonArc 2.6.24.

Top of Page