Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id D9B66632410 for ; Mon, 15 Feb 2010 13:21:32 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 19841-09 for ; Mon, 15 Feb 2010 17:21:22 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 6E7276322F8 for ; Mon, 15 Feb 2010 13:21:22 -0400 (AST) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.3/8.14.3) with ESMTP id o1FHLLqS038290 for ; Mon, 15 Feb 2010 13:21:21 -0400 (AST) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.3/8.14.3/Submit) id o1FG8khh010140 for pgsql-jdbc@postgresql.org; Mon, 15 Feb 2010 12:08:46 -0400 (AST) (envelope-from news) From: Richard Rowell X-Newsgroups: pgsql.interfaces.jdbc Subject: Re: Timestamp confusion Date: Mon, 15 Feb 2010 08:08:46 -0800 (PST) Organization: http://groups.google.com Lines: 101 Message-ID: <20104951-ea08-486e-8dab-e4e1f15b1e16@f29g2000yqa.googlegroups.com> References: <751b9c0a-823d-477a-a6bf-f7d7a26103b2@g28g2000yqh.googlegroups.com> <4B75CAB1.3030201@opencloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Complaints-To: groups-abuse@google.com Complaints-To: groups-abuse@google.com Injection-Info: f29g2000yqa.googlegroups.com; posting-host=71.205.132.208; posting-account=gEzRdwkAAABnZ9U_iU8jy5lPpDr1oi-0 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.43 Safari/532.5, gzip(gfe), gzip(gfe) To: pgsql-jdbc@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/26 X-Sequence-Number: 20313 Thank you Oliver for the followup. I did search the archives and read the group FAQ and even STFW before I posted, but obviously I have missed much. On Feb 12, 4:40=A0pm, oli...@opencloud.com (Oliver Jowett) wrote: > Richard Rowell wrote: > > I'm perplexed by the behaviour of ResultSet.getTimestamp() when passed > > a calendar. > > I ran your testcase and it seems to be working fine. Here's my output: > > > > > > > > > Default TZ: New Zealand Standard Time > > Application time: 2010-02-13 10:15:31.327 > > > Default Timezone: > > Row(1) d1(2010-02-13 10:15:31.327) d2(2010-02-13 10:15:31.327) > > Row(2) d1(2010-02-13 10:15:31.327) d2(2010-02-12 13:15:31.327) > > > PST Timezone: > > Row(1) d1(2010-02-13 10:15:31.327) d2(2010-02-14 07:15:31.327) > > Row(2) d1(2010-02-13 10:15:31.327) d2(2010-02-13 10:15:31.327) > > > PG Timezone adjust to PST8PDT, Default Calendar: > > Row(1) d1(2010-02-13 10:15:31.327) d2(2010-02-13 10:15:31.327) > > Row(2) d1(2010-02-13 10:15:31.327) d2(2010-02-12 13:15:31.327) > > > PG Timezone adjust to PST8PDT, PST Calendar > > Row(1) d1(2010-02-13 10:15:31.327) d2(2010-02-14 07:15:31.327) > > Row(2) d1(2010-02-13 10:15:31.327) d2(2010-02-13 10:15:31.327) > > CREATE TABLE dtest ( > > =A0 =A0 uid integer NOT NULL, > > =A0 =A0 d1 timestamp with time zone, > > =A0 =A0 d2 timestamp without time zone > > ); > > I made uid a SERIAL so your test code actually worked, and turned on > autocommit so I could look at the results after the fact: > > testdb=3D# select * from dtest; > =A0uid | =A0 =A0 =A0 =A0 =A0 =A0 d1 =A0 =A0 =A0 =A0 =A0 =A0 | =A0 =A0 =A0= =A0 =A0 d2 > -----+----------------------------+------------------------- > =A0 =A01 | 2010-02-13 10:15:31.327+13 | 2010-02-13 10:15:31.327 > =A0 =A02 | 2010-02-13 10:15:31.327+13 | 2010-02-12 13:15:31.327 > (2 rows) > > d1 identifies a particular instant in time, and you are always > formatting it with the default calendar (because you're just using > Timestamp.toString()), so it always ends up as exactly the same value > when displayed, regardless of what Calendar is passed. > > d2 identifies a particular *clock time*, which is a different instant in > time depending on what timezone you interpret it in. The server timezone > is entirely irrelevant. The Calendar you pass determines what timezone > it is interpreted in. > > So row 1's d2 is the clock time in the default timezone when the test > was run. > Row 2's d2 is the clock time in PST when the test was run. > > As you can see from the raw data shown by psql, row 2 is 21 hours behind > row 1. That's right, because PST is 21 hours behind NZ. > > Then all the output for d2 just follows from that. When you pass the > default calendar, you get Timestamps that are correct for the default > calendar. Timestamp.toString() uses the default calendar, so those cases > just reflect the values in the table directly. > > When you pass a PST Calendar when retrieving d2 from row 1, what you're > saying is "give me a Timestamp for the instant in time that is > 2010-02-13 10:15:31.327 in PST". This is not the same instant in time as > when the test is run - it is 21 hours *later* than the test ran, i.e. > 2010-02-14 07:15:31.327 in the NZ timezone. > > Then you print that value in the *default timezone*, which is > essentially saying "what time is 2010-02-13 10:15:31.327 PST in the NZ > timezone?". And it displays that correctly as 2010-02-14 07:15:31.327. > > The same sort of argument applies to row 2. > > Usually, you want to format timestamps using the same Calendar you used > to retrieve them. You're not doing that in your test code - Timestamp > stores no timezone information, and Timestamp.toString() always uses the > default JVM timezone. > > I've written this explanation, or a similar one, many times in the past. > You probably want to search the list archives. > > -O > > -- > Sent via pgsql-jdbc mailing list (pgsql-j...@postgresql.org) > To make changes to your subscription:http://www.postgresql.org/mailpref/p= gsql-jdbc