|
|||||||||||
|
[JDBC] Timestamps without time zone
From: Achilleas Mantzios <achill(at)matrix.gatewaynet.com>
Date: Tue Jan 08 2008 - 07:12:56 EST
However it seems for my case that i am really trapped, and i ask your advice:
Now the problem.
I had identified the problem, on some standalone applications and i used the
brute-force method of
However if i do it on the jboss JVM then the whole thing gets screwed up, as
it will be unsafe to touch a JVM global property, just for the sake of some
operations, on behalf of some user.
java.util.TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
// database operations here
......
java.util.TimeZone.setDefault(null);
}
This way i can solve the problem at the expense of some loss of concurrency.
Untill now i postponed talking about the problem in action, so let me now post
an example.
Table "public.gpscookeddata"
Column | Type | Modifiers
-----------+-----------------------------+-----------
vslid | integer | not null
utcts | timestamp without time zone | not null
latid | double precision |
longi | double precision |
tracktrue | double precision |
avgspeed | double precision |
minspeed | double precision |
maxspeed | double precision |
Indexes:
"gpscookeddata_pkey" primary key, btree (vslid, utcts)
"gpscookeddata_utcts" btree (utcts)
"gpscookeddata_vslid" btree (vslid)
where utcts holds the UTC (GMT) timestamp.
some sample data:
utcts | latid | longi | tracktrue | avgspeed | minspeed | maxspeed ---------------------+----------+----------+-----------+----------+----------+---------- 2006-03-26 02:29:49 | -2256.13 | -3707.46 | 211.1 | 13.6 | 13.3 | 14 2006-03-26 02:59:49 | -2302.31 | -3703.83 | 207.7 | 14 | 13.8 | 14.1 2006-03-26 03:29:49 | -2308.7 | -3700.11 | 209.4 | 14.4 | 14 | 14.6 2006-03-26 03:59:49 | -2315.16 | -3656.16 | 210.4 | 14.8 | 14.5 |15 (4 rows) Now if the application wants to read data between start='2006-03-26 02:00:00' and end='2006-03-26 04:00:00'
java.text.SimpleDateFormat dfrm =
dfrm.setTimeZone(TimeZone.getTimeZone("GMT"));
java.util.Date gstartDate = dfrm.parse(start);
java.util.Date gendDate = dfrm.parse(end);
//here the two dates have the correct values (millisecond wise)
st = con.prepareStatement("select
st.setInt(1,Integer.parseInt(vslid)); st.setTimestamp(2,new java.sql.Timestamp(gendDate.getTime())); st.setTimestamp(3,new java.sql.Timestamp(gstartDate.getTime())); if i do that then in the pgsql.log i get select utcts,latid,longi,tracktrue,avgspeed,minspeed,maxspeed from gpscookeddata where vslid=92 and utcts<'2006-03-26 07:00:00.000000+03' and utcts>='2006-03-26 05:00:00.000000+03' order by utctswhich does a wrong WHERE since the '+03' part is disragarded (correctly) by postgresql. So while technically the '2006-03-26 07:00:00.000000+03' value is fine, and the timestamp is indeed that one, this does not work in the query. If i rewrite the last 2 statements (as Chip Gobs suggested recently in the list, http://archives.postgresql.org/pgsql-jdbc/2007-12/msg00013.php) as st.setTimestamp(2,Timestamp.valueOf(end)); st.setTimestamp(3,Timestamp.valueOf(start)); then it does select utcts,latid,longi,tracktrue,avgspeed,minspeed,maxspeed from gpscookeddata where vslid=92 and utcts<'2006-03-26 04:00:00.000000+03' and utcts>='2006-03-26 02:00:00.000000+02' order by utctswhich although technically wrong gives the right query results (notice that at 2006-03-26 03:00:00 EET the time zone is increased by 1 (+03), to reflect dayligght savings). Even then, it seems that jdbc does yet another conversion when transfering timestamp values:
rs = st.executeQuery();
java.util.Date thists = rs.getTimestamp(1);
....
System.out.println("utc="+thists);
....
} rs.close(); st.close(); The above gives, utc=2006-03-26 02:29:49.0 utc=2006-03-26 02:59:49.0 utc=2006-03-26 04:29:49.0 (!!! +1) utc=2006-03-26 04:59:49.0 (!!! +1) so it interprets the above dates as greek dates, so at this point i have lost track and cant get the right dates. As you may have found (if you have read up to this point - i hope!!), i have not found a decent way to deal with this beast. Taking into account the huge difficulty to make everything "with time zone" in the database, along with the fact that some "local" dates really have *no* accompanying timezone info, and thus presenting them as "with time zone" would be a lie, how should deal with this?
Any help much appreciated.
-- Achilleas Mantzios ---------------------------(end of broadcast)--------------------------- TIP 5: don't forget to increase your free space map settingsReceived on Tue Jan 8 07:14:56 2008 This archive was generated by hypermail 2.1.8 : Wed Jun 18 2008 - 23:42:17 EDT |
||||||||||
|
|||||||||||