adrian.klaver@xxxxxxxxxxx wrote: I’ll make this my final turn on this thread. Yes, I accept that everything to do with the date-time story is tough stuff. And I do understand that this is, to a large extent, just a reflection of the fact that the terrain is inevitably affected by genuine astronomical facts together with the history of technology and human thought. I accept, too, that the PostgreSQL implementation in this space is constrained, to some extent, by decisions taken by the SQL Standard folks, over the years, some of which were arguably questionable. Then PostgreSQL brings its own quirks (esp. e.g. everything to do with intervals and their use). So I do see that application code will need a lot of commenting to make the programmer’s intent clear. Having said all this, the following example seems to me to make an unassailable point: deallocate all; prepare s as with c as ( select '2021-06-15' ::date as d, '2021-06-15 12:00:00'::timestamp as ts, '2021-06-15 12:00:00'::timestamptz as tstz) select rpad(current_setting('timezone'), 20) as "timezone", d ::text, ts ::text, tstz::text from c; \t on set timezone = 'Europe/Helsinki'; execute s; set timezone = 'America/Los_Angeles'; execute s; \t This is the result: Europe/Helsinki | 2021-06-15 | 2021-06-15 12:00:00 | 2021-06-15 22:00:00+03 America/Los_Angeles | 2021-06-15 | 2021-06-15 12:00:00 | 2021-06-15 12:00:00-07 So the “::text” typecast operator understands that “date” values, plain “timestamp” values, and “timestamptz” values each bear different information—and it takes account, in each case, only of the relevant information. There could, so easily, have been three “to_char()” overloads for these three data types that honored the spirit of the “::text” typecast by rendering only what’s meaningful, despite what the template asks for. I could write these myself and use “extract()” to get the meaningful elements, and only these, before rendering them, silently refusing the request to render meaningless fields. However, reality is different. “to_char()” uses defaults when the value at hand doesn’t represent these and then renders them as the template specifies. prepare s as with c as ( select '2021-06-15 12:00:00'::text as t, 'dd-Mon-yyyy TZH:TZM' as fmt) select to_char(t::timestamp, fmt) as "plain timestamp", to_char(t::timestamptz, fmt) as "timestamptz" from c; This always shows the “TZH:TZM” component of the plain “timestamp” as “00:00”. And for the “timestamptz” value, it shows this to reflect the session’s timezone setting. This informs what you get when you want to render a “date” value—and how to code it. Of course, when I do this, I wouldn’t ask to see time-of-day or timezone fields. So fair enough, I suppose. Anyway, so it is—and so it ever will be. |