Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpJSD-0001CV-No for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2019 21:00:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gpJSC-000414-El for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2019 21:00:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpJSC-00040x-1F for pgsql-general@lists.postgresql.org; Thu, 31 Jan 2019 21:00:56 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpJS8-0000uo-VM for pgsql-general@postgresql.org; Thu, 31 Jan 2019 21:00:54 +0000 Received: from [192.168.127.1] (port=28731 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gpJS5-0007mj-G0; Thu, 31 Jan 2019 21:00:49 +0000 Received: from [127.0.0.1] (port=27460 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gpJS4-000EBd-Q6; Thu, 31 Jan 2019 21:00:48 +0000 From: Andrew Gierth To: Bruce Momjian Cc: Ron , pgsql-general@postgresql.org Subject: Re: Date calculation In-Reply-To: <20190131202340.GB3377@momjian.us> (Bruce Momjian's message of "Thu, 31 Jan 2019 15:23:40 -0500") Message-ID: <875zu4a6ox.fsf@news-spur.riddles.org.uk> References: <20190131201514.GA3377@momjian.us> <24e5ee21-0b9f-e3c8-2553-817813a04593@gmail.com> <20190131202340.GB3377@momjian.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) X-Mayan-Date: Long count = 13.0.6.3.12; tzolkin = 13 Eb; haab = 5 Pax Date: Thu, 31 Jan 2019 21:00:48 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Bruce" == Bruce Momjian writes: Bruce> Oh, right, you want date, so use: Bruce> SELECT date_trunc('week', CURRENT_DATE) + '6 days'; Three major things wrong with this: 1. If you do this on Sunday, it gives you the current day not the _next_ Sunday. 2. If you try and do this for other days of the week it doesn't work at all, instead giving you the specified day of the current week whether or not it's before or after the current day. 3. It's letting PG cast the date to a timestamptz, which is inefficient, possibly incorrect, and mutable; you want to force it to cast to timestamp without timezone instead. (A good rule of thumb is that you virtually never want to cast dates to timestamptz; the natural cast from date is to timestamp _without_ timezone.) -- Andrew (irc:RhodiumToad)