Subject | Re: time zone problem |
---|---|
Author | rwilcom |
Post date | 2004-07-29T17:17:44Z |
I believe I have found the bug in the JayBird JDBC code - the
following method is defined in the org.firebirdsql.gds.XSQLVAR class:
public Timestamp encodeTimestamp(java.sql.Timestamp value,
Calendar cal){
if (cal == null) {
return value;
}
else {
long time = value.getTime() -
(cal.getTimeZone().getRawOffset() -
Calendar.getInstance().getTimeZone().getRawOffset());
return new Timestamp(time);
}
}
I think the bug is the use of "Calendar.getInstance().getTimeZone
().getRawOffset());" and the subtraction since that line of code is
getting the local machine's time zone to perform the time zone
conversion calculation (which it should *never* do) shouldn't this
just simply be:
long time = value.getTime() + cal.getTimeZone().getRawOffset();
This simply take the UTC and addes (or subtracts if negative) the
passed calendar object's offset value.
??thoughts??
Ron
--- In Firebird-Java@yahoogroups.com, "Rick DeBay" <rdebay@r...>
wrote:
following method is defined in the org.firebirdsql.gds.XSQLVAR class:
public Timestamp encodeTimestamp(java.sql.Timestamp value,
Calendar cal){
if (cal == null) {
return value;
}
else {
long time = value.getTime() -
(cal.getTimeZone().getRawOffset() -
Calendar.getInstance().getTimeZone().getRawOffset());
return new Timestamp(time);
}
}
I think the bug is the use of "Calendar.getInstance().getTimeZone
().getRawOffset());" and the subtraction since that line of code is
getting the local machine's time zone to perform the time zone
conversion calculation (which it should *never* do) shouldn't this
just simply be:
long time = value.getTime() + cal.getTimeZone().getRawOffset();
This simply take the UTC and addes (or subtracts if negative) the
passed calendar object's offset value.
??thoughts??
Ron
--- In Firebird-Java@yahoogroups.com, "Rick DeBay" <rdebay@r...>
wrote:
> Be wary of using static Calendars and parsers. They set instancethe
> variables during calculations, and if you use the same parser at
> same time in two different threads, they'll step on each others'should
> results.
>
> -----Original Message-----
> From: rwilcom [mailto:ron.wilcom@n...]
> Sent: Thursday, July 29, 2004 8:54 AM
> To: Firebird-Java@yahoogroups.com
> Subject: [Firebird-Java] Re: time zone problem
>
> Roman,
>
> I believe I am setting the timezone correctly for the calendar
> object - setting as follows:
>
> private static final Calendar GMT = Calendar.getInstance
> (TimeZone.getTimeZone("GMT") );
>
> I am using the machine locale (which would be US/en) but that
> not impact the time zone. Reminder, if I change the OS timezonethe
> setting the inserted date value changes!? This should never be
> case when using:()),
>
> ps.setTimestamp(1, new java.sql.Timestamp( testDateTime.getTime
> GMT );timezone
>
> Even if the calander object was wrongly set to a different
> the entry inserted into the database should be consistent evenwhen
> the OS timezone is changed. The driver should not relay on or use()),
> the OS timezone setting to calculate the date/time entry from the
> UTC millisec value?!
>
> Also - I did upgrade to RC4 and the same problem occurs.
>
> From my code snippet below you should easily be able to recreate
> this problem. Please advise! Meanwhile, I will look into the JDBC
> source code.
>
> thanks,
> Ron
>
> start code snippet-----------------------
>
>
> //assuming you have a connection object
> //already connected via JayBird JDBC
>
> Calendar GMT = Calendar.getInstance(TimeZone.getTimeZone
> ("GMT") ); //machine locale
> PreparedStatement ps = null;
> try {
> ps = connection.prepareStatement("INSERT INTO MyTable(id,
> dateAndtimeInGMT) VALUES (?, ?)");
>
> ps.setInt(1, 1 );
>
> Date testDate = format.parse("02-10-2004 10:00:00 EST");
> ps.setTimestamp(2, new java.sql.Timestamp( testDate.getTime
> GMT );there
>
> ps.executeUpdate();
>
> //check the database value; this will be
> //dependent on your OS time zone
> //not on the GMT calculated value;
> //After running this once change
> //your OS time zone and run again -
> //a different date/time value is inserted
> }
> catch (SQLException e){
> System.out.println("error:"+e.toString());
> }
> finally{
> try{ps.close();}
> catch(Exception any){/*nothing*/}
> }
>
>
> end code snippet-----------------------
>
>
> --- In Firebird-Java@yahoogroups.com, "Roman Rokytskyy"
> <rrokytskyy@a...> wrote:
> > > Date testDateTime= format.parse("02-10-2004 10:00:00 EST");
> >
> > > ps.setTimestamp(1, new java.sql.Timestamp( testDateTime.getTime
> ()),
> > > GMT );
> >
> > Please ensure that your GMT calendar has the time zone set
> correctly (i.e.
> > it is not the default time zone). If you still believe that
> is a bug,
> > please prepare a test case that reproduces the problem.
> >
> > If you want to check sources, please see
> > org.firebirdsql.jdbc.field.FBTimestampField and
> org.firebirdsql.gds.XSQLVAR
> > classes.
> >
> > Roman
>
>
>
>
>
> Yahoo! Groups Links