pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: How to get an inclusive interval when using daterange
Date: Tue, 3 Apr 2018 10:12:52 -0700
Message-ID: <ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com> (raw)
In-Reply-To: <CAMsqVxu21bqOWfXGcT2zK9QRJNzmH-=7=+W-tjATHC3X-yyKXg@mail.gmail.com>
References: <CAMsqVxtEkr-KwmvoM==+8vruv0hMc9bLd8E+csrRzAxfkx36wg@mail.gmail.com>
	<CADgbbqhc6sfWB4T_9ewCu7m3bHw0h-ivSd5wWb_D47fp35H=9w@mail.gmail.com>
	<CAMsqVxv=kgk+vw7ohbcwGX2mw2NMLre2LR6isv7Y_pyW4hBoZA@mail.gmail.com>
	<918a4ac1-2d07-c273-c809-8f0a3925bfcd@aklaver.com>
	<CAMsqVxu5C1-cpOS10Gu7yoqo1G0csw63bUbLEUb6EWWoyKXp4w@mail.gmail.com>
	<CAMsqVxu21bqOWfXGcT2zK9QRJNzmH-=7=+W-tjATHC3X-yyKXg@mail.gmail.com>

On 04/03/2018 09:40 AM, hmidi slim wrote:
> I tried insert into availability values ('product x', 
> '[2018-02-02,2018-03-01]'::daterange); and I got the same result such 
> as insert into availability values ('product x', daterange('2018-02-02', 
> '2018-03-01', '[]').

Yes, those are equivalent ways of constructing the same daterange.

If you really want a closed/closed daterange, you'll need to create your 
own type. I don't really recommend that, but you can do it. (Using 
close/open is the standard because it's so convenient for 
combining/comparing ranges.)

It's easy to create a type without a canonical function, e.g.:

     CREATE FUNCTION date_minus(date1 date, date2 date)
     RETURNS float AS $$
     SELECT cast(date1 - date2 as float);
     $$ LANGUAGE sql immutable;

     CREATE TYPE daterange2 AS range
     (subtype = date, subtype_diff = date_minus);

Then you can say:

     SELECT daterange2('2018-01-01', '2018-03-01', '[]');

This is not great though, because without a canonical function Postgres 
doesn't know that [x,y] is equal to [x,y+1). If you prefer to have a 
canonical function, you'll need to write one in C. (It's easy but you 
won't be able to install it on a managed service like AWS RDS.) It might 
help to read these and the code they link to (The second one is by me.):

https://stackoverflow.com/questions/29895077/how-to-create-a-new-date-range-type-with-included-upper...

https://illuminatedcomputing.com/posts/2016/06/inet-range/

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com




view thread (6+ messages)

Message-ID: <ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com>
Permalink:  ../ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com/
Also on:    postgresql.org/message-id/ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com

 · 

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: pj@illuminatedcomputing.com
  Subject: Re: How to get an inclusive interval when using daterange
  In-Reply-To: <ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com>
  References: <CAMsqVxtEkr-KwmvoM==+8vruv0hMc9bLd8E+csrRzAxfkx36wg@mail.gmail.com> <CADgbbqhc6sfWB4T_9ewCu7m3bHw0h-ivSd5wWb_D47fp35H=9w@mail.gmail.com> <CAMsqVxv=kgk+vw7ohbcwGX2mw2NMLre2LR6isv7Y_pyW4hBoZA@mail.gmail.com> <918a4ac1-2d07-c273-c809-8f0a3925bfcd@aklaver.com> <CAMsqVxu5C1-cpOS10Gu7yoqo1G0csw63bUbLEUb6EWWoyKXp4w@mail.gmail.com> <CAMsqVxu21bqOWfXGcT2zK9QRJNzmH-=7=+W-tjATHC3X-yyKXg@mail.gmail.com> <ede1f883-4dbd-9035-6543-d35abce46209@illuminatedcomputing.com>

* 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