pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Ron <ronljohnsonjr@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Date calculation
Date: Thu, 31 Jan 2019 20:53:07 +0000
Message-ID: <87d0oca8l0.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <c10b17de-623b-4af3-8a8c-45c4a3a5826d@gmail.com>
References: <c10b17de-623b-4af3-8a8c-45c4a3a5826d@gmail.com>

>>>>> "Ron" == Ron  <ronljohnsonjr@gmail.com> 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)




view thread (6+ messages)  latest in thread

Message-ID: <87d0oca8l0.fsf@news-spur.riddles.org.uk>
Permalink:  ../87d0oca8l0.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87d0oca8l0.fsf@news-spur.riddles.org.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, ronljohnsonjr@gmail.com
  Subject: Re: Date calculation
  In-Reply-To: <87d0oca8l0.fsf@news-spur.riddles.org.uk>
  References: <c10b17de-623b-4af3-8a8c-45c4a3a5826d@gmail.com> <87d0oca8l0.fsf@news-spur.riddles.org.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox