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 1gpJKm-0000hm-GA for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2019 20:53:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gpJKk-0000U4-E2 for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2019 20:53:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpJKk-0000Tx-5q for pgsql-general@lists.postgresql.org; Thu, 31 Jan 2019 20:53:14 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpJKh-0001Fe-VQ for pgsql-general@postgresql.org; Thu, 31 Jan 2019 20:53:13 +0000 Received: from [192.168.127.1] (port=32763 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 1gpJKf-0007lj-Db; Thu, 31 Jan 2019 20:53:09 +0000 Received: from [127.0.0.1] (port=24835 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gpJKe-000E9A-Jh; Thu, 31 Jan 2019 20:53:08 +0000 From: Andrew Gierth To: Ron Cc: pgsql-general@postgresql.org Subject: Re: Date calculation In-Reply-To: (Ron's message of "Thu, 31 Jan 2019 14:11:14 -0600") Message-ID: <87d0oca8l0.fsf@news-spur.riddles.org.uk> References: 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 20:53:07 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Ron" == Ron writes: Ron> Hi, Ron> v9.6.6 Ron> Is there a built in function to calculate, for example, next Ron> Sunday? No, but such things aren't hard to calculate using the available primitives. To get "next Xday", for example, you can add 7 days and then do "previous or current Xday". In turn, "previous or current Xday" can be done by subtracting (X-Monday), doing date_trunc 'week', and adding (X-Monday) again. select current_date, date_trunc('week', (current_date + 7 - 6)::timestamp)::date + 6; current_date | ?column? --------------+------------ 2019-01-31 | 2019-02-03 (1 row) If you do this sort of thing a lot, then define your own functions for it: -- create this to override the cast to timestamptz that otherwise messes -- things up: create function date_trunc(text,date) returns date language sql immutable as $f$ select date_trunc($1, $2::timestamp)::date; $f$; -- perfect hash function for weekday names, with Monday=0 -- (accepts upper, lower or mixed case) create function dayno(text) returns integer language sql immutable as $f$ select (( ((ascii(substring($1 from 3)) & 22)*10) # (ascii($1) & 23) )*5 + 2) % 7; $f$; create function next_dow(start_date date, day_name text) returns date language sql immutable as $f$ select date_trunc('week', (start_date + 7 - dayno(day_name))) + dayno(day_name); $f$; select current_date, next_dow(current_date, 'Thursday'), next_dow(current_date, 'Friday'); current_date | next_dow | next_dow --------------+------------+------------ 2019-01-31 | 2019-02-07 | 2019-02-01 -- Andrew (irc:RhodiumToad)