Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 1CFC61337B8B for ; Mon, 2 May 2011 07:45:35 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 09322-10 for ; Mon, 2 May 2011 10:45:16 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from depesz.com (andy.depesz.com [88.198.47.100]) by mail.postgresql.org (Postfix) with ESMTP id 379FC13364DE for ; Mon, 2 May 2011 07:45:16 -0300 (ADT) Received: from andy.depesz.com ([88.198.47.100] helo=depesz.com) by depesz.com with esmtpa (Exim 4.75) (envelope-from ) id 1QGqcs-0007Wb-HQ; Mon, 02 May 2011 12:45:14 +0200 Date: Mon, 2 May 2011 12:45:14 +0200 From: hubert depesz lubaczewski To: Marek Nos Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #6001: date_trunc is not timezone aware Message-ID: <20110502104514.GA2709@depesz.com> Reply-To: depesz@depesz.com References: <201105011848.p41ImvOT084137@wwwmaster.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <201105011848.p41ImvOT084137@wwwmaster.postgresql.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/2 X-Sequence-Number: 30141 On Sun, May 01, 2011 at 06:48:57PM +0000, Marek Nos wrote: > > The following bug has been logged online: > > Bug reference: 6001 > Logged by: Marek Nos > Email address: marek.nos@centrum.cz > PostgreSQL version: 9.0 > Operating system: Windows 7 > Description: date_trunc is not timezone aware > Details: > > and other like extract() as well > > SELECT date_trunc('day', '2011-05-02 02:00:00+05'::timestamptz) > > I would expect result as > '2011-05-02 00:00:00+05' > but > '2011-05-01 00:00:00+02' is given (pre conversion to server timezone is done > first) because you didn't specify which timezone you *want*. when you're connecting to server, and not providing your own time zone, it is assumed that you're using the same time zone as server. and all timestamptz, in all time zones, are converted to your time zone: $ show timezone; TimeZone ---------- Poland (1 row) $ select now(); now ------------------------------ 2011-05-02 12:43:56.56492+02 (1 row) $ SELECT '2011-05-02 02:00:00+05'::timestamptz; timestamptz ------------------------ 2011-05-01 23:00:00+02 (1 row) To make the calculations work in different time zone, simply set it: $ set timezone = '+05'; SET $ select now(); now ------------------------------- 2011-05-02 15:44:38.044978+05 (1 row) $ SELECT '2011-05-02 02:00:00+05'::timestamptz; timestamptz ------------------------ 2011-05-02 02:00:00+05 (1 row) $ SELECT date_trunc('day', '2011-05-02 02:00:00+05'::timestamptz); date_trunc ------------------------ 2011-05-02 00:00:00+05 (1 row) depesz -- The best thing about modern society is how easy it is to avoid contact with it. http://depesz.com/