pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Bryn Llewellyn <bryn@yugabyte.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: Looking for a doc section that presents the overload selection rules
Date: Fri, 22 Oct 2021 08:49:28 -0700
Message-ID: <36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.com> (raw)
In-Reply-To: <B4E55133-926B-4A0D-8E17-C0EE984531E7@yugabyte.com>
References: <4F35D95E-4974-44FC-AA4B-3F1E94580B82@yugabyte.com>
	<b9d87b72-aaf2-1d6a-951a-d67f866fbd66@aklaver.com>
	<F71D1FD3-F0A9-48F1-B8F2-9018D6D0DF31@yugabyte.com>
	<4f7a9fe8-ea27-e4a3-34ac-f05fcfb7f5c3@aklaver.com>
	<B4E55133-926B-4A0D-8E17-C0EE984531E7@yugabyte.com>

On 10/21/21 17:42, Bryn Llewellyn wrote:
>> On 21-Oct-2021, at 17:15, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
>>
>> On 10/21/21 15:45, Bryn Llewellyn wrote:
>>>> /Adrian Klaver wrote:/
>>>>

>>> *Why is there no "date" overload of "to_char()"?*
> 
> You've lost me entirely here, I'm afraid.
> 
> My question was simple: why is there no "to_char ( date, text ) → text" overload?

Because there is:

to_char ( timestamp with time zone, text ) → text

and date is a subset of timestamp(tz) where timestamptz is the preferred 
form.  This allows you to do things like:

select '10/30/21 22:00:00'::timestamptz - '10/22/21'::date;
     ?column?
-----------------
  8 days 22:00:00

> 
> Without this, and as long as the good practice rule is followed to code so that implicit conversion is never invoked, then using "to_char()" on a "date" value requires writing an explicit typecast. There are only two possible choices: cast to plain "timestamp" or cast to "timestamptz". And for reasons that I'm sure you'd explain better than I would, the choice makes no actual difference to the outcome when a template is used that's natural for a "date" value.
> 
> So the consequence is that you have to write cluttered code and a fairly elaborate comment to say what your intention is.

Welcome to date/times. As I have said before if you are looking for end 
to end consistency you are in the wrong place. It is an area where 
specific comments are needed to explain your choices.

I am not understanding the template reference as there is a difference 
in output between timestamp and timestamptz.

> 
> What would you do here? Would you break the rule of practice so that you simply invoke "to_char()" on a "date" value _without writing a typecast_ and then letting the implicit conversion (which we know is to "timestamptz") have its innocent effect?
> 

Working in timestamptz is a benefit so I'm fine with that being the 
default.

The bottom line is that working correctly with date/times is difficult 
and requires effort spent studying the underlying structure. More so 
when you switch between systems e.g. Javascript 0 indexed months.


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (15+ messages)  latest in thread

Message-ID: <36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.com>
Permalink:  ../36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.com/
Also on:    postgresql.org/message-id/36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.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: adrian.klaver@aklaver.com, bryn@yugabyte.com, david.g.johnston@gmail.com
  Subject: Re: Looking for a doc section that presents the overload selection rules
  In-Reply-To: <36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.com>
  References: <4F35D95E-4974-44FC-AA4B-3F1E94580B82@yugabyte.com> <b9d87b72-aaf2-1d6a-951a-d67f866fbd66@aklaver.com> <F71D1FD3-F0A9-48F1-B8F2-9018D6D0DF31@yugabyte.com> <4f7a9fe8-ea27-e4a3-34ac-f05fcfb7f5c3@aklaver.com> <B4E55133-926B-4A0D-8E17-C0EE984531E7@yugabyte.com> <36e3e2a5-cf09-61e2-3f12-3ea6d17e3ccd@aklaver.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