Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 49087634CE0 for ; Tue, 30 Jun 2009 03:52:02 -0300 (ADT) 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 95411-03-8 for ; Tue, 30 Jun 2009 03:51:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from megafon.hr (megafon.hr [213.202.104.5]) by mail.postgresql.org (Postfix) with ESMTP id 20BF46348FC for ; Tue, 30 Jun 2009 03:51:35 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by megafon.hr (Postfix) with ESMTP id A1139250058; Tue, 30 Jun 2009 08:51:33 +0200 (CEST) X-Virus-Scanned: ClamAV at megafon.hr Received: from megafon.hr ([127.0.0.1]) by localhost (localhost.localdomain [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5Mvr4OkG1BCK; Tue, 30 Jun 2009 08:51:28 +0200 (CEST) Received: from [IPv6:::1] (localhost.localdomain [127.0.0.1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: mike) by megafon.hr (Postfix) with ESMTPSA id 83A4D25004E; Tue, 30 Jun 2009 08:51:28 +0200 (CEST) Message-ID: <4A49B5EE.8030309@megafon.hr> Date: Tue, 30 Jun 2009 08:51:26 +0200 From: Mario Splivalo User-Agent: Thunderbird 2.0.0.22 (X11/20090608) MIME-Version: 1.0 To: Tom Lane CC: pgsql Subject: Re: date_trunc should be called date_round? References: <4A487F94.3070303@megafon.hr> <11724.1246285520@sss.pgh.pa.us> In-Reply-To: <11724.1246285520@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200906/125 X-Sequence-Number: 32730 Tom Lane wrote: > Mario Splivalo writes: >> But, date_trunc behaves like round function: round(1.9) = 2. > > Hmm ... only for float timestamps, and only for the millisec/microsec > cases. > > case DTK_MILLISEC: > #ifdef HAVE_INT64_TIMESTAMP > fsec = (fsec / 1000) * 1000; > #else > fsec = rint(fsec * 1000) / 1000; > #endif > break; > case DTK_MICROSEC: > #ifndef HAVE_INT64_TIMESTAMP > fsec = rint(fsec * 1000000) / 1000000; > #endif > break; > > I wonder if we should change this to use floor() instead. > I guess it's safe, since you don't have negative timestamps (right?) or parts of timestamps (millisecs/microsecs), so floor() would act as trunc. Esp. if for the other parts of timestamp (days, hours, ...) it's actualy truncating, not rounding, i.e.: date_trunc('minute', '2009-01-01 12:13:50'::timestamp) would return '2009-01-01 13:13:00', not '2009-01-01 13:14:00'. One would expect similar behavior for the milli/microsec part. Now it's truncating, unless dealing with milli/microseconds, where it's rounding. Mike