X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 18D8E52B79 for ; Mon, 25 Jul 2005 13:45:35 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 26048-06 for ; Mon, 25 Jul 2005 16:45:31 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.204]) by svr1.postgresql.org (Postfix) with ESMTP id 950B252B46 for ; Mon, 25 Jul 2005 13:45:25 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id i34so951197wra for ; Mon, 25 Jul 2005 09:45:25 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=pwh9bqz+MKR9KwqkkamxpF/yx4WpFwINeIb43W5jQOspY1IciF49GCzYqwWTtUjCLssXUKilG39XJISsvXydIuOopWjKPwuVb9Gr0aU/YlSJTZ4LW5mWS2xQAl/qu//L7eWDIyK+gmizFHZu2ei0wgEOBnL/OCHw3i4KzjqR86w= Received: by 10.54.13.37 with SMTP id 37mr2261084wrm; Mon, 25 Jul 2005 09:45:25 -0700 (PDT) Received: by 10.54.86.16 with HTTP; Mon, 25 Jul 2005 09:45:25 -0700 (PDT) Message-ID: <90876a9e050725094560a4aa6a@mail.gmail.com> Date: Mon, 25 Jul 2005 10:45:25 -0600 From: Christian Cryder Reply-To: Christian Cryder To: List Subject: Timestamp Summary Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.074 tagged_above=0 required=5 tests=AWL, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200507/284 X-Sequence-Number: 13432 Hey folks, I just wanted to take a few moments and summarize where we are in our Timestamp woes issue. I'm also going to post a piece of code that illustrates some of the problems we've experienced, and then try and explain the conclusions we've arrived at. Hopefully this well help others in the future. First of all, the code snippet to easily duplicate the problem (see the bottom of this email). What this code does is create a table Foo, and then it inserts 4 rows using a SQL Statement. It then turns around and reads those values via JDBC, and then simply re-inserts the values back into the DB. As the example illustrates, the data written is NOT the same as the data read (you can see this by querying the table through the sql console after you've run the example). The key point to note here is that the 4 dates being inserted originally fall immediately around and in the daylight savings cutover (2-3 AM, 1st Sunday of April). So t1 is before daylight savings, t2 is during the rollover (and thus technically not a valid daylight savings time), t3 and t4 are after the rollover. With DST turned on on the client, here's the results in the table... ---------------------------------------------------- 101;"2005-04-03 00:06:02" 102;"2005-04-03 02:29:43" 103;"2005-04-03 03:29:43" 104;"2005-04-03 04:35:17" 105;"2005-04-03 00:06:02" 106;"2005-04-03 03:29:43" 107;"2005-04-03 03:29:43" 108;"2005-04-03 04:35:17" This looks pretty good except that the original t2 (02:29) got munged to 03:29 in the DB.. With DST programatically turned off on the client, the dates display properly in the client output (no munging), but they still get munged on the server side of things (because there, DST is still turned on) - and there, everything above the cutoff gets adjusted. ----------------------------------------------------- 125;"2005-04-03 00:06:02" 126;"2005-04-03 02:29:43" 127;"2005-04-03 03:29:43" 128;"2005-04-03 04:35:17" 129;"2005-04-03 00:06:02" 130;"2005-04-03 03:29:43" 131;"2005-04-03 04:29:43" 132;"2005-04-03 05:35:17" So if we turn off DST on both the client -AND- the server (for me, the server part happens by issuing a "set timezone=3D'etc/gmt+7'" before I use the connection), we finally get what we're looking for - write, read, write, w/ proper values on both display and in the db when we're all done. ----------------------------------------------------- 133;"2005-04-03 00:06:02" 134;"2005-04-03 02:29:43" 135;"2005-04-03 03:29:43" 136;"2005-04-03 04:35:17" 137;"2005-04-03 00:06:02" 138;"2005-04-03 02:29:43" 139;"2005-04-03 03:29:43" 140;"2005-04-03 04:35:17" In other words, nothing got munged.=20 At this point, I'd like to submit a piece of code that will ensure that the server is operating in the same time zone as the client connection, taking DST settings into consideration as well (Dave should understand where this would go as he was playing around with this type of thing when helping me debug last week): //in order to avoid munging dates on insert, we need to set the server to the same timezone //as the client for the duration of this connection. If daylight savings is turned on, this is //simple: just send the current timezone; if dst is turned off, however, then we need to send the GMT //equivalent (w/ no DST offset), which takes the form of etc/gmt+X (where X is the number of hours) TimeZone tz =3D TimeZone.getDefault(); int roff =3D tz.getRawOffset(); if (tz.useDaylightTime()) { stmt.execute("set timezone=3D'"+tz.getID().toLowerCase()+"'"); } else { stmt.execute("set timezone=3D'etc/gmt"+(roff<=3D0 ? "+" : "")+(roff/-3600000)+"'"); } This piece of code only needs to get executed when the connection is first opened to the DB, and from that point on it will ensure that the db is in functionally the same timezone as the server (taking DST on/off into account as well). In other words, if my client is running in MST w/ DST turned off, the server will be configured to etc/gmt+7, and any dates I insert will be left untouched. That make sense? If you'd like to see an example of the strings generated for each timezone, here's a snippet... String zones[] =3D TimeZone.getAvailableIDs(); for (int i=3D0; i